$(document).ready(function(){
					   
	// MegaDropdown
	MDs = "fast";
	MDd = 50; 	
	MDde = 5000;
	MDt = new Object();
	
	$("#navMain").append('<div class="sub"></div>');
	
	var MDitem = $("#navMain>ul>li>a");
	MDitem.live("mouseover", function() {
		var item = $(this);
		$.doTimeout( 'dd', 400, function(){
			window.clearTimeout(MDt);
			$("#navMain .sub").empty().html( item.next("div").html() );
			$("#navMain>ul>li>a").removeClass("hover"); $(this).addClass("hover");
			!$("#navMain .sub").is(":visible") ? $("#navMain .sub").slideDown(MDs) : null;
		});
	});
	
	$("#navMain>ul>li>a").live("mouseout", function() {
		$.doTimeout( 'dd' );
	});
	
	$(".sub li a").live("mouseover", function(){$(".sub li a").removeClass("hover");$(this).addClass("hover");});
	
	// Listener
	$("html").live("mouseover", function(o) {
		o = o.target;
		( $("#navMain .sub").is(":visible") && !$(o).parents().is("#navMain") ) 
			? MDt = window.setTimeout('$("#navMain .sub").slideUp("'+MDs+'", function() {$("#navMain>ul>li>a").removeClass("hover");})', MDd)
			: window.clearTimeout(MDt);
		( !$(o).parents().is("ul.menu") ) ? $("ul.menu li").removeClass("hover") : "";
		( !$(o).parents().is(".sub li") ) ? $(".sub li a").removeClass("hover") : "";
	});
	
	// Radio-bouton checked par defaut
		checkRadioChecked();
	
	
	// Menu
	MNs = "fast";
	$("ul.menu ul").hide().before('<button type="button"><span>Afficher/masquer le sous-menu</span></button>');
	$("ul.menu li").live("mouseover", function(){
		$("ul.menu li").removeClass("hover");o=$(this).parent().is('ul.menu')?$(this):$(this).parents("li");o.addClass("hover");
	});
	$("ul.menu>li>a,ul.menu>li>span").live("click",function(e,a) {
		u = $(this).nextAll("ul");$("ul.menu>li").removeClass("current"); 
		switch(a) {
			case 0: $(this).parent().addClass("current"); u.show(); break;
			case 1: if(!u.is(":visible")) {$("ul.menu ul:visible").slideUp(MNs);$(this).parent().addClass("current");}
				else {$(this).parent().removeClass("current");}
				u.slideToggle(MNs); break;
			default: $(this).parent().addClass("current");
				if(!u.is(":visible")) {$("ul.menu ul:visible").slideUp(MNs);u.slideDown(MNs);}break;
		}
	});
	$("ul.menu li.active").each(function() {o = $(this).parent().is('ul.menu') ? $(this).children("a,span") : $(this).parent().prevAll("a,span");o.trigger("click", 0);});
	$("ul.menu>li>button").live("click", function(){$(this).prevAll("a,span").trigger("click", 1);});
	
	// FooterFix
	$(".footerFix-bloc .categories li a").mouseover(function() {
		$(".footerFix-bloc .close a").trigger("click", true);
		s = $(this).parents(".footerFix-bloc");
		s.css("background-image", "url()");
		s.find(".footerFix-form div").removeClass("open");
		s.find(".footerFix-form div:nth-child("+ ($(this).parent().prevAll("li").length + 2) +")").addClass("open");
		s.find(".footerFix-form").show();
		return false;
	});
	
	// lastChild class added 
	if($('#footerFix .inside .footerFix-bloc').length){
		$('#footerFix .inside .footerFix-bloc:last-child').addClass('lastChild');
	}
	
	
	
	
	$(".footerFix-bloc .formZone .formZoneIcon").mouseover(function() {
		s = $(this).parents(".footerFix-bloc");
		s.find(".categories li:nth-child("+ ($(this).parent().prevAll(".formZone").length + 1) +") a").trigger("mouseover");
		return false;
	});
	$(".footerFix-bloc .close a").click(function(e, a) {
		$(".footerFix-form:visible").hide();
		$(".footerFix-bloc").removeAttr("style");
		if(a != true) {FtFrm = false;}
		return false;
	});
	$(".footerFix-bloc form.formIn").submit(function() {
		flag=true;
		$(".required").removeClass("required");
		c1 = $(this).find("input[type='text'][value=''], textarea[value='']").parent("li").addClass("required");
		($(this).find("input[name='groupe1']:checked").length > 0) ? null : $(this).find("input[name='groupe1']").parent("li").parent("ul").addClass("required");
		if($(".required",$(this)).length){
			flag=false;
		}
			
		
		$("input, textarea",$(this)).each(function(){
			if($(this).val() && $(this).val() !='on') flag= true;
			
		});
		if(flag){
			o = $(this);
			/*o.parents(".footerFix-bloc").find(".confirmation").show(0, function() {
				if( !$(this).parents(".footerFix-bloc").hasClass("blocPourquoi") ) {
					$(this).parents(".footerFix-bloc:not('.blocPourquoi')").find(":text, :password, textarea").attr("value", "");
					$(this).parents(".footerFix-bloc:not('.blocPourquoi')").find(":radio, :checkbox").attr("checked", "");
					window.setTimeout('$(".footerFix-form:visible .close a").trigger("click", true); $(".footerFix-bloc:not(\'.blocPourquoi\') .confirmation").hide(); $(".footerFix-bloc:not(\'.blocPourquoi\')  form.formIn").show();', 2000);
				}
			});*/
			return true;
		}else return false;
	});
	
	$(".footerFixWide-bloc form.formIn").submit(function() {
 		$(this).find(".required").removeClass("required");
		c1 = $(this).find("input[type='text'][value=''], textarea[value='']").parent("li").addClass("required");
		($(this).find("input[name='groupe1']:checked").length > 0) ? null : $(this).find("input[name='groupe1']").parent("li").parent("ul").addClass("required");
		flag=false;
		$("input, textarea,select",$(this)).each(function(){
			if($(this).val() && $(this).val() !='on') flag= true;			
		});
		if(flag){
			o = $(this);
			/*o.parents(".footerFixWide-bloc").find(".confirmation").show(0, function() {
				if( !$(this).parents(".footerFixWide-bloc").hasClass("blocPourquoi") ) {
					$(this).parents(".footerFixWide-bloc:not('.blocPourquoi')").find(":text, :password, textarea").attr("value", "");
					$(this).parents(".footerFixWide-bloc:not('.blocPourquoi')").find(":radio, :checkbox").attr("checked", "");
					window.setTimeout('$(".footerFix-form:visible .close a").trigger("click", true); $(".footerFixWide-bloc:not(\'.blocPourquoi\') .confirmation").hide(); $(".footerFixWide-bloc:not(\'.blocPourquoi\')  form.formIn").show();', 2000);
				}
			});*/
			return true;
		}else return false;
	});	
	
	
	FtFrm = false;
	$(".footerFix-bloc form input, .footerFix-bloc form select, .footerFix-bloc form textarea,.footerFixWide-bloc form input, .footerFixWide-bloc form select, .footerFixWide-bloc form textarea").focus(function() {
		FtFrm = true;
	})

	$("html").live("mouseover click", function(o) {
		if( (o.type == "click") && ( $(o.target).parents(".formZone").length == 0) ) {FtFrm = false;}
		o = o.target;
		if( !$(o).parents(".footerFix-form").is(":visible") && (FtFrm == false) ) {
			$(".footerFix-form:visible").hide(); 
			$(".footerFix-bloc").removeAttr("style");
		}
	});

	
	// FooterFixWide
	$(".footerFixWide-bloc").mouseover(function() {
			$('.footerFixWide-content').hide();// Added Line
			$(this).find('.footerFixWide-content').show();
			$(this).addClass('zIndex10');// Added Line
	});
	
	$(".footerFixWide-bloc").mouseleave(function() {
			$(this).find('.footerFixWide-content').hide();
			$(this).removeClass('zIndex10');

	});
	
	var stay = false;
	$(".footerFixWide-content select").focus(function() {
		stay = true;
	});
	
 
	
	$(".footerFixWide-bloc input").focus(function() {
		$(this).parent('label').hide();
	});
	$(".footerFixWide-content select").blur(function() {
		stay = false;
	});
	
	$(".footerFixWide-content select").change(function() {
		$(this).blur();
		});
	
	$(".footerFixWide-content").live("mouseleave", function() {
		if (stay == false) {
		$(this).hide();
		$(this).parents('.footerFixWide-bloc').removeClass('zIndex10');// Added Line
		}
	});
	
	$(".footerFixWide-content .close").click(function() {
		$(this).parent().hide();
	});
	
	
	// form elements
	skinCheckbox();
	skinRadio('loc');
	skinRadio('station');
	skinRadio('open');
	skinRadio('products');
	skinInputFile();
	
	// select
	if ($('.select-cp').length) $('.select-cp').selectbox();
	if ($('.select-lub').length) $('.select-lub').selectbox();
	if ($('.select-visco').length) $('.select-visco').selectbox();
	if ($('.select-moteur').length) $('.select-moteur').selectbox();
	if ($('.select-lang').length) $('.select-lang').selectbox();
	
	$('#sidebar_left').height($('#container').height());
	
	
});

