	
/***********************************************************************************************
Copyright (c) 2005 - Alf Magne Kalleland post@dhtmlgoodies.com

Get this and other scripts at www.dhtmlgoodies.com	

You can use this script freely as long as this copyright message is kept intact.
***********************************************************************************************/ 
 	
var displayWaitMessage=true;	// Display a please wait message while images are loading?
var activeImage = false;
var imageGalleryLeftPos = false;
var imageGalleryWidth = false;
var imageGalleryObj = false;
var maxGalleryXPos = false;
var slideSpeed = 0;
var imageGalleryCaptions = new Array();
//variable added by Fabien 23.09.06
var imageClicked = null;
var speed = 30; 
var slideWidth = 500 // the slide width
var slideOrigin = 39 // the slide show origin
var limit = 0 + slideOrigin; // == the slide width


function getMouseX(e){
	if(document.all) e = event;
	return e.clientX;
}

function getImageWidth(imageSrc){
	var image = new Image;
	image.src = imageSrc;
	return image.width;
}

function initSlide()
{
	var id = this.id;
	this.getElementsByTagName('IMG')[0].src = 'typo3conf/ext/eco_gal/pi1/images/' + this.id + '_over.gif';
}

function startSlide(e)
{
	
	if(document.all)e = event;
	var id = this.id;
	if(this.id=='arrow_right'){
		var direction = 'right';
		var stopPosition = imageGalleryObj.offsetLeft - slideWidth;
	}
	else{
		var direction = 'left';
		var stopPosition = imageGalleryObj.offsetLeft + slideWidth;
	}
	translate(stopPosition,direction); //tells the limit
}

/* onmouse out on the arrow */
function releaseSlide()
{
	var id = this.id;
	this.getElementsByTagName('IMG')[0].src = 'typo3conf/ext/eco_gal/pi1/images/' + this.id + '.gif';
	//slideSpeed=0;
}

function releaseImage(){
	if(imageClicked != activeImage){//in fact it is the last image
		activeImage.style.filter = 'alpha(opacity=50)';	
		activeImage.style.opacity = '0.5';	
	}
}

/****************************************/
/******* TRANSLATE THE THUMBNAIL ********/
/****************************************/
function translate(stopPosition,direction){
		var increment = speed;
		if(direction == 'right') //change the direction
			increment = -increment;
		var leftPos = imageGalleryObj.offsetLeft/1 + increment; //new position
		if(maxGalleryXPos >= leftPos && minGalleryXPos <= leftPos){ //check the end of the gallery
			imageGalleryObj.style.left = leftPos + 'px';
			//stop recursive condition		
			if(direction == 'right'){
				if(stopPosition < imageGalleryObj.offsetLeft){
					setTimeout("translate("+ stopPosition +",'"+ direction +"')",20);
				}
			}
			else{
				if(stopPosition > imageGalleryObj.offsetLeft){
					setTimeout("translate("+ stopPosition +",'"+ direction +"')",20);
				}
			}
		}
}

/* onmouseover & onmouseout */
function showImage()
{
	if(activeImage){ // an image was memorize
		if(imageClicked != activeImage){//in fact it is the last image
			activeImage.style.filter = 'alpha(opacity=50)';	
			activeImage.style.opacity = '0.5';	
		}
	}
		this.style.filter = 'alpha(opacity=100)';
	this.style.opacity = '0.99';	//safari don't like 1
	activeImage = this;	//memorize the last image
}

