// JavaScript Document
jQuery.noConflict();
jQuery(document).ready(function($) {	
	
	function decoloreLiens(){
		$(".parent a").each(function () {
			$(this).removeClass("aSelectionne");
		});
	}
	
	function recupereValueCSS(listingParent, attcss){
		var posTextPX = listingParent.css(attcss).indexOf("px");
		var valueCSS = parseInt(listingParent.css(attcss).substring(0,posTextPX));
		return valueCSS;
	}
	
	activePop = null;
	activeEnfantPop = null;
	
	
	
    $(".popable").mouseover(function() {
	
	
		var topY = recupereValueCSS( $(this), 'top' );
		var top = topY + 20;
		if($(this).next(".listing").css("display")=="none"){ $(this).next(".listing").css("top", top); }
		topY += 10;
		
		var leftX = recupereValueCSS( $(this), 'left' );
		var WidthX = recupereValueCSS( $(this), 'width' );
		leftX += WidthX + 10;
		$(this).next(".listing").css("left", leftX);
		
		// ouvre au bon endroit la fenêtre
		if($(this).next(".listing").css("display")=="none"){
			$(this).next(".listing").animate({opacity: "show", top: topY+"px"}, "slow");
			if ($(this).hasClass('blocNoirAccueil')) {
				$(this).removeClass('blocNoirAccueil').addClass('blocNoirAccueilhover');
			}
			//var listingSuivant = $(this).next(".listing");
			activePop = $(".popable").index(this);
		}
		
		// ferme les autres fenêtres parents
		$(".popable").each(function (i) {
			if ($(this).hasClass('blocNoirAccueilhover') && i != activePop) {
				var topY = recupereValueCSS( $(this).next(".listing"), 'top' );
				topY += 10;
				$(this).next(".listing").animate({opacity: "hide", top: topY+"px"}, "fast");
				$(this).removeClass('blocNoirAccueilhover').addClass('blocNoirAccueil');
				
				// listing enfants
				topY += 10;
				$(".enfant").animate({opacity: "hide", top: topY+"px"}, "fast");
				// décolore le lien hypertexte cliqué 
				decoloreLiens();
			}
		});
		
	});
	
	$(".listing").mouseleave(function(){
		// ferme le listing ouvert quand le quitte
		// sauf si listing parent et que l'on a ouvert un listing enfant
		if($(this).hasClass('parent') && activeEnfantPop != null){
		
		}else{
			var topY = recupereValueCSS( $(this), 'top' );
			topY += 10;
    		$(this).animate({opacity: "hide", top: topY+"px"}, "fast");
			$(this).prev(".popable").removeClass('blocNoirAccueilhover').addClass('blocNoirAccueil');
			
			// si listing enfant, on agit aussi sur le parent
			if($(this).hasClass('enfant')){
				//var topParentY = recupereValueCSS( $(this).prev(".parent"), 'top' );
				//topParentY += 10;
    			//$(this).prev(".parent").animate({opacity: "hide", top: topParentY+"px"}, "fast");
				//$(this).prev(".parent").prev(".popable").removeClass('blocNoirAccueilhover').addClass('blocNoirAccueil');
				
				// décolore les liens hypertextes cliqués 
				decoloreLiens();
				
				// on réinitilalise variable indiquant l'ouverture
				activeEnfantPop = null;
			}
		}
  	});

	 
	$(".listingBlocVignette").mouseleave(function(){
		$(this).animate({opacity: "hide"}, "fast");
	});
	
	
	
	$(".blocXXLBlancAccueil a").mouseover(function () {
		$(".blocXXLBlancAccueil a").each(function () {
			if ($(this).hasClass('normal')) {
				$(this).removeClass('normal').addClass('hoverClass');
			}
		});
	});
	
	$(".blocXXLBlancAccueil a").mouseleave(function () {
		$(".blocXXLBlancAccueil a").each(function () {
			if ($(this).hasClass('hoverClass')) {
				$(this).removeClass('hoverClass').addClass('normal');
			}
		});
	});
	
	
	$(document.window).click(function(){ 
		
		activePop = null;
		activeEnfantPop = null;
	
		$(".popable").each(function(){
			$(this).removeClass('blocNoirAccueilhover').addClass('blocNoirAccueil');
		});
		
		$(".listing").each(function(){
			var topY = recupereValueCSS( $(this), 'top' );
			topY += 10;
			$(this).animate({opacity: "hide", top: topY+"px"}, "fast");
		});
		
		$(".listingBlocVignette").each(function () {
			$(this).animate({opacity: "hide"}, "fast");
		});
		
		// décolore les liens hypertextes cliqués 
		decoloreLiens();
    });
	
	
	$(".parent a").click(function() {
		
		// décolore les liens hypertextes cliqués 
		decoloreLiens();
		// colore le lien hypertexte cliqué
		$(this).toggleClass("aSelectionne");
		
		// identifie le lien ouvrant
		activeEnfantPop = $(this).attr('name');
		
		// change les données de la fenêtre enfant et l'affiche
		$.ajax({
		   type: "POST",
		   url: "JQ-csq/JQ-listeInvites.php",
		   data: "cat="+activeEnfantPop,
		   success: function(msg){
			 	$('#invitesListing').html('<div class="top">&nbsp;</div><div class="content"><ul>'+msg+'</ul></div><div class="bottom">&nbsp;</div>');
			 
			 	// positionne et ouvre la fenêtre enfant
				var topY =  recupereValueCSS( $("#invitesRubListing"), 'top' );
				var top = topY + 20;
				if($("#invitesListing").css("display")=="none"){ $("#invitesListing").css("top", top); }
				topY += 10;
				
				var leftX =  recupereValueCSS( $("#invitesRubListing"), 'left' );
				var WidthX =  recupereValueCSS( $("#invitesRubListing"), 'width' );
				leftX += WidthX + 10;
				$("#invitesListing").css("left", leftX);
				
				if($("#invitesListing").css("display")=="none"){
					$("#invitesListing").animate({opacity: "show", top: topY+"px"}, "slow");
					//var listingSuivant = $(this).next(".listing");
				}
		   }
		 });
		
		
		
		
		// évite que le lien hypertexte soit lancé
		return false;
	});
	
	
});