// LabelFade 
(function($){$.InFieldLabels=function(b,c,d){var f=this;f.$label=$(b);f.label=b;f.$field=$(c);f.field=c;
f.$label.data("InFieldLabels",f);
f.showing=true;f.init=function(){f.options=$.extend({},$.InFieldLabels.defaultOptions,d);if(f.$field.val()!=""){f.$label.hide();f.showing=false};
f.$field.focus(function(){f.fadeOnFocus()}).blur(function(){f.checkForEmpty(true)}).bind('keydown.infieldlabel',function(e){f.hideOnChange(e)}).change(function(e){f.checkForEmpty()}).bind('onPropertyChange',function(){f.checkForEmpty()})};f.fadeOnFocus=function(){if(f.showing){f.setOpacity(f.options.fadeOpacity)}};f.setOpacity=function(a){f.$label.stop().animate({opacity:a},f.options.fadeDuration);
f.showing=(a>0.0)};f.checkForEmpty=function(a){if(f.$field.val()==""){f.prepForShow();f.setOpacity(a?1.0:f.options.fadeOpacity)}else{f.setOpacity(0.0)}};f.prepForShow=function(e){if(!f.showing){f.$label.css({opacity:0.0}).show();f.$field.bind('keydown.infieldlabel',function(e){f.hideOnChange(e)})}};f.hideOnChange=function(e){if((e.keyCode==16)||(e.keyCode==9))return;if(f.showing){f.$label.hide();f.showing=false};f.$field.unbind('keydown.infieldlabel')};f.init()};$.InFieldLabels.defaultOptions={fadeOpacity:0.5,fadeDuration:300};$.fn.inFieldLabels=function(c){return this.each(function(){var a=$(this).attr('for');if(!a)return;var b=$("input#"+a+"[type='text'],"+"input#"+a+"[type='password'],"+"textarea#"+a);if(b.length==0)return;(new $.InFieldLabels(this,b[0],c))})}})(jQuery);

