(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

//Ajout d'un filtre sur la page pour les popup
function addModale() {
	if ( $('.modale').size() == 0 ) {
		$('body').prepend("<div class='modale'></div>");
	}
	
	hauteur_doc = $(document).height();
	$('.modale').css("height", hauteur_doc );
	$("body").scrollTop(0);	
}
//utilisé dans faq.asp pour les listes de sélection
function faq() {
	$('.faq_module .faq_reponse div div a, .autre_question a').click(function(){
		$('.form_contact').not(':animated').slideToggle();
		return false;
	});
	$('.faq_module .faq_question p.autre_question a, .autre_question a').click(function(){
		$('.form_contact').not(':animated').slideToggle();
		return false;
	});
	
	$('.faq_question .category_faq').change(function(){
		 //lors d'1 changement dans la 1 ere liste de selection
		 //la 2 eme liste de selection associée doit s'afficher et les question de la 1ère rubrique doivent s'afficher
		 
		var id_category = $('.category_faq').val();					
		var text_category = $('.category_faq option[value='+id_category+']').text();
		$('.form_contact input[type=hidden]').val( text_category );
		
		$('.theme_faq').hide();		
		$('.theme_faq_'+id_category).show(function(){

		var id_category = $('.category_faq').val();
		var id_theme = $(this).val();

		var text_theme = $(this).find('option[value='+id_theme+']').text();
		var text_category = $('.category_faq option[value='+id_category+']').text();
		$('.form_contact input[type=hidden]').val( text_category +' - '+ text_theme );
		
		jQuery.get( "/ajax/get_faq_question.asp", { rubrique_id : id_theme , id_category : id_category }, function(data){$(".faq_question p.questions").html(data)} );
	});
		
		
	});
	
	$('.theme_faq').change(function(){

		var id_category = $('.category_faq').val();
		var id_theme = $(this).val();
		
		var text_theme = $(this).find('option[value='+id_theme+']').text();
		var text_category = $('.category_faq option[value='+id_category+']').text();
		$('.form_contact input[type=hidden]').val( text_category +' - '+ text_theme );
		
		jQuery.get( "/ajax/get_faq_question.asp", { rubrique_id : id_theme, id_category : id_category  }, function(data){$(".faq_question p.questions").html(data)} );
	});
}
function rolloverImage() {
	$('img.hover').unbind();
	$('.hover').mouseover(function() {
		var source = $(this).attr("src").length;
		var ext = $(this).attr("src").substring(source - 4, source);
        var newsource = $(this).attr("src").substring(0, source - 4) + "_on" + ext;
        $(this).attr("src", newsource);
	});
	
	$('.hover').mouseout(function() {
		var source = $(this).attr("src").length;
		var ext = $(this).attr("src").substring(source - 4, source);
		var newsource = $(this).attr("src").substring(0, source - 7) + ext;
		if ( $(this).attr("src").substring( source - 7, source - 4) == "_on" ) {
		  $(this).attr("src", newsource);
		}
	});
}

function navigation() {
	
	$('.navigation > ul > li').not('li.nav_left,li.nav_right').each(function(){
		$(this).data('class',$(this).attr('class'));
	});

	$('.navigation > ul > li').not('li.nav_left,li.nav_right').mouseenter(function(){
		$(this).addClass('active').addClass('active_'+$(this).data('class'));
		$(this).find('.sous_menu').show();	
	});
	$('.navigation > ul > li').mouseleave(function(){
		$(this).removeClass('active').removeClass('active_'+$(this).data('class'));
		$(this).find('.sous_menu').hide();
	});
	
	$('a[rel=guide_taille]').click(function(){
		affichepop_tailles(returnLang());
		return false;
	});
	$('a[rel=conseil_entretien]').click(function(){
		affichepop_entretien(returnLang());
		return false;
	});
	
	jQuery.preLoadImages("/img/bg_menu_61.png", "/img/bg_menu_10.png", "/img/bg_menu_11.png", "/img/bg_menu_12.png", "/img/bg_menu_13.png");
	
	// Désactivation rollover image pour le rétablir avec d'autres déclencheur
	/*$('.catalogue li img.hover').unbind();
	$('.catalogue li').not('.sous_menu li').unbind();
	$('.catalogue li').not('.sous_menu2 li').unbind();
	
	$('.selection li img.hover').mouseover(function(){
		$(this).css('left','-1px')
	});
	$('.selection li img.hover').mouseout(function(){
		$(this).css('left','0')
	});
	
	$('.catalogue li').not('.sous_menu li').mouseenter(function() {
		// Affichage du sous-menu
		$(this).find('.sous_menu').show();
		$(this).find('.sous_menu2').show();
		// Rollover des image avec un autre déclencheur
			if ( $(this).find('.hover').size() > 0 ) {
			var source = $(this).find('.hover').attr("src").length;
			var ext = $(this).find('.hover').attr("src").substring(source - 4, source);
			var newsource = $(this).find('.hover').attr("src").substring(0, source - 4) + "_on" + ext;
			$(this).find('.hover').attr("src", newsource);
			$(this).find('.hover').css('left','-1px');

		}
		
	});	
	$('.catalogue li').not('.sous_menu li').mouseleave(function() {
		$(this).find('.sous_menu').hide();
		
		if ( $(this).find('.hover').size() > 0 ) {
			var source = $(this).find('.hover').attr("src").length;
			var ext = $(this).find('.hover').attr("src").substring(source - 4, source);
			var newsource = $(this).find('.hover').attr("src").substring(0, source - 7) + ext;
			if ( $(this).find('.hover').attr("src").substring( source - 7, source - 4) == "_on" ) {
			  $(this).find('.hover').attr("src", newsource);
			  $(this).find('.hover').css('left','0')
			}
		}
	});
	$('.catalogue li').not('.sous_menu2 li').mouseleave(function() {
		$(this).find('.sous_menu2').hide();
		
		if ( $(this).find('.hover').size() > 0 ) {
			var source = $(this).find('.hover').attr("src").length;
			var ext = $(this).find('.hover').attr("src").substring(source - 4, source);
			var newsource = $(this).find('.hover').attr("src").substring(0, source - 7) + ext;
			if ( $(this).find('.hover').attr("src").substring( source - 7, source - 4) == "_on" ) {
			  $(this).find('.hover').attr("src", newsource);
			  $(this).find('.hover').css('left','0')
			}
		}
	});	*/
}

function stock_in_list(){
	$('.item_liste, .focus').unbind();
	$(document).unbind();
	
	pageX = 0;
	pageY = 0;
	$(document).bind('mousemove',function(e){ 
		pageX = e.pageX;
		pageY = e.pageY;
		if ( $('.lihover').size() == 0 ) {
			$('.taille_en_stock').remove();
		};
	});
	
	$('.item_liste, .focus').bind('mousemove',function(e){ 
		itemX = $(this).offset().left;
		itemY = $(this).offset().top;

		if( $('.taille_en_stock').size() > 0 ) {
			stock.css('left',pageX-itemX-25)
			stock.css('top',pageY-itemY-30-$('.taille_en_stock').outerHeight());
		}
	});
	
	$('.focus, .item_liste').each(function(index){
		$(this).css('z-index', (Math.floor(((index+1)*2)/10))+''+($('.focus, .item_liste').size()-index) );
	});
	
	$('.focus, .item_liste').mouseenter(function(){
		
		//$(this).find('img:first').attr('src', $(this).find('img:first').attr('src').replace('A_lpdt','C_pdt') );
		$(this).addClass('lihover');
		$('.taille_en_stock').remove();
		
		if( $(this).find('input.stock_produit').size() > 0 ) {
			hover_pdt = $(this);
			$.get('/ajax/get_taille_dispo.asp', {ID: $(this).find("input.gamme_id").val(), SSGID: $(this).find("input.ss_gamme_id").val()},  function(data) {
				taille_en_stock = data;

				if (hover_pdt.hasClass("lihover")) {
					if (taille_en_stock != ""){
						taille_en_stock = taille_en_stock.split('|');
					
						liste_taille_en_stock = getTraduction('taille_dispo')+'<br>';
					
						for(i=0;i<taille_en_stock.length;i++) {
							liste_taille_en_stock += '<span>'+taille_en_stock[i]+'</span>';
						}
					} else {
						taille_en_stock = "<strong>"+getTraduction('aucune_taille_dispo')+"</strong>";
						liste_taille_en_stock = getTraduction('taille_dispo')+'<br>'+taille_en_stock;
					}
					
					stock = $('<div>').addClass('taille_en_stock').html('<div class="top_stock"></div>'+liste_taille_en_stock+'<div class="btm_stock"></div>');
					stock.appendTo(hover_pdt);
					stock.css('left',pageX-itemX-25)
					stock.css('top',pageY-itemY-30-$('.taille_en_stock').outerHeight());
					$('.taille_en_stock').show();
				}
			});
		}
	});
	
	$('.focus, .item_liste').mouseleave(function(){
		$('.taille_en_stock').remove();
		$(this).removeClass('lihover');
		//$(this).find('img:first').attr('src', $(this).find('img:first').attr('src').replace('C_pdt','A_lpdt') );
	});	
}

function global_link() {
	$('.focus a, .item_liste a').click(function(e){
		e.stopPropagation();
	});
	$('.focus, .item_liste').click(function(e){
		var lien_produit = $(this).find('a').attr('href');
		document.location.href = lien_produit;
	});
}



