$(function() {

    var $el, leftPos, newWidth,
        $mainNav 		= $("#titremenu");

    $mainNav.append("<li id='magicline'><\/li>");
    var $magicLine 		= $("#magicline");

    $magicLine
        .width($(".page_on").width())
		.height("8px")
        .css("left", $(".page_on a").position().left)
        .data("origLeft", $magicLine.position().left)
        .data("origWidth", $magicLine.width());

    $("#titremenu li a").hover(
		function() {
			$el 		= $(this);
			leftPos 	= $el.position().left;
			newWidth 	= $el.parent().width();
			$magicLine.stop().animate({
				left : 		leftPos,
				width : 	newWidth
			});
    	}, 
		function() {
			$magicLine.stop().animate({
				left : 		$magicLine.data("origLeft"),
				width : 	$magicLine.data("origWidth")
			});
		}
    );
	/////////////////////////////////////////////////////////////
	//***** gestion des trucs a effacer quand il faut ... *****//
	/////////////////////////////////////////////////////////////
	$("#tabs,#recherche,#zonessfam,#tabs ul li a,#nouveaute,#centrehaut,#textegauche,#zoneinfoservices,#zoneinfosociete, #zonequisommesnous,#zonecontact,#zonenews,#zonesitemap,#zonefaq,#zonementionslegales").click(
	 	function(){
			//suppression du sous-menu si y a ...
			if($('#sm_cadre').length > 0) 
			{
				$('#sm_cadre').fadeOut(500);
				// reinitialisation du choix dans le menu de gauche ...
				var adresse 		= $(location).attr('href').split('?');
				var page 			= adresse[0].substr((adresse[0].length - 14) , 14);
				if ((page == 'ssfamilles.php') && ($(this).attr('id').substr(0,7)!='famille')) 
				{
					var tmpFam 		= adresse[1].split('&');
					var fam 		= tmpFam[0].split('=');
					var idFam		= fam[1];
					$("li[id^='famille_']").children().remove();
					$("li[id^='famille_']").removeClass('choix');
					$("li[id^='famille_']").css('color', '#000000');
					$("li[id^='famille_"+idFam+"_ligne_']").addClass('choix');
					$("li[id^='famille_"+idFam+"_ligne_']").css('color', '#eb008b');
					$("li[id^='famille_"+idFam+"_ligne_']").prepend('<span>&nbsp;>> <\/span>');
				}
			}
			//suppression de la recherche si y a ...
			if($('#suggestions').length > 0)
			{
				$('#suggestions').fadeOut();
			}
			
		}
	);
	//////////////////////////////////////////////////////////////
	/*$("#tabs,#recherche,#zonessfam,#tabs ul li a,#nouveaute,#centrehaut,#textegauche,#zoneinfoservices,#zoneinfosociete, #zonequisommesnous,#zonecontact,#zonenews,#zonesitemap,#zonefaq,#zonementionslegales").hover(*/
																																																					  	$("#haut,#logo,#menuhaut,#zonebesoin,#recherche,#besoin,#textegauche,#bienvenue,#nouveaute,#decouvrez,#zonecarrousel,#footer").hover(
	 	function(){
			//suppression du sous-menu si y a ...
			if($('#sm_cadre').length > 0) 
			{
				$('#sm_cadre').fadeOut(500);
				$("li[id^='famille_']").children().remove();
				$("li[id^='famille_']").removeClass('choix');
				$("li[id^='famille_']").css('color', '#000000');
			}
			//remise du look de la famille selectionnee ...
			var adresse 		= $(location).attr('href').split('?');
			var page 			= adresse[0].substr((adresse[0].length - 14) , 14);
			if ((page == 'ssfamilles.php')) 
			{
				var tmpFam 		= adresse[1].split('&');
				var fam 		= tmpFam[0].split('=');
				var idFam		= fam[1];
				$("li[id^='famille_']").children().remove();
				$("li[id^='famille_']").removeClass('choix');
				$("li[id^='famille_']").css('color', '#000000');
				$("li[id^='famille_"+idFam+"_ligne_']").addClass('choix');
				$("li[id^='famille_"+idFam+"_ligne_']").css('color', '#eb008b');
				$("li[id^='famille_"+idFam+"_ligne_']").prepend('<span>&nbsp;>> <\/span>');
			}
		}
	);
	
	$("li[id^='famille_']").hover(
		function(){
			if ($(this).hasClass('choix') == false) 
			{
				$(this).addClass('choixHover');
				$(this).css('color', '#eb008b');
				$(this).prepend('<span>>> <\/span>');
			}
			//--le sous-menu s'affiche sur le hover ...
			var tmpFam 		= $(this).attr('id').split('_');
			var idFam 		= tmpFam[1];
			var idLigne 	= tmpFam[3];
			appartitionSousMenu(idFam,idLigne);
	 	},
		function(){
			if ($(this).hasClass('choix') == false && $('#sm_cadre').is('hidden')) 
			{
				$(this).removeClass('choixHover');
				$(this).css('color', '#000000');
				$(this).children().remove();
			}
	 	}
	 );
	 
	 $("li[id^='sfamille_']").hover(
		function(){
			$(this).css('font-weight', 'bold');
	 	},
		function(){
			$(this).css('font-weight', 'normal');
	 	}
	 );
	 
	 //////////// lien vers accueil sur le logo ////////////////
	 $("#logo > img").click(
	 	function() {
			window.location = "index.php";
		}
	 );

//-- le sous-menu s'affiche sur le hover ...
	function appartitionSousMenu(idFam,idLigne){
		$('#sm_cadre').hide();
		var elem 		= $("#famille_"+idFam+"_ligne_"+idLigne+"");
		$("li[id^='famille_']").children().remove();
		$("li[id^='famille_']").removeClass('choix');
		$("li[id^='famille_']").css('color', '#000000');
		elem.addClass('choix');
		elem.css('color', '#eb008b');
		elem.prepend('<span>&nbsp;>> <\/span>');
		var hauteur 	= -343 + ((idLigne - 1) * 26); // 351(taille div)
		$('#sm_cadre').css('margin-top',''+hauteur+'px');
		$.post("ajax/navigation.ajax.php",
			{ action: "sousmenu", fam_id: idFam },
			function (data) {
				$('#sm_cadre').html(data);
				$('#sm_cadre').fadeIn(500);
				$("li:[id^='sfamille_'] , li:[id^='documentation_']").hover(
					function(){
						$(this).css('font-weight', 'bold');
					},
					function(){
						$(this).css('font-weight', 'normal');
					}
				 );
				 $("li[id^='sfamille_']").click(
					function(){
						var ssfamId 	= $(this).attr('id');
						var tmp2 		= ssfamId.split('_');
						var sfamId 		= tmp2[1];
						var ssfamId 	= tmp2[3];
						$('#sm_cadre').fadeOut(500);
						if(ssfamId != "")
						{
							window.location="ssfamilles.php?fam="+idFam+"&sfam="+sfamId+"&ssfam="+ssfamId+"";
						}
					}
				 );
				 $("li[id^='documentation_']").click(
					function(){
						window.location="documentation.php?fam="+idFam+"";
					}
				 );
			}
		);
	
	}
	$('#cadrerecherche input').keyup(function() {
		if ($('#cadrerecherche input').val().length >= 3)
		{
			$('#suggestions').fadeIn();
			$('#suggestions').html("<p>Recherche en cours...<\/p>");
			$.post("ajax/recherche.ajax.php", {action: 'recherche', recherche: $('#cadrerecherche input').val()}, function(data) {
				$('#suggestions').html(data);
			});
		} else
		{
			$('#suggestions').fadeOut();
		}
	});
	
});