/********************************/
/********* MAIN METHOD **********/
/********************************/
function initSlideShow()
{
	document.getElementById('arrow_left').onmouseover = initSlide;
	document.getElementById('arrow_left').onclick = startSlide;
	document.getElementById('arrow_left').onmouseout = releaseSlide;
	
	document.getElementById('arrow_right').onmouseover = initSlide;
	document.getElementById('arrow_right').onclick = startSlide;
	document.getElementById('arrow_right').onmouseout = releaseSlide;
	
	imageGalleryObj = document.getElementById('theImages');
	imageGalleryObj.onmouseout = releaseImage //release the opacity of the thumbnail
	imageGalleryLeftPos = imageGalleryObj.offsetLeft;
	imageGalleryWidth = document.getElementById('galleryContainer').offsetWidth - 80;
	maxGalleryXPos = imageGalleryObj.offsetLeft; 
	minGalleryXPos = imageGalleryWidth - document.getElementById('slideEnd').offsetLeft;
	var slideshowImages = imageGalleryObj.getElementsByTagName('IMG');
	for(var no=0;no<slideshowImages.length;no++){
		slideshowImages[no].onmousemove = showImage;
		slideshowImages[no].onmouseover = showImage;
	}
	
	var divs = imageGalleryObj.getElementsByTagName('DIV');
	for(var no=0;no<divs.length;no++){
		if(divs[no].className=='imageCaption')imageGalleryCaptions[imageGalleryCaptions.length] = divs[no].innerHTML;
	}
	echo('Gallery express inited');
}

/* display the nex image onclick, it is <a> */	
function showPreview(imagePath,imageIndex,el,event){
	var imgEl = el.getElementsByTagName('IMG')[0];
	if(imageClicked){ //it could be true on the on the second click
		if(imageClicked != imgEl){ //avoid a second click on the same image
			//the last image is now disabled
			imageClicked.style.filter = 'alpha(opacity=50)';	
			imageClicked.style.opacity = '0.5';
	
	 		//check if the picture was clicked left or right
	 		//COMMENT BY fabien : it's not the faster way but it works....
	 		//it could be much more smarter to interact with the imageIndex in an object approach
			if(getAbsoluteLeft(imageClicked) > getMouseX(event)){ //offsetLeft from the old image
				var direction = 'left';
				var pixelWidth = getImageWidth(imgEl.src); //it is the new image
				var stopPosition = imageGalleryObj.offsetLeft + pixelWidth
			}
			else{
				var direction = 'right';
				var pixelWidth = getImageWidth(imgEl.src);
				var stopPosition = imageGalleryObj.offsetLeft - pixelWidth
			}
 			translate(stopPosition,direction); //tells the limit
		}
	}
	else{ //the first time 
		var pixel = getImageWidth(imgEl.src);
		translate(imageGalleryObj.offsetLeft - pixel,'right'); //tells the limit		
	}
	imageClicked	= imgEl;
			
		var subImages = document.getElementById('previewPane').getElementsByTagName('IMG');
		if(subImages.length==0){
			var img = document.createElement('IMG');
			document.getElementById('previewPane').appendChild(img);
		}
		else 
			img = subImages[0];
		
		if(displayWaitMessage){ //tells if a displayWaitMessage is displaid or not
			document.getElementById('waitMessage').style.display='inline';
		}
		document.getElementById('largeImageCaption').style.display='none';
		img.onload = function() { hideWaitMessageAndShowCaption(imageIndex-1); };
		img.src = imagePath;
}

/* hide the wait message */	
function hideWaitMessageAndShowCaption(imageIndex)
{
	document.getElementById('waitMessage').style.display='none';	
	document.getElementById('largeImageCaption').innerHTML = imageGalleryCaptions[imageIndex];
	document.getElementById('largeImageCaption').style.display='block';
}

/********* UTIL *********/
function getAbsoluteLeft(o) {
	// Get an object top position from the upper left viewport corner
	// Tested with relative and nested objects
	oTop = o.offsetLeft;						// Get top position from the parent object
	while(o.offsetParent!=null) { // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent;	// Get parent object reference
		oTop += oParent.offsetLeft; // Add parent top position
		o = oParent;
	}
	// Return top position
	return oTop
}

function echo(data){
	try{
		dump(data+"\n");
	}
	catch(e){
		e = null;
	}
}
window.onload = initSlideShow;
