var glissGallery_zoom = {
	'zoom':null,
	'inZoom':null,
	'box':null,
	'inBox':null,
	'over':null,
	'zoomed':null
};
var glissGallery_params = {
	'zoomRegExp':/(.*?product\/)([0-9]+)(\/gallery\/)([0-9]+)(\/.*?-)[a-z_]+(\.[a-z]{3,4})/,
	'galleryRegExp':/(.*?gallery\/[a-z]+)(_enable)?(\..*)/,
	'x':null,
	'y':null,
	'xBox':null,
	'xBoxHalf':null,
	'yBox':null,
	'yBoxHalf':null,
	'xMax':null,
	'yMax':null,
	'xCenter':null,
	'yCenter':null,
	'xScale':null,
	'yScale':null,
	'elementCount':null,
	'elementWidth':null,
	'previousEnable':false,
	'nextEnable':false
};
var glissGallery_preloaded = new Array;
var glissGallery_gallery = {
	'gallery':null,
	'elements':null,
	'previous':null,
	'next':null
};

Event.observe(window, 'load', glissGalleryInit);

function glissGalleryInit(event){
		//Stop event and delete load listener
	Event.stop(event);
	Event.stopObserving(window, 'load', glissGalleryInit);

	//glissGalleryMakeZoom();
	glissGalleryMakeGallery();
}

	//Make zoom elements
function glissGalleryMakeZoom(){
	if($$('.ficheProduitContent')[0] != null){
		glissGallery_zoom.zoom = new Element('div', {'id':'zoom'});
		glissGallery_zoom.box = new Element('div', {'id':'zoom_box'});
		glissGallery_zoom.over = new Element('div', {'id':'zoom_over'});

			//Insert zoom window into DOM
		$$('.ficheProduitContent')[0].insert({'bottom':glissGallery_zoom.zoom});

			//Get some params
		glissGallery_params.x = glissGallery_zoom.zoom.getWidth();
		glissGallery_params.y = glissGallery_zoom.zoom.getHeight();

		glissGallerySetZoom();
	}
}

	//Make zoom on actual displayed image(s)
function glissGallerySetZoom(){
		//For each elements (must be an image) with zoom class
	$$('.zoom').each(function(img){
		var regExec = glissGallery_params.zoomRegExp.exec(img.src);

		if(regExec != null){
				//Preload full size image
			if(glissGallery_preloaded[regExec[2]] == null){
				glissGallery_preloaded[regExec[2]] = new Array;
			}
			glissGallery_preloaded[regExec[2]][regExec[4]] = new Image;
			glissGallery_preloaded[regExec[2]][regExec[4]].src = regExec[1] + regExec[2] + regExec[3] + regExec[4] + regExec[5] + 'original' + regExec[6];
			
				//Declare mouseover event
			img.observe('mouseover', glissGalleryDisplayZoom);
		}
	});
}

	//Display zoom
