	window.addEvent('domready', function() {
		var url = '/bin/ajax/joke_vote.php';
		var url_get = '/bin/ajax/joke_get_vote.php';
		
		var joke_rating = 'joke_rating';
		var joke_rating_up = 'joke_rating_up';
		var joke_rating_down = 'joke_rating_down';
		
		function jokeVote(value, joke_rate_id) {
			$('joke_rating').setStyle('color', '#888');
			$('joke_rating').setHTML(voting_text);

			// tiny delay for better user experience
			(function() {
				voteRequest.send(url, "joke_id="+joke_id+"&rate="+value+"&ajax=1");
			}).delay(200);
		}

		// on domready, get current vote
		var getVote = new Ajax(url_get, {
			method: 'get',
			onComplete: function(data) {
				data = data.toString();

				if (data != "ERR") {
					tmp = data.split("|");
					$('joke_rating_up').setHTML(tmp[0]);
					$('joke_rating_down').setHTML(tmp[1]);
				}
			}
		});
		//alert(joke_id)
		if (typeof(joke_id) !== "undefined") {
			getVote.send(url_get, "joke_id="+joke_id);
		}

		// request object for rating
		
		var voteRequest = new Ajax(url, {
			method: 'post', 
			onComplete: function(data) {
				var out = "";

				if (data == "already") {
					$('joke_rating').setStyle('color', '#A7102A');
					$('joke_rating').setHTML(already_text);
							
					(function() { 
						$('joke_rating').setHTML(''); 
					}).delay(2000);
				}
				else {
					data = data.toString();
					tmp = data.split("|");
					 
					$('joke_rating_up').setHTML(tmp[0]);
					$('joke_rating_down').setHTML(tmp[1]);
					$('joke_rating').setHTML(''); 
				}
			}
		});
		
		if ($('joke_rate_up') && $('joke_rate_down')) {
			$('joke_rate_up').addEvent('click', function(event) {
				new Event(event).stop(); 
	
				this.blur();
				jokeVote(1);
			});
			
			$('joke_rate_down').addEvent('click', function(event) {
				new Event(event).stop();
	
				this.blur();
				jokeVote(-1);
			});
		}
	});
	
	function toggleJoke() {
		effect = $('text_joke_content').effects({duration: 700, transition: Fx.Transitions.Sine.easeInOut});
		joke_height = $('jokeText').getSize().size.y + 10;
		
		if (jokeOpened) {
			effect.start({'height': '100'});
			$('text_joke_gradient').setStyle('display', 'block');
			$('text_joke_arrow').removeClass('opened');
			jokeOpened = false;
		}
		else {
			effect.start({'height': joke_height});
			$('text_joke_gradient').setStyle('display', 'none');
			$('text_joke_arrow').addClass('opened');
			jokeOpened = true;
		}
	}
	
	function toggleJokeSend() {
		effect_slide = $('jokeSend').effects({duration: 700, transition: Fx.Transitions.Sine.easeInOut});
		
		if (jokeSend) {
			effect_slide.start({'height': '0'});
			jokeSend = false;
		}
		else {
			effect_slide.start({'height': '230'});
			jokeSend = true;
		}
	}
	
	/*
	 * Function send joke
	 * @param div_name Name of the div to write in
	 * @param script_name name of the script to execute
	 * @param text_ok text if article was sent
	 * @param text_error text if there goes something wrong
	 *
	*/
	function send_joke(div_name,script_name,text_ok, text_error, jokes_id) {
		var email_from = "email_from";
		var email_to = "email_to";
		var body = "body";
		var post_document = "joke_send";
		
		if(jokes_id !== undefined){
			email_from = email_from + "_" + jokes_id;
			email_to = email_to + "_" + jokes_id;
			body = body + "_" + jokes_id;
			post_document = post_document + "_" + jokes_id;
		}

		if( ($(email_from).value) == "$$ARTICLE_SEND_FROM$$" || ($(email_from).value) == "" )
                            {
                                            $(email_from).value = '';
                                            $(email_from).focus();
                                            return false;
                            }
		
		if( ($(email_to).value) == "$$ARTICLE_SEND_TO$$" || ($(email_to).value) == "" )
                            {
                                            $(email_to).value = '';
                                            $(email_to).focus();
                                            return false;
                            }
		
		if( ($(body).value) == "$$ARTICLE_SEND_MESSAGE$$")
			$(body).value = '';
		
		new Ajax(script_name, {
			method: 'post',
			encoding: 'utf-8',
			postBody: $(post_document),
			onComplete: function(response) {
				if (response == 'OK') {
					$(div_name).innerHTML = text_ok;
					try	{
						$("email_to").value = "";
					} catch (r) {null;}
				} else {
					$(div_name).innerHTML = text_error;
				}
			}
		}).request();
	}

