// JavaScript Document
var initialSite = 'MW'; 
var posCur = 0;
var compteur = 0;

$(document).ready(function(){
	$('.thumb').each(function(){
			compteur++;		  
	});
	
	if(compteur > 5) $('#thumbDefilBas').fadeIn('fast');

	$('#thumbDefilBas').bind('click',function(){
		posCur -= 61;
		posMax = -1*((compteur-5)*61);
		posCur < posMax ? posCur = posMax :	posCur = posCur;
		if(posCur == posMax) $(this).fadeOut('fast');
		$("#containerThumbDeplacement2").animate({top: posCur+'px'}, 500);
		if(posCur < 0) $('#thumbDefilHaut').fadeIn('fast');
	});
	$('#thumbDefilHaut').bind('click',function(){
		posCur += 61;		
		posCur > 0 ? posCur = 0 : posCur = posCur;
		if(posCur == 0) $(this).fadeOut('fast');
		$("#containerThumbDeplacement2").animate({top: posCur+'px'}, 500);
		if(posCur > posMax) $('#thumbDefilBas').fadeIn('fast');
	});
	
	$('.thumb').hover(function(){
		var reg = new RegExp("[/]+", "g");
		var tab = $(this).attr('src').split(reg);
		if(window.navigator.appName == 'Microsoft Internet Explorer'){
			var ref = tab[4];
		}else{
			var ref = tab[5];
		}
		
		var reg = new RegExp("[_]+", "g");
		var tab = $(this).attr('id').split(reg);
		var num = tab[1];
		var debRef = ref.substr(0,3);
		var finRef = ref.substr(3,3);
		if($('#picApercu2').attr('src') != '/images/products_new/'+debRef+'/'+finRef+'/'+ref+'/'+ref+'_'+'large_'+initialSite+'_'+num+'.jpg'){
			$('#picApercu2').animate({left: '-520px'}, 250, 'swing', function(){
				$('#picApercu2').attr('src','/images/products_new/'+debRef+'/'+finRef+'/'+ref+'/'+ref+'_'+'large_'+initialSite+'_'+num+'.jpg');///images/products_new/AMP/AUV/AMPAUV0001/AMPAUV0001_large_MW_0.jpg
				$('#picApercu2').animate({left: '0px'}, 250);
			});
		}
	});
	$('.thumb').click(function(){
		$('img#picApercu2').click();
	});
	
	/*
	var margeTop;
	var margeTopRetour;
	var numCurrentGroup;
	var quiDoitBouger = 'niv1';
	$('ul#lesPacks li div img.flecheSurvol').bind('click',function(){
		if($(this).parent('div').parent('li').children('ul').attr('tagName')){
			if(($(this).parent().parent().attr('id') != "") && (quiDoitBouger == 'niv1')){
				//on déplace les éléments de niveau 1 qui n'ont pas été cliqué
				//var nomCurrentGroup = $(this).parent().attr('id');
				numCurrentGroup = $(this).parent().parent().attr('id').substr(6,1);
				margeTop = -86*(numCurrentGroup-1);
				for(var i=1;i<5;i++){
					if(i != numCurrentGroup){
						if(document.getElementById('Groupe'+i)){
							$('ul#lesPacks li#Groupe'+i).animate({left: '450px'}, 500, 'swing', function(){
								$('ul#lesPacks li#Groupe'+numCurrentGroup).animate({top: margeTop+'px'}, 500, 'swing', function(){
									//alert($(this).css('top'));
									$('ul#lesPacks li#Groupe'+numCurrentGroup+' ul').animate({left: '0px'}, 500);
									$('ul#lesPacks li#Groupe'+numCurrentGroup+' ul').css('display','block');
								});
							});
						}
					}
				}
				//on affiche le bouton retour
				$('#RetourPack span').fadeOut('fast', function(){
					$(this).attr('innerHTML', 'Retour');
					$(this).fadeIn('fast');
				});
				$('#RetourPack img').attr('src', '/images/pack/flecheHautFondPackBleu.jpg');
				//on change la fléche du parent
				$(this).attr('src','/images/pack/flechePackRetour.jpg');
				
				quiDoitBouger = 'niv2';
			}else{
			
			if(quiDoitBouger == 'niv2'){
				$('ul#lesPacks li#Groupe'+numCurrentGroup+' ul').animate({left: '-900px'}, 500, 'swing', function(){
					$('ul#lesPacks li#Groupe'+numCurrentGroup+' ul').css('display','none');
					$('ul#lesPacks li#Groupe'+numCurrentGroup).animate({top: '0px'}, 500, 'swing', function(){
						for(var i=1;i<5;i++){
							if(i != numCurrentGroup){
								if(document.getElementById('Groupe'+i)){
									$('ul#lesPacks li#Groupe'+i).animate({left: '0px'}, 500);
								}
							}
						}	
					});
				});
				quiDoitBouger = 'niv1';
			}
			//on affiche le bouton pack similaire
			$('#RetourPack span').fadeOut('fast', function(){
				$(this).attr('innerHTML', 'Packs similaires');
				$(this).fadeIn('fast');
			});

			$('#RetourPack img').attr('src', '/images/pack/flecheBasFondPackBleu.jpg');
			//on remet la fléche du parent dans le bon sens
			$('li#Groupe'+numCurrentGroup+' img.flecheSurvol').attr('src','/images/pack/flechePack.jpg');
			
			}
		}
	});
	$('div#RetourPack').bind('click',function(){
		if(quiDoitBouger == 'niv2'){
			$('ul#lesPacks li#Groupe'+numCurrentGroup+' ul').animate({left: '-900px'}, 500, 'swing', function(){
				$('ul#lesPacks li#Groupe'+numCurrentGroup).animate({top: '0px'}, 500, 'swing', function(){
					for(var i=1;i<5;i++){
						if(i != numCurrentGroup){
							if(document.getElementById('Groupe'+i)){
								$('ul#lesPacks li#Groupe'+i).animate({left: '0px'}, 500);
							}
						}
					}	
				});
			});
			quiDoitBouger = 'niv1';
		}
		//on affiche le bouton pack similaire
		$('#RetourPack span').attr('innerHTML', 'Packs similaires');
		$('#RetourPack img').attr('src', '/images/pack/flecheBasFondPackBleu.jpg');
		//on remet la fléche du parent dans le bon sens
		$('li#Groupe'+numCurrentGroup+' img.flecheSurvol').attr('src','/images/pack/flechePack.jpg');
	});
	$('ul#lesPacks div img.flecheSurvol').hover(function(){
		if($(this).parent('div').parent('li').children('ul').attr('tagName'))
			if(quiDoitBouger == 'niv1'){
				$(this).attr('src','/images/pack/flecheSurvolPack.jpg');
			}else{
				$(this).attr('src','/images/pack/flecheSurvolPackRetour.jpg');								  
			}
	},function(){
		//alert($(this).parent('div').parent('li').children('ul').attr('tagName'));
		if($(this).parent('div').parent('li').children('ul').attr('tagName')){
			if(quiDoitBouger == 'niv1'){
				$(this).attr('src','/images/pack/flechePack.jpg');
			}else{
				$(this).attr('src','/images/pack/flechePackRetour.jpg');
			}
		}else{
			$(this).attr('src','/images/pack/flechePackSansEnfant.jpg');
		}
		
	});
	
	// Mise en avant des produits  et fiches signalétiques
	$('.article').hover(function(e){
		if(window.navigator.appName != 'Microsoft Internet Explorer')
		$(this).fadeTo("fast", 0.60);
		
		//alert($(this).attr('id'));
		var reg = new RegExp("[_]+", "g");
		var tab = $(this).attr('id').split(reg);
		var ref = tab[0];
		var numGroupe  = tab[1];
		var numElement = tab[2];
		var obj = eval(ref+'_'+numGroupe);

		var name 						= obj.name;
		var teaser 					= obj.teaser;
		var priceBeforePack = obj.priceBeforePack;
		var priceAfterPack  = obj.priceAfterPack;
		var debRef = ref.substr(0,3);
		var finRef = ref.substr(3,3);
		
		$('#detail').html("<img style=\"float:left;margin:7px 5px 0px 5px;border:1px solid #797979;padding:2px;background:#FFF\" src=\"/images/products_new/"+debRef+'/'+finRef+'/'+ref+'/'+unescape(ref)+"_117.jpg\" /><div style=\"height:90px;\"><h3 style=\"font-size:12px;margin:10px 3px 5px 3px;color:#333;\">"+unescape(name)+"</h3><p style=\"margin:0 0 4px 0;color:#333;\">"+unescape(obj.teaser)+"</p></div><p style=\"color:#FFF;margin:0;width:100px;float:left;\">Hors pack :<br />"+unescape(obj.priceBeforePack)+"</p><p style=\"color:#FFF;margin:0;float:right;\">Dans ce pack :<br /><strong>"+unescape(obj.priceAfterPack)+"</strong></p>");
		
		
		
		
		var position = 0;
		if(numElement > 1){
			position = numElement;
		}else{
			if(($(this).parent().parent().css('top') == '0px') || $(this).parent().parent().css('top') == 'auto'){
				position = numGroupe;
			}else{
				position = numElement;
			}
		}
		
		//alert('Groupe : '+numGroupe+', El : '+numElement+', Top : '+$(this).parent().parent().css('top')+', position : '+position);
		//alert(position);
		
		//posX= e.pageX-375;
		posY = e.pageY+30;
		//var numGroupe  = $(this).attr('id').substr(11,1);
		//var numElement = $(this).attr('id').substr(13,1);
		//alert(posY+'-->'+$(this).parent().css('top'));
		if(position==1){
			//alert('position 1'+posY);
			$('#detail').css('margin-top','-262px');
			$('#detail').fadeIn(200);
			//$('#detail').css('display','block');
			//$('#detail').slideDown(200);
		}
		if(position==2){
			//alert('position 2'+posY);
			$('#detail').css('margin-top','-175px');
			$('#detail').fadeIn(200);
			//$('#detail').css('display','block');
			//$('#detail').slideDown(200);
		}
		if(position==3){
			//alert('position 3'+posY);
			$('#detail').css('margin-top','-305px');
			$('#detail').fadeIn(200);
			//$('#detail').css('display','block');
			//$('#detail').slideDown(200);
		}
		if(position==4){
			//alert('position 4'+posY);
			$('#detail').css('margin-top','-220px');
			$('#detail').fadeIn(200);
			//$('#detail').css('display','block');
			//$('#detail').slideDown(200);
		}
		
		//$('#detail').css('display','block');

	}, function(){
		if(window.navigator.appName != 'Microsoft Internet Explorer')
		$(this).fadeTo("fast", 1);
		//$('#detail').css('margin-top','0px');
		$('#detail').css('display', 'none');
	});
	
*/	
	
	/****** Blink d'un bouton  ******/
	var fadeAutoReverse = {
		start : function(){
			$('#areaAjoutPanier a').fadeTo('slow',0.3, function(){fadeAutoReverse.backward();
															});
		},
		backward : function(){
			$('#areaAjoutPanier a').fadeTo('slow',1, function(){fadeAutoReverse.start();
															});
		}
	}
	//fadeAutoReverse.start();
	
	/******* popup zoom image *****/
	var popupStatus = 0;  
	//loading popup with jQuery magic!
	function loadPopup(){
		//loads popup only if it is disabled
		if(popupStatus==0){
			$("#backgroundPopup").css({
			"opacity": "0.7"
			});
			$("#backgroundPopup").fadeIn("slow");
			$("#popup").fadeIn("slow");
			popupStatus = 1;
		}
	}
	//disabling popup with jQuery magic!
	function disablePopup(){
		//disables popup only if it is enabled
		if(popupStatus==1){
			$("#backgroundPopup").fadeOut("slow");
			$("#popup").fadeOut("slow");
			popupStatus = 0;
		}
	}
	//centering popup
	function centerPopup(){
		//request data for centering
		//alert(window.pageYOffset+' '+document.documentElement.scrollTop+' '+window.innerHeight+' '+document.documentElement.clientHeight);
		var reg = new RegExp("[/]+", "g");
		var tab = $("#picApercu2").attr('src').split(reg);
		
		var reg = new RegExp("[_]+", "g");
		if(window.navigator.appName == 'Microsoft Internet Explorer'){
			var tab = tab[5].split(reg);
		}else{
			var tab = tab[6].split(reg);
		}
		//DDESDV0101N_main_MW_zoom_0.jpg  400*482
		
		var pathOfZoom = '/images/products_new/'+tab[0].substr(0,3)+'/'+tab[0].substr(3,3)+'/'+tab[0]+'/'+tab[0]+'_zoom_MW_'+tab[3];
		$("#imgZoom").attr('src', pathOfZoom);
		//alert(pathOfZoom);
		
		//alert($("#imgZoom").attr('height'));
		$("#popup").css('height', $("#imgZoom").attr('height'));
		$("#popup").css('width', $("#imgZoom").attr('width'));
		
		if(window.navigator.appName != 'Microsoft Internet Explorer'){
			var offsetY = window.pageYOffset;
			var windowWidth = document.documentElement.clientWidth;
			var windowHeight = window.innerHeight;//document.documentElement.clientHeight;
		}else{
			var offsetY = document.body.scrollTop;
			var windowWidth = document.body.clientWidth;
			var windowHeight = document.body.clientHeight;
			var offsetTotal = document.body.scrollHeight;
			
		}
		
		var popupHeight = $("#popup").height();
		var popupWidth = $("#popup").width();
		//centering
		/*$("#popup").css({
			"position": "absolute",
			"top": windowHeight/2+offsetY-popupHeight/2,
			"left": windowWidth/2-popupWidth/2
		});*/
		var var1 = (windowHeight / 2) + offsetY - (popupHeight/2);
		var var2 = (windowWidth / 2) - (popupWidth/2);
		$("#popup").css('position', 'absolute');
		$("#popup").css('top', var1);
		$("#popup").css('left', var2);
		//only need force for IE6
		//alert(windowWidth+' '+windowHeight+' '+offsetTotal+' '+offsetY);
		
		$("#backgroundPopup").css("width", windowWidth);
		$("#backgroundPopup").css("height", windowHeight+offsetTotal+offsetY);
	}
	
	//LOADING POPUP
	//Click the button event!
	/*$("#picApercu2").click(function(){
		centerPopup();
		loadPopup();
	});*/
	
	//CLOSING POPUP
	//Click the x event!
	$("#popupClose").click(function(){
		disablePopup();
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
	
	
	
});
/****  Preload des photos  ****/
function simplePreload(){ 
	var args = simplePreload.arguments;
	document.imageArray = new Array(args.length);
	for(var i=0; i<args.length; i++){
		document.imageArray[i] = new Image;
		document.imageArray[i].src = args[i];
	}
}

function ficheTechnique(teaser, name, erp, priceBeforePack, priceAfterPack){
	this.teaser				= teaser;
	this.name				= name;
	this.erp				= erp;
	this.priceBeforePack	= priceBeforePack;
	this.priceAfterPack		= priceAfterPack;

}