/**
 * @author Nurun
 */



function initGalerie(){
	 //galerieNow=0;
	//alert("initGalerie");
	 // BOUTONS : Suivant / Pr?c?dent
	 Event.observe('prev', 'click', galeriePrev, false);
	 Event.observe('next', 'click', galerieNext, false);
	 Event.observe('galeryZoom', 'mouseout', galerieZoomOff, false);
	 document.onmousemove = captureMousePosition;
	 // Pagination
	 //alert("avant paginate");
	 paginate();
	 //alert("après paginate");
	 // Nbr photos / Slide
	 galerieNbr=5;
	 galerieSlide=galerieMax-galerieNbr;
	 galerieNext=0;
	 
	 // Position mouse
	  xMousePos = 0;
	  yMousePos = 0;
	  
	  
	 // Slide Transition
	 animDelay=1;
	 animDeplace=20;
	 
	 galerieGo(galerieNow);
}

function paginate(){
	//alert("paginate");
	 $('galeryPagination').innerHTML = "";
	 //alert("avant getProductIdList")
	 //var tab=getProductIdList(selectedModel,selectedColor,selectedTheme);
	 tab=getProductIdList(selectedModel,selectedColor,selectedTheme);
	 galerieMax = Math.ceil(tab.length/5);
	 //galerieNow=1;
	 //alert("avant for");
	 j=1;
	 //for (var i=0;i<tab.length;i=i+5){
	 for (var i=0;i<galerieMax;i=i+1){
		j++;
		$('galeryPagination').innerHTML += "<a href='#"+buildPageUrl(i)+"' onclick='galerieGo("+i+");'>"+(i+1)+"</a>";
	}
	//alert("après for");
	displayCursor();
}
function displayCursor(){
	$('next').style.display='block';
	$('prev').style.display='block';
	if(galerieNow==0){
		$('prev').style.display='none';
	}
	if(galerieNow+1>=galerieMax){
	//if(galerieNow+5>=galerieMax*5){
		$('next').style.display='none';
	}
	k=0;
	for(var i=0;i<galerieMax;i=i+1){
	//for(var i=0;i<tab.length;i=i+5){
		k=i/5;
		$('galeryPagination').getElementsByTagName('a')[i].className="";
		if(i==galerieNow){
			$('galeryPagination').getElementsByTagName('a')[i].className="current";
		}
	}
}
//*** ZOOM GALERIE ***//
function galerieZoom(index, id){
	if (displayZoom == true) {
		// Changement url
		if (getProductUrl(index) != "" && getProductUrl(index) != undefined && getProductUrl(index) != null) {
			$('galeryZoomUrl').href = getProductUrl(index);
		}
		else {
			$('galeryZoomUrl').href = "#";
		}
		// Changement image
		$('galeryZoomImage').src = getLargeImageProductUrl(index, selectedColor);
		if (getProductPrice(index) != "" && getProductPrice(index) != undefined && getProductPrice(index) != null) {
			var productPrice = getProductPrice(index);
			var productOriginalPrice = getProductOriginalPrice(index);

			if (productPrice != productOriginalPrice) {
				//el090115 - modif affichage pourcentage //$('galeryZoomPrice').innerHTML = "<span class='prix_barre'>" + productOriginalPrice + " Euros</span>&nbsp;" + productPrice + " Euros&nbsp;";
				$('galeryZoomPrice').innerHTML =  productPrice + " Euros";
			}			
			//modif MB090727
			//"<span class='prix_barre'>" + productOriginalPrice + " Euros</span>&nbsp;" + || &nbsp;<span class='promo'>- " + getProductPercentPrice(index) + "%</span>"
			else
			{
				$('galeryZoomPrice').innerHTML = getProductPrice(index) + " Euros";
			}
		}
		else {
			$('galeryZoomPrice').innerHTML = "Prix non disponible";
		}

		// Positionement Calque
		document.onmousemove = captureMousePosition;
		var imgPos = $(id+"_img").cumulativeOffset();
		var imgPos2 = $("scrollZone").cumulativeOffset();
		$("galeryPagination").style.zIndex=10;
		$("galeryZoom").style.zIndex=9;
		$("scrollZone").style.zIndex=8;
		//alert(id+" "+imgPos[0]);
		/*if (xMousePos!=null) {
			$('galeryZoom').style.left = (xMousePos - 60) + "px";
		}
		if (yMousePos!=null) {
			$('galeryZoom').style.top = (yMousePos - 60) + "px";
		}*/
		//var imgPos=$(id).cumulativeOffset();
		$('galeryZoom').style.left = (imgPos[0]-imgPos2[0]-30) + "px";
		$('galeryZoom').style.top = (imgPos[1]-imgPos2[1]-50) + "px";
		
		//alert(x+" "+y);
		//$('galeryZoom').style.left = x+"px";
		//$('galeryZoom').style.top = y+"px";
		// Apparition Calques
		//$('galeryZoom').style.opacity="0";
		//$('galeryZoom').style.filter="alpha(opacity=0)";
		$('galeryZoomImage').style.opacity = "0";
		$('galeryZoomImage').style.filter = "alpha(opacity=0)";
		$('galeryZoom').style.visibility = "visible";
		$('galeryZoom').style.display = "block";
		$('galeryZoomImage').onload = function(){
                //alert("onload")
                //this.style.display="block";
                new Effect.Appear('galeryZoomImage', {
                    duration: 0.5,
                    fps: 25,
                    from: 0.0,
                    to: 1.0
                });
                //new Effect.Opacity('backgroundImage',{ duration: 0.5, transition: Effect.Transitions.linear, from: 0, to: 1 });
            }
		//new Effect.Opacity('galeryZoom',{ duration: 0.5, transition: Effect.Transitions.linear, from: 0, to: 1 });
		/*new Effect.Opacity('galeryZoomImage', {
			duration: 0.5,
			transition: Effect.Transitions.linear,
			from: 0,
			to: 1
		});*/
	}
}
function galerieZoomOff(){
	 $('galeryZoom').style.visibility="hidden";
}


//*** BOUTONS GALERIE ***//

function galerieGo(id){
	  //$('galeryList').style.left = 0-(190*id*5)+"px";
	  $('galeryList').style.overflow="hidden";
	  new Effect.Move($('galeryList'), { x: 0-(190*(id)*5), y: 0 , mode:'absolute',duration: (2.0+0.6*(Math.abs(galerieNow-id)-1)), afterFinish:allowZoom}); 
	  //new Effect.MoveBy('galeryList', 0, 0-(190*(galerieNow-id)*5), {duration:3, fps:25, from:0.0, to:1.0});
	 galerieNow = id;
	 displayCursor();
	 galerieZoomOff();
	 displayZoom=false;
}

function galeriePrev(){
	galerieGo(galerieNow-1);
}
function galerieNext(){
	galerieGo(galerieNow+1);
}

function captureMousePosition(e) {
	// IE
    if (document.all) {
		xMousePos = event.clientX;
		yMousePos = event.clientY;
	}
	// Firefox
	else {
		xMousePos = e.pageX;
		yMousePos = e.pageY;
	}
}