$(function(){
	
	//$('.round14').corner("14px");
	$(".savoir_plus_event").click(function(){ $(".news_art_event").slideToggle("slow"); });
	
	function showCart() {
	 var myCart = $(this);
	 myCart.children("#expand_cart").slideDown();
	}
	
	function hideCart() { 
	 var myCart = $(this);
	 myCart.children("#expand_cart").slideUp();
	}
	
   $("#cart_block").hoverIntent({
     sensitivity: 1,
     interval: 50,
     over: showCart,
     timeout: 300,
     out: hideCart
   });
 
	$('.carouselAuto').jcarousel({
		visible: 1,
		scroll: 1,
		auto: 3,
		wrap: 'last'
	});
	
	$('.biographie').expander({
	  	slicePoint: 720, 
	  	widow: 2,
	  	expandEffect: 'fadeIn', 
	  	userCollapseText: '[^]'
	});
	
	//////////////////////////////////////////////////////////////////
	///////////////////////   MENUS - SS-MENUS  //////////////////////
	//////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////
	
	var id; //id du menu
	var name; //name du parent des a
	var cptArtistes=0; //pour limite des box .artistes
	var width; //pour limite des box .artistes
	var width2;
	var cptScrollbox = 0; // Pour les lmites des scrollbox
	var prent;
	var widthTab; //Largeur des onglet du menu niveau 1
	var visible = 0;
	
		if(typeof(pageInterne) !='undefined'){ 
			alert(pageInterne);
			if (pageInterne) {
				$('#ss_menu').css({
					opacity : 100
				});	
			}
		}
	
	
	$('#menu').mouseover(function(){
		$('#ss_menu').show();
		$('#ss_menu').css({
			opacity : 100
		});
	});
	$('#menu').mouseout(function(){
		if(typeof(pageInterne) !='undefined'){ 
			if (!pageInterne) $('#ss_menu').hide();
		}
		
	});
	
	
	$('.ss_menu, #ss_menu').hide();
	//Détection tab
	$('#menu .niveau1 li').mouseover(function(){
		id = $(this).attr('id').substr(3,2);
		$('.ss_menu').hide();
		$('#ss_menu #ss_menu'+id).show();
		//Définir la taille de la box des artistes
		width = $('#ss_menu #ss_menu'+id+ ' .box2Artistes > ul').size() * 160;
		width2 = $('#ss_menu #ss_menu'+id+ ' .box2Artistes2 > ul').size() * 160;
		
		if (width/160 <= 2) {
			$('#ss_menu #ss_menu'+id+ ' .box2Artistes > ul').css('border-color','#B0B0B0');
			$('.fleche-g, .fleche-d').hide();
		}
		else { 
			$('#ss_menu #ss_menu'+id+ ' .box2Artistes > ul').css('border-color','#CCC');
			$('.fleche-g, .fleche-d').show(); 
		}
	 	
		$('#ss_menu .box2Artistes').css('width',width);
		$('#ss_menu .box2Artistes2').css('width',width2);
		
		//Reinitialisation du scroll
		$('#ss_menu .box2Artistes').css('margin-left','0');
		$('#ss_menu .scroll2Box').css('margin-top','0');
		//réinitialisation des box artistes
		cptArtistes = 0;
		cptScrollbox =0;
		
		//Colorise en permanence la catégorie concerné
		$('.niveau1 li a').removeClass('tabActif');
		visible = $(this).attr('id').substr(3,2);
		//alert(visible);
		$('#tab'+visible+' a').addClass('tabActif');
		
	});
	
	
	$('#ss_menu a').not($('.artistes a')).click(function(){
		parent =  $(this).parent().parent().parent().parent().attr('id');
		name = $(this).parent().parent().parent().attr('name');
		$('#ss_menu #'+parent+' .box_ssmenu[name="'+name+'"] a').removeClass('actif');
		$(this).addClass('actif');
	});
	
	$('#ss_menu .artistes a').click(function(){
		$('#ss_menu .artistes a').removeClass('actif');
		$(this).addClass('actif');
	});
	
	//Pour les scroll hozizontaux
	$('#ss_menu .artistes .fleche-g').click(function(){
		if (cptArtistes > 0){
		cptArtistes-=1;
		id = $(this).parent().parent().attr('id').substr(7,2);
		$('#ss_menu #ss_menu'+id+' .box2Artistes').animate({
			marginLeft:-140*cptArtistes
		},400);
		}
	});
	
	$('#ss_menu .artistes .fleche-d').click(function(){
		if (cptArtistes < ((width/160))-1){
		cptArtistes+=1;
		id = $(this).parent().parent().attr('id').substr(7,2);
		$('#ss_menu #ss_menu'+id+' .box2Artistes').animate({
			marginLeft:-140*cptArtistes
		},400);
		}
	});
	
	///////// COPIE POUR SCROLLBOX 2 /////////////////////
	//Pour les scroll hozizontaux
	$('#ss_menu .artistes .fleche-g2').click(function(){
		if (cptArtistes > 0){
		cptArtistes-=1;
		id = $(this).parent().parent().attr('id').substr(7,2);
		$('#ss_menu #ss_menu'+id+' .box2Artistes2').animate({
			marginLeft:-140*cptArtistes
		},400);
		}
	});
	
	$('#ss_menu .artistes .fleche-d2').click(function(){
		if (cptArtistes < ((width/160))-1){
		cptArtistes+=1;
		id = $(this).parent().parent().attr('id').substr(7,2);
		$('#ss_menu #ss_menu'+id+' .box2Artistes2').animate({
			marginLeft:-140*cptArtistes
		},400);
		}
	});
	/////////////////////////////////////////////////
	
	//Pour les scroll verticaux
	$('#ss_menu .normalBox .fleche-h').click(function(){
		if (cptScrollbox > 0){
		cptScrollbox-=1;
		id = $(this).parent().parent().attr('id').substr(7,2);
		$('#ss_menu #ss_menu'+id+' .scroll2Box').animate({
			marginTop:-50*cptScrollbox
		},400);
		}
	});
	
	$('#ss_menu .normalBox .fleche-b').click(function(){
		if (cptScrollbox < 3){
		cptScrollbox+=1;
		id = $(this).parent().parent().attr('id').substr(7,2);
		$('#ss_menu #ss_menu'+id+' .scroll2Box').animate({
			marginTop:-50*cptScrollbox
		},400);
		}
	});
	
	
	
	
	
	
	
	
	
	//Les onglets du menu s'étalent sur toute la largeur : largeur des onglets en fonction du nombre d'onglet total
	for (var j=1; j<$('ul.niveau1 li').size()+1 ; j++){
		var currentWidth = parseInt($('ul.niveau1 li#tab'+j).css('width'));
		var resizeWidth = currentWidth+18;
		
		//$('ul.niveau1 li#tab'+j).css('width',resizeWidth+'px');
		//$('ul.niveau1 li#tab'+j).css('text-align','center');
	}
	
	//////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////
	/////////////////////// POPUP QUICK DETAILS //////////////////////
	//////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////


	var width;//Défini taille de box2Decli
	var name;//quel li ?
	
	//$('.btnSubmit').corner("5px");

	$('ul#product_list li.last-chance').hover(function(){
		id = $(this).attr('id').replace('prod_', '');
		$('ul#product_list li[id="prod_'+id+'"] .img_lastchance').show();
	},
	function(){
		$('ul#product_list li[id="prod_'+id+'"] .img_lastchance').hide();
	});
	
	$('#voile, .popup_produit').hide();
	
	//Voile recouvrant le site
	$('#voile').css('width',$(document).width());
	$('#voile').css('height',$(document).height());
	
	$(window).resize(function(){
		$('#voile').css('width',$(document).width());
		$('#voile').css('height',$(document).height());
	});
	
	$('.btnFermer').click(function(){
		$('.popup_produit').fadeOut('500');
		$('#voile').fadeOut();
		
	});
	
	var idProduit;
	var baseUrl;
	var urlAjax;
	
	$('.infosplus').click(function(){
		
		//récupération des données
		idProduit = $(this).parent().attr("id").substr(5,5);
		baseUrl = baseDir + "product_popup.php?id_product=";
		urlAjax = baseUrl + idProduit;
		//alert(idProduit);
		
		$('.popup_produit').fadeIn('500');
		$('#voile').fadeIn();
		width = $('.popup_produit .popup_right .decli_prod .box2Decli img').size() * 310;
		$('.popup_produit .popup_right .decli_prod .box2Decli').css('width',width);
		
		if(width == 0) $('.popup_produit .popup_right .decli_prod .fleche-d, .fleche-g').hide();
				
		$(".popup_produit .popup_right .infos_produit select.couleur, select.taille, select.quantite").find("option").remove();
		$(".popup_produit .popup_right .decli_prod .box2Decli ul").find("li").remove();
				
		$.ajax({ 
			url: urlAjax, 
			dataType : "json",
			success: function(product, data, textStatus, jqXHR){
			
			//var whishList = "javascript:WishlistCart('wishlist_block_list', 'add', '"+ idProduit +"', 0, 1);";
			$(".taux_reduc").attr("qty", product.quantity);
			if (parseInt(product.quantity) < 1){
				   $(".btn_add").css('display','none');
					$(".quantite").css('display','none');
				   $(".infos_stock").text("HORS STOCK");
				  
				   }  else{
				   $(".btn_add").css('display','block');
					$(".quantite").css('display','block');
				   $(".infos_stock").text("");
				   
				   }
				   
			$(".popup_produit .popup_left img.apercu").attr("src", product.path_img);
			$(".popup_produit .popup_left a").attr("href", product.path_fiche);
     		$(".popup_produit h2").html(product.title);
			$(".popup_produit .popup_right .shortDesc").html(product.desc);
			$(".popup_produit .popup_right .taux_reduc").text("-" + product.percent + "%");
			$(".popup_produit .popup_right .old_prix").text(product.oldPrice + " €");
			$(".popup_produit .popup_right .new_prix").text(product.newPrice + " €");
			$(".popup_produit .popup_right .infos2_produit .left b").text(product.ssFrais + " €");
			$(".popup_produit .popup_right input#product_page_product_id").attr("value", idProduit);
			$(".popup_produit .popup_right input#token").attr("value", product.token);
			//$(".popup_produit .popup_right .infos2_produit a.mywhish").attr("onclick", whishList);
			$(".popup_produit .popup_right .infos2_produit .add_2_compare_popup").attr("value", "compare_" + idProduit);
			
			if(product.percent == 0)
				$(".popup_produit .popup_right .taux_reduc, .popup_produit .popup_right .old_prix").hide();
				
			if(product.id_category_default == 29 || product.id_category_default == 30) 
				$(".popup_produit .popup_right .infos_produit .info_right").hide();
					
			var a;
			var b;
			var c;
			var d;
			
			if(product.colors == '')
				$(".popup_produit .popup_right .infos_produit select.couleur").css('visibility','hidden');
				
			if(product.taille == '')
				$(".popup_produit .popup_right .infos_produit select.taille").css('visibility','hidden');
				
			for (a=0 ; a<product.colors.length ; a++){
				$(".popup_produit .popup_right .infos_produit select.couleur").append("<option>" + product.colors[a] + "</option>");
			}
			
			for (b=0 ; b<product.taille.length ; b++){
				$(".popup_produit .popup_right .infos_produit select.taille").append("<option>" + product.taille[b] + "</option>");
			}
			
			for (c=1 ; c<=product.qte ; c++){
				$(".popup_produit .popup_right .infos_produit select.quantite").append("<option>" + c + "</option>");
			}
			
			for (d=0 ; d<product.decli.length ; d++){
				$(".popup_produit .popup_right .decli_prod .box2Decli ul").append("<li><img src='" + product.decli[d] + "' /></li>");
			}
			
			
			//Click sur les visuels de dérivé
			$('.box2Decli img').click(function(){
				var tinyUrl = "";
				var index = 0;
				
				tinyUrl = $(this).attr('src');
				index = tinyUrl.indexOf('small');
				
				var prevUrl = tinyUrl.substr(0,index);
				var nextUrl = tinyUrl.substr(index+5,200);
				var newUrl = prevUrl+'large'+nextUrl;
				
				$('.popup_left .apercu').attr('src',newUrl);
			})

		
      	},
			error: function(error){
				alert("Désolé il y'a eu une erreur, les données n'ont pu être chargées");
			}
		});//fin ajax
		
	});//fin fonction click
	
	//Scroll des décli
	var cptDecli = 0;
	$('.popup_produit .popup_right .decli_prod .fleche-d').click(function(){
		if (cptDecli < Math.round($('.popup_produit .popup_right .decli_prod .box2Decli img').size()/3)){
			cptDecli+=1;
			$('.popup_produit .popup_right .decli_prod .box2Decli').animate({
				marginLeft:-310*cptDecli
			});
		}
	});
	
	$('.popup_produit .popup_right .decli_prod .fleche-g').click(function(){
		if (cptDecli > 0){
			cptDecli-=1;
			$('.popup_produit .popup_right .decli_prod .box2Decli').animate({
				marginLeft:-310*cptDecli
			});
		}
	});

	/*
	for (var j=1; j<$('ul.niveau1 li').size()+1 ; j++){
		//alert('il faut resize tab'+j);
		var currentWidth = parseInt($('ul.niveau1 li#tab'+j).css('width'));
		var resizeWidth = currentWidth+18;
		
		$('ul.niveau1 li#tab'+j+'').css('width',''+resizeWidth+'px');
		$('ul.niveau1 li#tab'+j+'').css('text-align','center');
	}
	
	$('#tab10').css('width', '110px');
	*/
	

	
});