function glissGalleryDisplayZoom(event){
		//Stop event and delete load listener
	Event.stop(event);

		//Local var declaration
	var regExec, tmp;

		//Get event dispatcher
	glissGallery_zoom.zoomed = event.element();

	regExec = glissGallery_params.zoomRegExp.exec(glissGallery_zoom.zoomed.src);

		//Make some math
			//Box size
	tmp = glissGallery_zoom.zoomed.width * glissGallery_params.x / glissGallery_preloaded[regExec[2]][regExec[4]].width;
	if(tmp > glissGallery_zoom.zoomed.width){
		glissGallery_params.xBox = Math.round(glissGallery_zoom.zoomed.width);
		glissGallery_params.xBoxHalf = Math.round(glissGallery_zoom.zoomed.width / 2);
	}else{
		glissGallery_params.xBox = Math.round(tmp);
		glissGallery_params.xBoxHalf = Math.round(tmp / 2);
	}

	tmp = glissGallery_zoom.zoomed.height * glissGallery_params.y / glissGallery_preloaded[regExec[2]][regExec[4]].height;
	if(tmp > glissGallery_zoom.zoomed.height){
		glissGallery_params.yBox = Math.round(glissGallery_zoom.zoomed.height);
		glissGallery_params.yBoxHalf = Math.round(glissGallery_zoom.zoomed.height / 2);
	}else{
		glissGallery_params.yBox = Math.round(tmp);
		glissGallery_params.yBoxHalf = Math.round(tmp / 2);
	}

			//Max limits
	glissGallery_params.xMax = glissGallery_params.x - glissGallery_preloaded[regExec[2]][regExec[4]].width;
	glissGallery_params.yMax = glissGallery_params.y - glissGallery_preloaded[regExec[2]][regExec[4]].height;

			//Zoom center
	if(glissGallery_params.xMax > 0){
		glissGallery_params.xCenter = Math.round(glissGallery_params.xMax / 2);
	}else{
		glissGallery_params.xCenter = 0;
	}

	if(glissGallery_params.yMax > 0){
		glissGallery_params.yCenter = Math.round(glissGallery_params.yMax / 2);
	}else{
		glissGallery_params.yCenter = 0;
	}

			//Scales
	glissGallery_params.xScale = glissGallery_preloaded[regExec[2]][regExec[4]].width / glissGallery_zoom.zoomed.width;
	glissGallery_params.yScale = glissGallery_preloaded[regExec[2]][regExec[4]].height / glissGallery_zoom.zoomed.height;

		//Ok now zoom only if orginal is wider of heigher
	if(glissGallery_params.xCenter == 0 || glissGallery_params.yCenter == 0){
			//Reduce image opacity
		glissGallery_zoom.zoomed.setStyle({'opacity':'0.5'});

			//Insert box into DOM
		glissGallery_zoom.box.setStyle({
			'width':glissGallery_params.xBox + 'px',
			'height':glissGallery_params.yBox + 'px',
			'top':glissGallery_zoom.zoomed.positionedOffset().top + 'px',
			'left':glissGallery_zoom.zoomed.positionedOffset().left + 'px'
		});
		glissGallery_zoom.inBox = new Image;
		glissGallery_zoom.inBox.src = glissGallery_zoom.zoomed.src;
		glissGallery_zoom.box.insert({'top':glissGallery_zoom.inBox});
		$$('.ficheProduitContent')[0].insert({'bottom':glissGallery_zoom.box});
	
			//Insert over into DOM
		glissGallery_zoom.over.update('');
		glissGallery_zoom.over.setStyle({
			'width':glissGallery_zoom.zoomed.width + 'px',
			'height':glissGallery_zoom.zoomed.height + 'px',
			'top':glissGallery_zoom.zoomed.positionedOffset().top + 'px',
			'left':glissGallery_zoom.zoomed.positionedOffset().left + 'px',
			'backgroundColor':'transparent',
			'opacity':'1'
		});
		$$('.ficheProduitContent')[0].insert({'bottom':glissGallery_zoom.over});
	
			//Display zoom and feign once mousemove event
		glissGallery_zoom.zoom.setStyle({
			'display':'block'
		});
		glissGallery_zoom.inZoom = new Image;
		glissGallery_zoom.inZoom.src = glissGallery_preloaded[regExec[2]][regExec[4]].src;
		glissGallery_zoom.zoom.insert({'top':glissGallery_zoom.inZoom});
		glissGalleryFollowZoom(event, regExec[2], regExec[4]);

			//Define event on over
		glissGallery_zoom.over.observe('mouseout', glissGalleryHideZoom);
		glissGallery_zoom.over.observe('mousemove', function(eventM){glissGalleryFollowZoom(eventM, regExec[2], regExec[4]);});
	}else{
			//Insert over into DOM
		glissGallery_zoom.over.setStyle({
			'width':glissGallery_zoom.zoomed.width + 'px',
			'height':glissGallery_zoom.zoomed.height + 'px',
			'top':glissGallery_zoom.zoomed.positionedOffset().top + 'px',
			'left':glissGallery_zoom.zoomed.positionedOffset().left + 'px',
			'backgroundColor':'white',
			'opacity':'0.75'
		});
		glissGallery_zoom.over.update('Zoom non disponible');
		$$('.ficheProduitContent')[0].insert({'bottom':glissGallery_zoom.over});
		glissGallery_zoom.over.observe('mouseout', glissGalleryHideZoom);
	}
}

	//Process mouse move data
