$(document).ready(function(){
	$("#contestMeniu a").click(function(){return false;});
	$("#contestContain").corner().parent().css('padding' ,'5px').corner("round br bl");
	$(".contestDay").corner("notch tl tr 10px cc:#D60062").corner("round br bl 15px");
	$("#contestTop").corner("round tl tr");
	$("#adaugaComment").corner();
	
	function refreshComments(id){
		$.post("newsite/top/showComm.php", {'id':id},
		function () {
		});
	};
	showRatings();
	$(".top_comment").corner();
	$(".comment").corner();
	$("#top_comment").corner();
	$("a .contestDay").click(function() {	
		var newDay = $(this);		
		if($("#expand").length == 0) {
			hideRatings(); 
			refreshVote();
			$(this).append("<div id=\"expand\"><ul><li>L</li><li>O</li><li>A</li><li>D</li><li>I</li><li>N</li><li>G</li><li><img src=\"newsite/top/img/loader.gif\" /></div>");
			$("#expand").css("width", newDay.css("width"));
			$("#expand").css("background-color", newDay.css("background-color"));
			$("#contestContain").empty();
			$("#contestContain").hide(1);
			$("#expand").slideDown(1200, function() {
			if(newDay.attr('name') == "week") { var newWeekId = newDay.attr("title"); var newDayId = 0;}
			else { var newDayId = newDay.attr("title"); var newWeekId = 0;}
			$.post("newsite/top/getPage2.php", { 'id_entry' : newDayId, 'id_week' : newWeekId},
 	 			function(data)
				{
					$("#contestContain").show("slow", 
						function(){
							$(this).corner();$("#contestContain").html(data);
							$(".title_week").corner("tl bl 20px cc:#D60062");
							$(".top_comment").corner();
							$(".comment").corner();
							$("#top_comment").corner();
							$("#adaugaComment").corner();
							if(!newWeekId)showRatings();
						});
					$("#expand").slideUp("slow", function(){$(this).remove();});
				});
		});
		}
	});
$("#parareaTa").corner();
 $("#voteaza ul").children().next().fadeTo(0, 0.9).next().fadeTo(0, 0.8).next().fadeTo(0, 0.7).next().fadeTo(0, 0.6);

function moveOnMouseOver(){
	$(this).append("<div class=\"postVote\"></div>");
	var theContent = $(this).attr("name");	
	$(this).contents().text(theContent);
	$(this).contents().stop().fadeIn("slow");
	$(this).fadeTo("fast", 1);
	$(this).animate({backgroundPosition:"0px 50%"},0);
}
function moveOnMouseOut() {
	$(this).contents().stop().fadeOut("slow");
	$(this).stop().animate({backgroundPosition:"0px 0%"}, 0);
	$(this).contents().remove();
	var len = $(this).prevAll().length;
	len = '0.'+len;
	$(this).fadeTo("slow", 1-len);
}
function onMouseClick() {
	$(this).prevAll().animate({backgroundPosition:"0px 100%"},100).unbind('click').unbind('mouseover').unbind('mouseout');
	$(this).nextAll().animate({backgroundPosition:"0px 100%"},100).unbind('click').unbind('mouseover').unbind('mouseout');
	$(this).animate({backgroundPosition:"0px 50%"},100);
	$(this).unbind('mouseover').unbind('mouseout').unbind('click');
	$(this).ready(getVoteResponse($(this)));
}
function hideRatings() {
	$("#voteaza li div").remove();
	$("#voteaza ul li").unbind('mouseover').unbind('mouseout').unbind('click');
	$("#voteaza ul li").css("cursor", "cross");
	$(".total_votes").fadeOut("slow", function(){ $(this).remove();});
}
function refreshVote() {
	$("#voteaza li").animate({backgroundPosition:"0px 0%"}, 100);
}
function refreshAll() {
	hideRatings();
	refreshVote();
	showNewRating();
}
function showNewRating() {
	$("#voteaza ul li").mouseover(moveOnMouseOver).mouseout(moveOnMouseOut);
}
function getVoteResponse(a) {
	$.post("newsite/top/voteaza.php", {id_day: $("#contestContain .title").attr("name"), rating : a.attr("rel")},
	function(data)
	{
		if(data.status == "failed")a.append("<div class=\"voting\"><br /><img src=\"newsite/top/img/failed.png\"><br /><div>"+(data.error)+"<br /><a href=\"#\">close</a></div></div>");
		if(data.status == "succes")a.append("<div class=\"voting\"><br /><img src=\"newsite/top/img/succes.png\"><br /><div style=\"background-color:#84FF66;\">"+(data.error)+"<br /><a href=\"#\" style=\"color:#FFF;\">close</a></div></div>");
		$(".voting").css("margin-left","20px");
		$(".voting div").css("width", a.contents().outerWidth());		
		$(".voting div a").click(function(e){ e.preventDefault(); refreshAll();});
		a.contents().next().show(500);
	}, "json");
}
function selectedRating(avrege)
{
	$("#voteaza ul").find("li[rel!="+avrege+"]").animate({backgroundPosition:"0px 100%"},100).unbind("mouseover").unbind("mouseout").unbind("click");
}
function showRatings(){
$("#voteaza ul li").mouseover(moveOnMouseOver).mouseout(moveOnMouseOut).click(onMouseClick);
$.post("newsite/top/getRating.php", {id_entry: $("#contestContain .title").attr("name")},
	function(data)
	{
		if(data.votes != 0 ){
		if($(".total_votes").length > 0 )
		$(".total_votes").fadeOut(1000, function(){
		$(this).remove();
		$("#voteaza").after('<div class="total_votes"><p>'+data.votes+'</p><img src="newsite/top/img/kop.png" /></div>');
		$(this).fadeIn("slow");
			});
		else $("#voteaza").after('<div class="total_votes"><p>'+data.votes+'</p><img src="newsite/top/img/kop.png" /></div>');
		if(data.end == 'failed')
		{
			selectedRating(data.avrege);
		}
		} else $(".total_votes").fadeOut(1000, function(){$(this).remove();});
	}, 'json');
}
});