// LabelInit
jQuery(document).ready(function() {
	jQuery("#search label , #footerFix label, #footerFixWide-b2c label,.footerFix-bloc label").inFieldLabels({fadeOpacity:0});
});

$('#recherche').click(function() {
	$(this).val('');
});



//login form
$('#header .wrapLogOut').unbind('mouseenter').bind('mouseenter', function(e){
	$('#logOut').show();
});



var timer1 = 0;
$("html").live('mouseover',function(e){	
	if($(e.target).parents(".wrapLogOut").length == 0){	
		clearTimeout(timer1);
		timer1 = setTimeout(function(){
			$('#logOut').hide();
		},800);
	}else{
	clearTimeout(timer1);
		$('#logOut').show();
	}
});


/*
.live('mouseleave', function(e){
	console.log(e.target);
	timer1 = setTimeout(function(){
		$('#logOut').hide();
	},300);
});*/


// alternative flash home b2b
$(".mosaicWrapper .item").live("mouseenter", function(o) {
	$('.item-content').hide();
	var itemLeft = $(this).position().left;
	var itemTop = $(this).position().top;
	var itemContentLeft = itemLeft - 25;
	var itemContentTop = itemTop - 10;
	var itemContent = $(this).next('.item-content');
	
	itemContent.css('left',itemContentLeft+'px');
	itemContent.css('top',itemContentTop+'px');		
	itemContent.show();
});