function glissGalleryFollowZoom(event, prodId, orderId){
	Event.stop(event);

		//Local vars declaration
	var x, y, offsetX, offsetY;

		//Get offset by browser
	offsetX = event.offsetX || event.layerX;
	offsetY = event.offsetY || event.layerY;

		//Define x modifier
	if(offsetX < glissGallery_params.xBoxHalf || glissGallery_preloaded[prodId][orderId].width < glissGallery_params.x){
		x = 0;
	}else if(offsetX + glissGallery_params.xBoxHalf > glissGallery_zoom.zoomed.width){
		x = glissGallery_params.xMax;
	}else{
		x = parseInt('-' + Math.round((offsetX - glissGallery_params.xBoxHalf) * ((glissGallery_preloaded[prodId][orderId].width) / glissGallery_zoom.zoomed.width)));
	}

		//Define y modifier
	if(offsetY < glissGallery_params.yBoxHalf || glissGallery_preloaded[prodId][orderId].height < glissGallery_params.y){
		y = 0;
	}else if(offsetY + glissGallery_params.yBoxHalf > glissGallery_zoom.zoomed.height){
		y = glissGallery_params.yMax;
	}else{
		y = parseInt('-' + Math.round((offsetY - glissGallery_params.yBoxHalf) * ((glissGallery_preloaded[prodId][orderId].height) / glissGallery_zoom.zoomed.height)));
	}

		//Apply modifiers on box
	glissGallery_zoom.box.setStyle({'marginLeft':Math.round(-x / glissGallery_params.xScale) - 1 + 'px', 'marginTop':Math.round(-y / glissGallery_params.yScale)	 - 1 + 'px'});
	glissGallery_zoom.inBox.setStyle({'marginLeft':Math.round(x / glissGallery_params.xScale) + 'px', 'marginTop':Math.round(y / glissGallery_params.yScale) + 'px'});

		//If zoom image smaller than zoom zone center it, apply modifier otherwise
	if(glissGallery_params.xCenter > 0){
		x = glissGallery_params.xCenter;
	}
	if(glissGallery_params.yCenter > 0){
		y = glissGallery_params.yCenter;
	}
	glissGallery_zoom.inZoom.setStyle({'marginLeft':x + 'px', 'marginTop':y + 'px'});
}

	//Stop zoom
function glissGalleryHideZoom(event){
		//Stop event and delete load listener
	Event.stop(event);
	glissGallery_zoom.over.stopObserving();

		//Hide zoom
	zoom.setStyle({'display':'none'});
	glissGallery_zoom.zoom.update('');

	if(glissGallery_zoom.box.ancestors() != ''){
		glissGallery_zoom.box.remove();
		glissGallery_zoom.box.update('');
	}
	if(glissGallery_zoom.over.ancestors() != ''){
		glissGallery_zoom.over.remove();
		glissGallery_zoom.over.update('');
	}

	glissGallery_zoom.zoomed.setStyle({'opacity':'1'});
}

function glissGalleryMakeGallery(){
	glissGallery_gallery.gallery = $('gallery');

		//Make slide
	if(glissGallery_gallery.gallery != null){
		glissGallery_gallery.elements = $$('#gallery .elements')[0];
		glissGallery_params.elementCount = glissGallery_gallery.elements.childElements().length;

		if(glissGallery_params.elementCount > 3){
			glissGallery_gallery.previous = $('gallery_previous');
			glissGallery_gallery.next = $('gallery_next');
			glissGallery_params.elementWidth = $$('#gallery .element')[0];
			glissGallery_params.elementWidth = parseInt(glissGallery_params.elementWidth.getStyle('marginLeft')) + glissGallery_params.elementWidth.getWidth() + parseInt(glissGallery_params.elementWidth.getStyle('marginRight'));

				//Adjust width
			glissGallery_gallery.elements.setStyle({'width':glissGallery_params.elementCount * glissGallery_params.elementWidth + 'px'});
			glissGalleryEnableNext();
		}

		glissGallery_gallery.elements.childElements().each(function(div){
			div.childElements()[0].observe('click', glissGalleryChangeZoom);
			//div.childElements()[0].observe('click', function(e){Event.stop(e);});
			//div.childElements()[0].observe('mouseover', glissGalleryChangeZoom);
		});
	}
}

