$(document).ready(function(){
	var loginTriggered = false;
	var passwordTriggered = false;
	
	$(".external-link").click(function(event){
	  event.preventDefault();
	  window.open($(this).attr('href'));
	});

	$("#espaceClient input[name='login']").click(function(event){
		if(!loginTriggered){
			loginTriggered = true;
			$(this).val("");
		}
	});

	$("#espaceClient input[name='password']").click(function(event){
		if(!passwordTriggered){
			passwordTriggered = true;
			$(this).val("");
		}
	});

	$(".scrollTo").click(function(event){
		event.preventDefault();
		$.scrollTo($("#"+$(this).attr("rel")),1000);
	});
	/**
	 * Submenu
	 */


	$(".scrollLeft, .scrollRight").click(function(event){ event.preventDefault(); });

	/** Calcul de la longueur des produits **/
	var divWidth = 0;
	$("#submenu .items li").each(function(){
		divWidth = divWidth + $(this).width() + ($(this).outerWidth(true) - $(this).innerWidth());
	});
	$("#submenu .items").css("width",divWidth);

	if($("#submenu .items").width() > $("#submenu").width()){
		$(".scrollBarInterface").show();
		var initX = $(".scrollBar").position().left;
		$(".scrollBar").click(function(event){ event.preventDefault(); });
		$(".scrollBar").draggable({
			axis: "x",
			containment: $(".scrollTrack"),
			zIndex: 10,
			drag: function(event, ui){
				var iProgress = ui.position.left - initX;
				var iContainerWidth = $("#submenu .items").width()-$("#submenu").width();
				var iCoeff = (iContainerWidth / ($(".scrollTrack").width()-$(".scrollBar").width()));
				var iNewPos = iCoeff * iProgress;
				$("#submenu .items").css("left",-iNewPos);
			}
		});
		$(".scrollTrack").click(function(event){
			var iClickPos = event.pageX - $(".scrollTrack").offset().left - ($(".scrollBar").width()/2) + initX;

			if(iClickPos < $(".scrollBar").width()/2){
				iClickPos = initX;
			}

			if(iClickPos > $(".scrollTrack").width()-$(".scrollBar").width()){
				iClickPos = $(".scrollTrack").width() - $(".scrollBar").width() + initX;
			}

			var iContainerWidth = $("#submenu .items").width()-$("#submenu").width();
			var iCoeff = (iContainerWidth / ($(".scrollTrack").width()-($(".scrollBar").width())));
			var iNewPos = iCoeff * iClickPos - initX;

			$(".scrollBar").animate(
				{
					left: iClickPos
				},
				{
					duration: 1500,
					queue: false
				}
			)
			$("#submenu .items").animate(
				{
					left: -iNewPos
				},
				{
					duration: 1500,
					queue: false
				}
			)
		});
	}
	
	
	/**********************************/
	/** Infobulle sur les références **/
	/**********************************/
	var xOffset = 150; // position haut / bas
    var yOffset = -105; // position gauche / droite
	$("a.realisations")
	.mouseover(function(e){
		var title = $(this).find("+ span.invisible").html();
		$("body").append("<p class='tooltip'>"+ title +"</p>");
		$("p.tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");
		})
	.mousemove(function(e){
		$("p.tooltip")
		.css("top",(e.pageY - xOffset) + "px")
		.css("left",(e.pageX + yOffset) + "px")
		})
	.mouseout(function(){
			$("p.tooltip").remove();
		});
	
	/***********************************/
	/** Gestion de la page Références **/
	/***********************************/
	$("div.content_liste_ref").each(function(i){
		nbChild = $(this).find("a.realisations").length;
		// on affiche le nombre de référence pour chaque cétégorie
		$(this).find("div.first").append('<div class="floatLeft marginLeft">( '+nbChild+' )</div>'); 
		// s'il y a plus de 4 références pour une catégorie, on génère le lien "voir plus"...
		if(nbChild> 4) $(this).find("div.first").append('<a href="#" class="see_more_ref" rel="RefNb'+i+'" title="Voir toutes les références."></a>');
	});
	
	$("a.see_more_ref").click(function(event){
		event.preventDefault();
		var Link = $(this); // Objet JQuery = le lien sur lequel on a cliqué
		var ClassParent = Link.attr("rel"); // on recherche la classe du mask
		var HeightMask = $("div."+ClassParent+"").height(); // height du mask
		var HeightParent = $("div."+ClassParent+"").parent().height(); // Height du conteneur mask
		// si la div contenant les liens est ouverte, on la referme, sinon, on l'ouvre
		if(parseInt(HeightParent) >= parseInt(HeightMask))
		{
			$("div."+ClassParent+"").parent().animate({height:90},300,function(){
				Link
					.css("background","url('images/interface/see_more_ref.png') top no-repeat")
					.attr("title", "Voir toutes les références.");
			});
		}
		else
		{
			$("div."+ClassParent+"").parent().animate({height:parseInt(HeightMask)},300,function(){
				Link
					.css("background","url('images/interface/see_less_ref.png') top no-repeat")
					.attr("title","Refermer les références.");
			});
		}
	});
});
