$(document).ready(function() {
	
	$("a[href$='pdf']").each(function() {
	  $(this).attr('target', '_blank');
	});
	
	// Ajout de propriété CSS pour les vieux navigateurs
	$('#not_nested ul li:nth-child(n+11)').remove();
	$('#not_nested ul li:last-child').css({'width':'96px','border-right':'none'});
	$('.navSecondaire li:last-child,#footer li:last-child').css({'border-right':'none'});
	
	// Diaporama d'arrière plan
	$('.rubrique_sommaire_ .carousel-wrap').cycle({
			fx: 'fade',
			speed:  'slow', 
			pager:  '#not_nested .first_level',
			pagerEvent: 'mouseover',
			pauseOnPagerHover: 1,
		    pagerAnchorBuilder: function(idx, slide) { 
		        return '#not_nested .first_level li:eq(' + idx + ') a'; 
		    } 
	});
	
	// Permet de rendre clicable les onglets malgré de diaporama
	$('#not_nested .first_level li a').click(function(){
		window.open($(this).attr('href'),'_self');
	});
	
	// Redimensionnement
	$(window).resize(function(){ 
		$('.rubrique_sommaire_ #slide').width($(window).width());
		$('.rubrique_sommaire_ #slide .carousel-wrap').width($(window).width());
		$('.rubrique_sommaire_ #slide .item').width($(window).width());
		return false 
	});
	
	
	// Supprime la marge à Gauche quand il n'y pas pas de navigation contextuelle
	if($.trim($("#leftbar").text()) === ""){
		$('#conteneur').css({'padding-left':'0px','width':'960px'});
	}
	
	
	// Affiche RightBar dans le catalogue
	// if($.trim($("#main .catalog").text()) !== ""){
	// 				$('#contents').css({'padding-right':'155px'});
	// 				$('#rightbar').css({'display':'block'});
	// 			}
	
	
	

	
	
	
	// ==================================================
	// = Générateur de tableau pour les fiches produits =
	// ==================================================
	
if($('#formproductversion label[for="variante2"]').length != 0){

	// Création d'un tableau vide
	$('#formproductversion').after('<div id="scrolltable"><table border="1" id="valeurs">');
	$('#valeurs').prepend('<thead><tr></tr></thead>');
	
	// Création des entêtes de colonne
	// Ajout de la colonne pour la variante 1
	//var var1Nom = $('#formproductversion label[for="variante1"]').html();
	$('#valeurs tr').append('<th>'+$('#formproductversion label[for="variante1"]').html()+'</th>');
	
	// Ajout des colonnes pour la variante 2
	// listage des noms de dimensions
	
	var var2Nom = new Array();
	var2Nom = $('#formproductversion label[for="variante2"]').html().split(' / ');
	// Ajout des colonnes dimmensions
	for (var i=0; i < var2Nom.length; i++) {
		$('#valeurs tr').append('<th>'+var2Nom[i]+'</th>');
	};

	// Ajout de la colonne pour le bouton radio
	$('#valeurs tr').append('<th>&nbsp;</th>');



	// Création du tableau de toutes les combinaisons de variante possibles 

	// Listage des valeur de la variante 1 (Matière)
	var var1List = new Array();
	var var1Tag = new Array();
	$('#formproductversion .fields:eq(0) option').each(function(){
		var1List.push($(this).html());
		var1Tag.push($(this).attr('value'));
	});


	// Listage des valeur de la variante 2 (Dimensions)
	var var2List = new Array();
	var var2Tag = new Array();
	$('#formproductversion .fields:eq(1) option').each(function(){
		var2List.push($(this).html());
		var2Tag.push($(this).attr('value'));
	});



	
	var combNum = var1List.length * var2List.length;
	var var1var2 = new Array();
	var ptV1 = 0;
	var ptV2 = 0;
	for (var i=0; i < combNum; i++) {
		var1var2[i] = new Array();
		var1var2[i][0] = var1List[ptV1];
		var1var2[i][1] = var1Tag[ptV1];
		var1var2[i][2] = var2List[ptV2];
		var1var2[i][3] = var2Tag[ptV2];
		if(ptV2 < var2List.length-1) {
			ptV2++;
		}else{
			ptV2 = 0;
			if(ptV1 < var1List.length) {
				ptV1++;
			}	
		};
	};

	// $('#valeurs').append('<tbody></tbody>');
	
	// Création des lignes
	var dimensionsList = new Array();
	for (var i=0; i < var1var2.length; i++){
		dimensionsList = var1var2[i][2].split(' / ');
		var ligne = "<tr ><td class='"+var1var2[i][1]+"'>"+var1var2[i][0]+"</td>";
		for (var j=0; j < dimensionsList.length; j++){
			ligne+='<td class="'+var1var2[i][3]+'">'+dimensionsList[j]+'</td>';
		};
		ligne += "<td><input type='radio' name='ref' /></td></tr>";
		$('#valeurs').append(ligne);
	};
	
	
	$('#valeurs input:eq(0)').attr('checked','checked');
	
	$('#valeurs tr').click(function(){
		$(this).find('input').attr('checked','checked');
		var selectedVar1 = $(this).find('td').attr('class');
		$("select#variante1 option[value='"+selectedVar1+"']").attr('selected', 'selected');
		var selectedVar2 = $(this).find('td').next('td').attr('class');
		$("select#variante2 option[value='"+selectedVar2+"']").attr('selected', 'selected');
		updatePrice();
		
	}).mouseover(function(){
		$(this).css({'cursor':'pointer','background-color':'#f6f6f6'})
	}).mouseout(function(){
		$(this).css({'cursor':'pointer','background-color':'#fff'})
	});
	
	
	$('#formproductversion fieldset .row:eq(0),#formproductversion fieldset .row:eq(1)').hide();
}

	
/* SI une seule variante */ 

if($('#formproductversion label[for="variante2"]').length == 0){
	
	// Création d'un tableau vide
	$('#formproductversion').after('<div id="scrolltable"><table border="1" id="valeurs">');

	// Création du tableau de toutes les combinaisons de variante possibles 

	// Listage des valeur de la variante 1 (Matière)
	var var1List = new Array();
	var var1Tag = new Array();
	$('#formproductversion .fields:eq(0) option').each(function(){
		var1List.push($(this).html());
		var1Tag.push($(this).attr('value'));
	});

	// Création des entêtes de colonne
	// Ajout de la colonne pour la variante 1
	// var var1Nom = $('#formproductversion label[for="variante1"]').html();

	$('#valeurs').prepend('<thead></thead>');


		varList = $('#formproductversion label[for="variante1"]').text().split(' / ');
		var titre = "<tr>";
		for (var j=0; j < varList.length; j++){
			titre+='<th>'+varList[j]+'</th>';
		};
		titre += "<th>&nbsp;</th></tr>";
		$('#valeurs thead').append(titre);








	// $('#valeurs').append('<tbody></tbody>');
	for (var i=0; i < var1List.length; i++){
		dimensionsList = var1List[i].split(' / ');
		var ligne = "<tr >";
		for (var j=0; j < dimensionsList.length; j++){
			ligne+='<td class="'+var1Tag[i]+'">'+dimensionsList[j]+'</td>';
		};
		ligne += "<td><input type='radio' name='ref' /></td></tr>";
		// alert(ligne);
		$('#valeurs').append(ligne);
	};	
	
	
	
	
	
	
	
	
	
	
	
	
	
	$('#valeurs input:eq(0)').attr('checked','checked');

	$('#valeurs tr').click(function(){
		$(this).find('input').attr('checked','checked');
		var selectedVar1 = $(this).find('td').attr('class');
		$("select#variante1 option[value='"+selectedVar1+"']").attr('selected', 'selected');
		updatePrice();

	}).mouseover(function(){
		$(this).css({'cursor':'pointer','background-color':'#f6f6f6'})
	}).mouseout(function(){
		$(this).css({'cursor':'pointer','background-color':'#fff'})
	});


	$('#formproductversion fieldset .row:eq(0),#formproductversion fieldset .row:eq(1)').hide();
	
	
	


}








    
	//$('#valeurs input').click(function(){
	//	var selectedVar1 = $(this).parent().parent().find('td').attr('class');
	//	$("select#variante1 option[value='"+selectedVar1+"']").attr('selected', 'selected');
	//	var selectedVar2 = $(this).parent().parent().find('td').next('td').attr('class');
	//	$("select#variante2 option[value='"+selectedVar2+"']").attr('selected', 'selected');
	//	updatePrice();
	//});
	
	// ================================
	// = Changement de visuel produit =
	// ================================
	
	$('#visuels_additionnels div').click(function(){
		var urlThumbnail = $(this).find('img').attr('src');
		var urlCurrent = $('#productPicture img').attr('src');
		urlCurrent = urlCurrent.replace('/media/','/media/thumbnails/');
		var urlFullsize = urlThumbnail.replace('thumbnails/','');
		$(this).find('img').attr('src',urlCurrent);
		$('#productPicture img').attr('src',urlFullsize);
		
	}).mouseover(function(){
		$(this).css({'cursor':'pointer'});
	});







	$("label:contains('Quantité : ')").parent().remove();











	// ==========
	// = Divers =
	// ==========

	
	$("#wrapper #conteneur .first_level li").hover(function(){
	   $(this).addClass("iehover");
	 },function(){
	   $(this).removeClass("iehover");
	 });
	
	
	
	
	
	

	
	$('.familly, .short_product').css({cursor:'pointer'}).click( function() { 
		window.location.href = $(this).find('a').attr('href');
		return false;
	} );
	
	
	if ($('.familly img').width()>140){
		$('.familly img').width(140);
	}
	
	if ($('#product #productPicture img').width()>190){
		$('#product #productPicture img').width(190);
		
	}
	
	if ($('#produits_associes .short_product img').width()>90){
				$('#produits_associes .short_product img').width(90);
	}
	
	if ($('.panier #formcart img').width()>50){
		$('.panier #formcart img').width(50);
	}
	if ($('.panier #cart_detail img').width()>50){
		$('.panier #cart_detail img').width(50);
	}
	if ($('.promoBox img').width()>90){
		$('.promoBox img').width(90);
	}

	// if ($('#contents').height()+20>$('#leftbar').height()){
	// 	$('#leftbar').height($('#contents').height()+20);
	// }else if ($('#contents').height()+20<$('#leftbar').height()) {
	// 	$('#contents').height($('#leftbar').height()+30);
	// }
	
	if ($('#conteneur').height()<450) {
		$('#conteneur').height(450);
	}
	$('.rubrique_sommaire_ #conteneur').height(550);

	
	
	
});