function glissGalleryDisablePrevious(){
		//Local vars init
	var regExec;

	if(glissGallery_params.previousEnable){
		regExec = glissGallery_params.galleryRegExp.exec(glissGallery_gallery.previous.getAttribute('src'));

		glissGallery_gallery.previous.setStyle({'cursor':'default'});
		glissGallery_gallery.previous.setAttribute('src', regExec[1] + regExec[3]);
		glissGallery_gallery.previous.stopObserving('click');
		//glissGallery_gallery.previous.stopObserving('mouseover');
	}

	glissGallery_params.previousEnable = false;
}

function glissGalleryEnablePrevious(){
		//Local vars init
	var regExec;

	if(!glissGallery_params.previousEnable){
		regExec = glissGallery_params.galleryRegExp.exec(glissGallery_gallery.previous.getAttribute('src'));

		glissGallery_gallery.previous.setStyle({'cursor':'pointer'});
		glissGallery_gallery.previous.setAttribute('src', regExec[1] + '_enable' + regExec[3]);
		glissGallery_gallery.previous.observe('click', function(event){glissGallerySlidePrevious();});
		//glissGallery_gallery.previous.observe('click', function(e){Event.stop(e);});
		//glissGallery_gallery.previous.observe('mouseover', function(event){glissGallerySlidePrevious();});
	}

	glissGallery_params.previousEnable = true;
}

function glissGalleryDisableNext(){
		//Local vars init
	var regExec;

	if(glissGallery_params.nextEnable){
		regExec = glissGallery_params.galleryRegExp.exec(glissGallery_gallery.next.getAttribute('src'));
	
		glissGallery_gallery.next.setStyle({'cursor':'default'});
		glissGallery_gallery.next.setAttribute('src',regExec[1] + regExec[3]);
		glissGallery_gallery.next.stopObserving('click');
		//glissGallery_gallery.next.stopObserving('mouseover');
	}

	glissGallery_params.nextEnable = false;
}

function glissGalleryEnableNext(){
		//Local vars init
	var regExec;

	if(!glissGallery_params.nextEnable){
		regExec = glissGallery_params.galleryRegExp.exec(glissGallery_gallery.next.getAttribute('src'));

		glissGallery_gallery.next.setStyle({'cursor':'pointer'});
		glissGallery_gallery.next.setAttribute('src', regExec[1] + '_enable' + regExec[3]);
		glissGallery_gallery.next.observe('click', function(event){glissGallerySlideNext();});
		//glissGallery_gallery.next.observe('click', function(e){Event.stop(e);});
		//glissGallery_gallery.next.observe('mouseover', function(event){glissGallerySlideNext();});
	}

	glissGallery_params.nextEnable = true;
}

function glissGallerySlidePrevious(px){
		//Local var init
	var marginLeft = parseInt(glissGallery_gallery.elements.getStyle('marginLeft'));
	var limit, maxSlide;

	if((glissGallery_gallery.elements.slideNext == null && glissGallery_gallery.elements.slidePrevious == null && px == null) || (glissGallery_gallery.elements.slideNext == null && glissGallery_gallery.elements.slidePrevious != null && px != null)){
		maxSlide = (glissGallery_params.elementCount - 3) * -glissGallery_params.elementWidth;
		limit = px == null ? marginLeft + glissGallery_params.elementWidth : px;

		if(marginLeft < limit){
			glissGallery_gallery.elements.setStyle({'marginLeft':marginLeft + 5 + 'px'});
			glissGallery_gallery.elements.slidePrevious = setTimeout(function(){glissGallerySlidePrevious(limit);}, 10);
		}else{
			if(marginLeft >= 0){
				glissGallery_gallery.elements.setStyle({'marginLeft':'0px'});
				glissGalleryDisablePrevious();
			}else{
				glissGallery_gallery.elements.setStyle({'marginLeft':limit + 'px'});
			}
			glissGallery_gallery.elements.slidePrevious = null;
		}

		if(marginLeft > maxSlide){
			glissGalleryEnableNext();
		}
	}
}

