$(document).ready(function() {
	//PHOTO, NOTE : ANIMATIONS
	//MOUSEOVER SUR UNE ETOILE
	$(".pollPicture").mouseover(function() {
		var note = parseInt($(this).attr("rel"));
		
		$(".pollPicture", $(this).parents(".vote:eq(0)")).removeClass("or");
		$(".pollPicture:lt(" + note + ")", $(this).parents(".vote:eq(0)")).addClass("or")
	
	//MOUSEOUT SUR UNE ETOILE
	})				 .mouseout(function() {
		var note = parseInt($("[name=note]:eq(0)", $(this).parents("form:eq(0)")).val());
		
		$(".pollPicture", $(this).parents(".vote:eq(0)")).removeClass("or")
		$(".pollPicture:lt(" + note + ")", $(this).parents(".vote:eq(0)")).addClass("or");
	});
	
	
	//PHOTO, NOTE : NOTER
	$(".pollPicture").click(function() {
		var idPhotoGalerie = $("[name=idPhotoGalerie]:eq(0)", $(this).parents("form:eq(0)")).val();
		var note = parseInt($(this).attr("rel"));
		
		$(".responseMessagePicture" + idPhotoGalerie).html('Vote en cours ...');
		
		$.ajax({
			type: "POST",
			mode: "abort",
			dataType: "json",
			url: "galerie.photo/poll/",
			data: "idPhotoGalerie=" + idPhotoGalerie + "&note=" + note + "&timestamp=" + new Date().getTime(),
			success: function(json) {
				if (json.success) {
					$(".responseMessagePicture" + idPhotoGalerie).html(json.message);

					//MODIFIER LES ETOILES
					$(".pollPicture.picture" + idPhotoGalerie).parents(".vote").each(function() {
						//OR
						$(".pollPicture.picture" + idPhotoGalerie + ":lt(" + json.note + ")", $(this)).addClass("or");
						//GRISE
						$(".pollPicture.picture" + idPhotoGalerie + ":gt(" + (json.note - 1) + ")", $(this)).removeClass("or");
					});
						
					//DESACTIVER LA POSSIBILITE DE NOTER, ET FIGER LES ETOILES
					$(".pollPicture.picture" + idPhotoGalerie).unbind("click")
															  .removeClass("pollPicture")
															  .addClass("polledPicture");
					
					//NOMBRE DE VOTES
					var notesCountString = "(" + json.notesCount + " vote" + (json.notesCount > 1 ? "s" : "")  + ")";
					$(".nbVotes.picture" + idPhotoGalerie + " > strong").html(notesCountString);
					
				}else {
					$(".responseMessagePicture" + idPhotoGalerie).html(json.message);
					
					//DESACTIVER LA POSSIBILITE DE NOTER
					$(".pollPicture.picture" + idPhotoGalerie).unbind("click");
				}
			}
		});
	});
	
	
	//PHOTO, NOTE : NOTEE
	$(".polledPicture").click(function() {
		var idPhotoGalerie = $("[name=idPhotoGalerie]:eq(0)", $(this).parents("form:eq(0)")).val();
		$(".responseMessagePicture" + idPhotoGalerie).html('Vous avez déjà noté cette photo.');
	});
	
	
	//THICKBOX : CHARGEMENT D'UNE PHOTO LORSQU'ON PASSE LA SOURIS AU DESSUS DU LIEN
	$('.thickboxPicture').click(function() {
		//CHARGEMENT THICKBOX
		tb_show();
		
		var triggerThickbox = $(this);
		var idP = triggerThickbox.prev().val();
		var rel = $(this).attr('rel');
		
		//CALCUL DES DIMENSIONS DU DIV A CHARGER APRES CHARGEMENT DE LA GRANDE VIGNETTE
		$('.boxPhoto' + idP + '.' + rel + ' img.picture').load(function() {
			//SI NON CALCULE
			if (!triggerThickbox.hasClass('thickboxLinkCalculated')) {
				divBoxPicture = $('.boxPhoto' + idP + '.' + rel);
				var width = divBoxPicture.innerWidth();
				var height = divBoxPicture.innerHeight() + 5;
				
				//MODIFIER LE LIEN THICKBOX
				var href = triggerThickbox.attr('href');
				href += '&width=' + width + '&height=' + height;
				triggerThickbox.attr('href', href)
							   .addClass('thickboxLinkCalculated');
				
				//DECLENCHER LA THICKBOX
				triggerThickbox.unbind('click');
				tb_init(triggerThickbox[0]);
				
				triggerThickbox.click();
			}
		});		
		
		//CHARGEMENT DE LA GRANDE VIGNETTE
		$('.boxPhoto' + idP + '.' + rel + ' img.picture').attr('src', $('.boxPhoto' + idP + '.' + rel + ' input.picture').val());
		
		return false;
	});
	
	
	//THICKBOX : PAGINATION PERSONNALISEE POUR LES PHOTOS
	$('.TB_prev > a, .TB_next > a', '#TB_ajaxContent').live('click', function() {
		//FERMETURE DE LA THICKBOX
		tb_remove();
		
		var idPhotoGalerie = $(this).attr('rel');
		var direction = $(this).parent().hasClass('TB_prev') ? 'prev' : 'next';		
		var rel = $(this).hasClass('galerie-photo-top') ? 'galerie-photo-top' : 'galerie-photo-bottom'; 
		var currentLink = $("a[rel=" + rel + "]").prevAll("input[type=hidden][value=" + idPhotoGalerie + "]").next();
		var targetedLink = null;
		
		//DERTERMINER LA PHOTO A OUVRIR
		$("a[rel=" + rel + "]").each(function(currentIndex, link) {
			if (currentLink.get(0) === link) {
				//PHOTO PRECEDENTE
				if (direction === 'prev') {
					//PREMIERE PHOTO, ALLER A LA DERNIERE
					if (currentIndex === 0) {
						targetedLink = $("a[rel=" + rel + "]:last");
					}else {
						targetedLink = $("a[rel=" + rel + "]:eq(" + (currentIndex - 1) + ")");
					}
				//PHOTO SUIVANTE
				}else {
					//DERNIERE PHOTO, ALLER A LA PREMIERE
					if ((currentIndex + 1) === $("a[rel=" + rel + "]").length) {
						targetedLink = $("a[rel=" + rel + "]:first");
					}else {
						targetedLink = $("a[rel=" + rel + "]:eq(" + (currentIndex + 1) + ")");
					}
				}
				
				//OUVRIR LA PHOTO (AVEC UN DELAI, SINON BUG THICKBOX)
				window.setTimeout(function() { 
					targetedLink.click();
					}, 800);
				
				return false;
			}
		});

		return false;
	});	
});


//PHOTO THICKBOX POUR FLASH
function openDiv(idBox){
	$('#lienBoxPhotoTop' + idBox).click();
}

//THICKBOX : AFFICHER UNE PHOTO AU CHARGEMENT DE LA PAGE
function openPictureOnLoad(idPhotoGalerie) {
	var triggerElement = $(".thickboxPicture").prevAll("input[type=hidden][value=" +  idPhotoGalerie + "]").next().eq(0);
	
	if (triggerElement.length === 1) {
		triggerElement.click();
	}
}
//PHOTO THICKBOX POUR FLASH
function openTheme(url){
	document.location.href= url;
}

