/*
 * This is the vote behaviour for all content in Funny-City.com
 */

/* Language */
var lang = new Array();
lang['added_in_favourites'] = 'προστέθηκε στα αγαπημένα';
lang['thanks_for_voting'] = 'η ψήφος σας καταχωρήθηκε';


$(document).ready(function(){
	$("#ratingblock-div .rater").click(function(e){
		
		var rating, width_of_stars;	
		// first switch the ul with a loading ul	
		$("#unit_ul"+content_id).html("<div class=\"loading\"></div>");
		
		// the rating he gave
		rating = $(this).attr("title");
		// the width of the rating after the vote is registered
		width_of_stars = rating * 26;
		
		$.get("/ajax_vote.php?id="+content_id+"&vote="+rating, {}, function(results) {

			var votes_count, votes_avg_number;
			
			votes_count_number = $('votes_count', results).text();
			votes_avg_number = $('votes_avg', results).text();
			// alert(votes_count_number);
			
			$("#votes_avg").text(votes_avg_number);
			$("#votes_count").text(votes_count_number);
			$("#votes-count-under-stars").text(votes_count_number);
			$("#rate-it").text(lang['thanks_for_voting']);
			$("#rate-it").attr( "class", "thanks");

			// make the inside of the stars to be simply the rating he gave

			$("#unit_ul"+content_id).html("<li class=\"current-rating\" style=\"width:"+width_of_stars+"px;\"></li>");
		});
		e.preventDefault();
	});
	
	// now deal with the add to favourites only if he is a member
	if (mem_id>0) {
		$("#addtofavslink").click(function(e){
			$("#addtofavs").html("<div class=\"favs\"><div class=\"loading\" style=\"background-position: 10px 50%;\"></div></div>");
			$.get("/ajax_add_to_favourites.php?cont_id="+content_id+"&mem_id="+mem_id, {}, function() {
				// just change the text to added to favourites
				$("#addtofavs").html("<div class=\"favs\">"+lang['added_in_favourites']+"</div> ");
			});
			e.preventDefault();
		});
	}
	
});