function glissGallerySlideNext(px){
		//Local var init
	var marginLeft = parseInt(glissGallery_gallery.elements.getStyle('marginLeft'));
	var limit, maxSlide;

	if((glissGallery_gallery.elements.slidePrevious == null && glissGallery_gallery.elements.slideNext == null && px == null) || (glissGallery_gallery.elements.slidePrevious == null && glissGallery_gallery.elements.slideNext != null && px != null)){
		maxSlide = (glissGallery_params.elementCount - 3) * -glissGallery_params.elementWidth;
		limit = px == null ? marginLeft - glissGallery_params.elementWidth : px;

		if(marginLeft > limit){
			glissGallery_gallery.elements.setStyle({'marginLeft':marginLeft - 5 + 'px'});
			glissGallery_gallery.elements.slideNext = setTimeout(function(){glissGallerySlideNext(limit);}, 10);
		}else{
			if(marginLeft <= maxSlide){
				glissGallery_gallery.elements.setStyle({'marginLeft':maxSlide + 'px'});
				glissGalleryDisableNext();
			}else{
				glissGallery_gallery.elements.setStyle({'marginLeft':limit + 'px'});
			}
			glissGallery_gallery.elements.slideNext = null;
		}

		if(marginLeft < 0){
			glissGalleryEnablePrevious();
		}
	}
}

function glissGalleryChangeZoom(event){
		//Stop event
	Event.stop(event);
	event.preventDefault();

		//Local vars init
	var regExecGallery, ending;
	ending = event.element().getAttribute('alt').substr(event.element().getAttribute('alt').length - 3);
	regExecGallery = glissGallery_params.zoomRegExp.exec(event.element().src);

	if($('visuelPrincipal') != null){
		MagicZoom.stop($('visuelPrincipal'));
		$('visuelPrincipal').setAttribute('href', regExecGallery[1] + regExecGallery[2] + regExecGallery[3] + regExecGallery[4] + regExecGallery[5] + 'original' + regExecGallery[6]);
		$('visuelPrincipal').childElements()[0].src = regExecGallery[1] + regExecGallery[2] + regExecGallery[3] + regExecGallery[4] + regExecGallery[5] + 'fiche' + regExecGallery[6];
		MagicZoom.start($('visuelPrincipal'));
	}

	if(ending == '__1'){
		MagicZoom.stop($('visuelPrincipal_1'));
		$('visuelPrincipal_1').setAttribute('href', regExecGallery[1] + regExecGallery[2] + regExecGallery[3] + regExecGallery[4] + regExecGallery[5] + 'original' + regExecGallery[6]);
		$('visuelPrincipal_1').childElements()[0].src = regExecGallery[1] + regExecGallery[2] + regExecGallery[3] + regExecGallery[4] + regExecGallery[5] + 'fiche' + regExecGallery[6];
		MagicZoom.start($('visuelPrincipal_1'));
	}

	if(ending == '__2'){
		MagicZoom.stop($('visuelPrincipal_2'));
		$('visuelPrincipal_2').setAttribute('href', regExecGallery[1] + regExecGallery[2] + regExecGallery[3] + regExecGallery[4] + regExecGallery[5] + 'original' + regExecGallery[6]);
		$('visuelPrincipal_2').childElements()[0].src = regExecGallery[1] + regExecGallery[2] + regExecGallery[3] + regExecGallery[4] + regExecGallery[5] + 'fiche_mini' + regExecGallery[6];
		MagicZoom.start($('visuelPrincipal_2'));
	}
}