$(".mosaicWrapper .item-content").live("mouseleave", function(o) {
	$(this).hide();
	});

$(".mosaicWrapper .item-content2").live("mouseleave", function(o) {
	$(this).hide();
	});

$(".mosaicWrapper .close").click(function() {
	$(this).parent().hide();
	});

$(".mosaicWrapper .item-content a.esp-client").click(function() {
	
	var itemContent2 = $(this).parent().next('.item-content2');
	itemContent2.css('left',$(this).parent().position().left+'px');
	itemContent2.css('top',$(this).parent().position().top+'px');
	$(this).parent().hide();	
	itemContent2.show();
	});


function skinCheckbox() {
	if ($('.checkbox').length) {
				//click on checkbox
        $('.picto_check').click(function() {
            var input = $('.checkbox', $(this).parents('div')); 
            if ($(this).hasClass('checked')) {
                input.attr('checked', false);
                $(this).removeClass('checked');
            } else {
                input.attr('checked', true);
                $(this).addClass('checked');
            }
        });
        //click on label
        $('.label-check').click(function(e) {
        		e.preventDefault();
            var input = $('.checkbox', $(this).parents('div'));
            var picto = $('.mytotal-picto-check', $(this).parents('div'));
            if (picto.hasClass('checked')) {
                input.attr('checked', false);
                picto.removeClass('checked');
            } else {
                input.attr('checked', true);
                picto.addClass('checked');
            }
        });	
   }
}
function checkRadioChecked(){
	if($('.radiochecked').length){
		var input=$('.radiochecked').parent().parent().find('.radio'); 
		input.attr('checked', true);
	}

}
function skinRadio(radioClass) {
	if ($('.radio').length) {
    		// click on radio
        $('.picto_radio').click(function() {
            var input = $(this).parent().parent().find('.radio'); 
            if (input.hasClass(radioClass)) {
	            $('.'+radioClass).next().find('.picto_radio').each(function(){
	            	$(this).removeClass('radiochecked');
	            	});
	            $(this).addClass('radiochecked');
	            input.attr('checked', true);
	            }
        	});
        	

        // click on label 
        $('.label_radio').click(function() {
            var input = $(this).parent().parent().find('.radio'); 
            var picto = $('.picto_radio', $(this).parents('span'));
            if (input.hasClass(radioClass)) {
	            $('.'+radioClass).next().find('.picto_radio').each(function(){
	            	$(this).removeClass('radiochecked');
	            	});
	            picto.addClass('radiochecked');
				input.attr('checked', true);
	        		}
        		});
      }
}

function skinInputFile() {
		if ($('.file').length) {
			$('.file').change(function() {
				//alert($('.file').val());
				$('.file-content',$('.file').parents('div')).val($('.file').val());
			});
		}
	}


function setCookie(name,v,h) {
  var expDate = new Date()
  expDate.setTime(expDate.getTime() + (h * 3600 * 1000))
  document.cookie = name + "=" + escape(v) + ";path=/;expires=" + expDate.toGMTString()
  }

function getCookie(name) {
  deb = document.cookie.indexOf(name + "=")
  if (deb >= 0) {
      deb += name.length + 1
      fin = document.cookie.indexOf(";",deb)
      if (fin < 0) fin = document.cookie.length
      return unescape(document.cookie.substring(deb,fin))
      }
  return "";
  }


////Langs
//$(function(){
//  $('#langsListToggle').click( function() {
//    $('#langsList').slideToggle("fast");	
//    $(this).toggleClass( 'open' );    
//    return false;
//  });	
//});
// 
//$(document).ready(function(){
//
//var getListWidth = function(){
//  var maxWidth = 0, width, $langList = $( '#langsList' );
//  $langList.show();
//  $langList.find( 'li' ).each( function( i, li ){
//  var $li = $( li );
//    width = $li.width(); 
//    if ( width > maxWidth ){
//      maxWidth = width;
//    }
//  });
//
//  $langList.hide();
//  return maxWidth;
//};
//
//$( '.langs .btnLangs' ).width( getListWidth() );
//
//});
