	/************************************************************************************************************
	(C) www.dhtmlgoodies.com, November 2005
	
	This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.	
	
	Terms of use:
	You are free to use this script as long as the copyright message is kept intact. However, you may not
	redistribute, sell or repost it without our permission.
	
	Thank you!
	
	www.dhtmlgoodies.com
	Alf Magne Kalleland
	
	************************************************************************************************************/
	
	
	var SlideImageGalleryArray = new Array();
	var currentIdGallery = '';
	
	function setCurrentImageGallery(idGallery)
	{
     currentIdGallery = idGallery;
  }

	function getCurrentImageGallery(idGallery)
	{
  	if(idGallery != null)
  	  currentIdGallery = idGallery;
		//alert('sono in getCurrentImageGallery con ' + currentIdGallery);
		if( SlideImageGalleryArray[currentIdGallery] == null )
		{
			//alert('la creao per ' + currentIdGallery);
			var slideImageGallery = new SlideImageGallery(currentIdGallery);
			slideImageGallery.initGalleryScript();
			SlideImageGalleryArray[currentIdGallery] = slideImageGallery;
			slideImageGallery.slidePreviewPane();
		}		
		return SlideImageGalleryArray[currentIdGallery];
	}
	function getTopPos(inputObj)
	{		
	  var returnValue = inputObj.offsetTop;
	  while((inputObj = inputObj.offsetParent) != null)returnValue += inputObj.offsetTop;
	  return returnValue;
	}
	
	function getLeftPos(inputObj)
	{
	  var returnValue = inputObj.offsetLeft;
	  while((inputObj = inputObj.offsetParent) != null)returnValue += inputObj.offsetLeft;
	  return returnValue;
	}
	
function SlideImageGallery(idGallery)
{
	this.idGallery = idGallery;
	this.idPreviewPane = idGallery + '_previewPane';
	
	this.arrowImageHeight = 35;	// Height of arrow image in pixels;
	this.displayWaitMessage = false;	// Display a please wait message while images are loading?
	
	this.galleryContainer = null;
	this.previewImagePane = null;
	this.previewImage = null;
	this.imagesContainer = null;
	this.slideEndMarker = null;
	this.arrowUpImage = null;
	this.arrowDownImage = null;
	this.waitMessage = null;

	this.slideSpeed = 10;
	
	this.showPreview = function(newSrc)
	{
		if(!this.previewImage)
		{
			var images = document.getElementById(this.idPreviewPane).getElementsByTagName('IMG');
			if(images.length>0)
			{
				this.previewImage = images[0];
			}
			else
			{
				this.previewImage = document.createElement('IMG');
				document.getElementById(this.idPreviewPane).appendChild(this.previewImage);	
			}
		}
		if(this.displayWaitMessage)
		{
			this.waitMessage.style.display='inline';
		}
		
		this.previewImage.onload = function() {
			getCurrentImageGallery().hideWaitMessage();
			};
		this.previewImage.src = newSrc;		
	}
	
	this.hideWaitMessage = function()
	{
		this.waitMessage.style.display='none';	
	}	
	
	this.initSlide = function(e)
	{
  	var currentGallery = getCurrentImageGallery();
		if(document.all)e = event;
		if(this.src.indexOf('over')<0)this.src = this.src.replace('.gif','-over.gif');
		
		currentGallery.slideSpeed = e.clientY + Math.max(document.body.scrollTop,document.documentElement.scrollTop) - getTopPos(this);
		if(this.src.indexOf('down')>=0)
		{
			currentGallery.slideSpeed = (currentGallery.slideSpeed)*-1;	
		}
		else
		{
			currentGallery.slideSpeed = currentGallery.arrowImageHeight - currentGallery.slideSpeed;
		}
		currentGallery.slideSpeed = Math.round(currentGallery.slideSpeed * 10 / currentGallery.arrowImageHeight);
	}
	
	this.stopSlide = function()
	{		
  	var currentGallery = getCurrentImageGallery();
		currentGallery.slideSpeed = 0;
		this.src = this.src.replace('-over','');
	}
	
	this.slidePreviewPane = function()
	{
  	var currentGallery = getCurrentImageGallery();
		if(currentGallery.slideSpeed!=0)
		{
			var topPos = currentGallery.previewImagePane.style.top.replace(/[^\-0-9]/g,'')/1;	
		
			if(currentGallery.slideSpeed<0 && currentGallery.slideEndMarker.offsetTop<(currentGallery.imagesContainer.offsetHeight - topPos))
			{
				currentGallery.slideSpeed=0;
			}
			topPos = topPos + currentGallery.slideSpeed;
			if(topPos>0)
				topPos=0;

		 	currentGallery.previewImagePane.style.top = topPos + 'px';
	 	
		}
	 	setTimeout('getCurrentImageGallery().slidePreviewPane();',30);		
	}
	
	this.revealThumbnail = function()
	{
		this.style.filter = 'alpha(opacity=100)';
		this.style.opacity = 1;
	}
	
	this.hideThumbnail = function()
	{
		this.style.filter = 'alpha(opacity=70)';
		this.style.opacity = 0.7;
	}
	
	this.initGalleryScript = function()
	{
		this.galleryContainer = document.getElementById(idGallery + '_slideShowGalleryContainer');
		this.imagesContainer = document.getElementById(idGallery + '_imagesContainer');
		this.slideEndMarker = document.getElementById(idGallery + '_slideEnd');
		this.arrowUpImage = document.getElementById(idGallery + '_arrowUpImage');
		this.arrowDownImage = document.getElementById(idGallery + '_arrowDownImage');
		this.waitMessage = document.getElementById(idGallery + '_waitMessage');
		
		this.previewImagePane = this.imagesContainer.getElementsByTagName('DIV')[0];
		this.previewImagePane.style.top = '0px';
		var images = this.imagesContainer.getElementsByTagName('IMG');
		for(var no=0;no<images.length;no++)
		{
			images[no].onmouseover = this.revealThumbnail;
			images[no].onmouseout = this.hideThumbnail;
		}	
		
		this.arrowUpImage.onmousemove = this.initSlide;
		this.arrowUpImage.onmouseout = this.stopSlide;
		
		this.arrowDownImage.onmousemove = this.initSlide;
		this.arrowDownImage.onmouseout = this.stopSlide;
		
	}
}
