var imageLoading = '<div style="text-align: center;"><img src="images/interface/loadingAnimation.gif" id="loadingVote" alt="" /></div>';
var elementVoted;
$(document).ready(function(){

	//bouton de choix de vote
	$("form#choice_vote input[name='want']").change(
		function() {
			$("form#choice_vote").submit();
		}
	);

	//Petites stars pink
	var srcImagePink = "images/interface/stars_pink.gif";
	var cursor;
	$(".stars_pink img").css({background: "url("+srcImagePink+")", height: "22px", width: "22px", backgroundRepeat: "no-repeat", backgroundPosition: "right", cursor: "pointer"})
	.mousemove(function(e) {
		var imgHover = $(this);
		cursor = 2;
		$(this).prevAll("img").each(function() {
			$(this).css({backgroundPosition: "left"});
			cursor += 2;
		});
		$(this).nextAll("img").css({backgroundPosition: "right"});
		
		var decalageX = e.pageX - imgHover.offset().left;
		if(decalageX < 22/2) {
			imgHover.css({backgroundPosition: "center"});
			cursor--;
		}
		else imgHover.css({backgroundPosition: "left"});
	})
	.parent(".stars_pink").mouseout(function() {
		 $(this).children("img").css({backgroundPosition: "right"});
	 })
	.click(function() {
		elementVoted = $(this);
		var id_p = $(this).children("input[name='id_p']").val();
		$(this).fadeOut("slow", function() {
			$.post(URL_SCRIPT+"voter.php", { id: id_p, note: cursor, js: 1 }, function(data) {
				elementVoted.html(data.toString());
				elementVoted.show();
			});
		});
	 });

	//Photo à noter
	if($("#contenu_photo").length > 0) $("#contenu_photo").load(URL_SCRIPT+'ajax/photo_a_noter.php');
	setStars();
 });


function setStars() {
	$("img.scale_js").livequery('click', function(e) {
		var num_star = $(e.target).attr("num_star");
		var note = Math.round(num_star);
		var id_p = $("#ZoneNote input[name='id_p']").val();
		var fichier = $("#ZoneNote input[name='fichier']").val();
		var moyenne = $("#ZoneNote input[name='moyenne']").val();
		//var nbVotes = $("#ZoneNote input[name='nb_votes']").val();
		var reload = $("#ZoneNote input[name='reload']").val();
		
		$.post(URL_SCRIPT+"voter.php", { id: id_p, note: note, js: 1 }, function(data) {
			if($("#last_vote").length > 0) { // A améliorer
				//Informations
				var posLastSlashe =  $("#last_vote img").attr("src").lastIndexOf('/', 50);
				var dossier_mini = $("#last_vote img").attr("src").substring(0, posLastSlashe+1);
				var lastIdPhoto = $("#last_vote input[name='id_p']").val();

				//Image
				$("#last_vote img").attr("src", dossier_mini+fichier);
				//Moyenne
				//moyenne = Math.round(((moyenne*nbVotes+note)/(nbVotes)) *10)/10;
				moyenne = Math.round(moyenne*10)/10;
				$("#last_vote span").html(moyenne);
				//Lien
				$("#last_vote a").attr("href", $("#last_vote a").attr('href').replace(lastIdPhoto, id_p));
				$("#last_vote input[name='id_p']").val(id_p);
			}
			
			if(reload == 1) {
				$("#contenu_photo").fadeOut('slow',
					function() {
						$("#contenu_photo").load(URL_SCRIPT+'ajax/photo_a_noter.php',
							function() {
								$("#contenu_photo").fadeIn('slow');
							}
						);
					}
				);
				//$("#photo_a_noter").fadeIn('slow');
			}
			else $("#ZoneNote").html(data.toString());
		});
	});
 }

NbStar=	10;	//note max

function StarOver(Star) {
	for (i=0;i<=(Star*1);i++) {
		//document.getElementById('Star'+i).src=StarOverUrl;
		var top = 308-((i+1)*28);
		document.getElementById('Star'+i).style.backgroundPosition= '-28px -'+top+'px';
	}
}

function StarOut(Star) {
	for (i=0;i<=(Star*1);i++) {
		//document.getElementById('Star'+i).src=StarOutUrl;
		var top = 308-((i+1)*28);
		document.getElementById('Star'+i).style.backgroundPosition= '0px -'+top+'px';
	}
}

