var interval = 25;

var lowSpeed = 1;
var midSpeed = 3;
var highSpeed = 5;

var padding = 10;

var divGallery = null;
var galleryWidth = 0;
var galleryHeight = 0;
var galleryX = 0;
var galleryY = 0;


var hasGallery = true;

var divGalleryPictures = null;

var thePictures = Array ();
var nbPictures = 0;
var thePicturesSize = Array ();
var thePictureTotalSize = 0;


// set the orientation. default horizontal i.e. vert= false
var vert = false;

// MOUSE COORDINATES
// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
var IE = document.all ? true:false
var IE6 = (navigator.userAgent.indexOf ("MSIE 6.0") != -1);

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)

// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;

// Variables to hold mouse x-y pos.s
var mX = 0
var mY = 0

// Main function to retrieve mouse x-y pos.s
function getMouseXY (e) {
	if (IE) { // grab the x-y pos.s if browser is IE
		mX = event.clientX + document.body.scrollLeft;
		mY = event.clientY + document.documentElement.scrollTop;
	} else {  // grab the x-y pos.s if browser is NS
		mX = e.pageX;
		mY = e.pageY;
	}  
	// catch possible negative values in NS4
	if (mX < 0){mX = 0}
	if (mY < 0){mY = 0}  
	return true;
}



function setPositions (firstPicture, firstPosition)
{
	
	if (!document.getElementById)
		return;
		
	if (!hasGallery)
		return
		
	if (!divGalleryPictures)
		if (document.getElementById ("picturesGalleryPictures"))
		{
			document.getElementById ("picturesGallery").style.display = "block";
			
			divGalleryPictures = document.getElementById ("picturesGalleryPictures");
			thePictures = divGalleryPictures.getElementsByTagName("a");
			nbPictures = thePictures.length;
			
			for (i=0; i<nbPictures; i++)
			{
				thePictures[i].getElementsByTagName("img")[0].selSize = vert ? thePictures[i].getElementsByTagName("img")[0].height : thePictures[i].getElementsByTagName("img")[0].width
				thePicturesSize [i] = thePictures[i].getElementsByTagName("img")[0].selSize;
				thePictureTotalSize += thePicturesSize [i];
			}
			
			
		}
		else
		{
			hasGallery = false;
			return;
		}

		
	if (firstPicture == undefined)
		firstPicture = 0;
		
	if (firstPosition == undefined)
		firstPosition = 0;
		
	//window.status = findPos (document.getElementById ("picturesGallery", true)) + ", Mouse: " + mX + ", " +mY;	//firstPicture +", " + firstPosition;

	actualPosition = firstPosition;
	
	
	
	for (i = firstPicture; i < nbPictures + firstPicture; i++)
	{
		j = i % nbPictures;
		
		if (i == firstPicture + 1)
			secondPosition = actualPosition;
			
		if (vert)
			thePictures[j].style.top = actualPosition+"px";
		else
			thePictures[j].style.left = actualPosition+"px";
		
		actualPosition += thePicturesSize[j] + padding;
	}
	
	direction = getSpeed () / Math.abs (getSpeed());
	
	if (firstPosition > 0 )
	{
		firstPicture = (nbPictures + firstPicture - direction ) % nbPictures;
			
		firstPosition -= padding + thePicturesSize [firstPicture];
	}
	if (secondPosition < 0 )
	{
		firstPicture = (nbPictures + firstPicture - direction ) % nbPictures;
			
		firstPosition = secondPosition;
	}
	
	
	firstPosition += getSpeed ();
	
	setTimeout ("setPositions ("+firstPicture+", "+firstPosition+")", interval);

}

function getSpeed ()
{
	if (!divGallery)
	{
		divGallery = document.getElementById ("picturesGallery");
		
		galleryWidth = divGallery.offsetWidth;
		galleryHeight = divGallery.offsetHeight;
		galleryX = findPos (divGallery)[0];
		galleryY = findPos (divGallery)[1];
	}

	var gallerySize = vert	?	galleryHeight	:	galleryWidth;

	if ( gallerySize > thePictureTotalSize )
		return 0;
		
	speed = lowSpeed
	
	//window.status = secondPosition; //(mY - galleryY) +" >= 0  && " + (mY - galleryY) +" <= " + galleryHeight +" && " + (mX - galleryX) + " >= 0 && " + (mX - galleryX) +" <= " + gallerySize;
	
	if (mY - galleryY >= 0  && mY - galleryY <= galleryHeight && mX - galleryX >= 0 && mX - galleryX <= galleryWidth)
	{
	
		var mCoord 			= vert ? mY 		: mX;
		var galleryCoord 	= vert ? galleryY 	: galleryX;
		
		//window.status = "inside";
		switch (true)
		{
			case (mCoord - galleryCoord < gallerySize / 5):
				speed = highSpeed;
			break;
			case (mCoord - galleryCoord >= gallerySize / 5 && mCoord - galleryCoord < 2 * gallerySize / 5):
				speed = midSpeed;
			break;
			case (mCoord - galleryCoord >= 2 * gallerySize / 5 && mCoord - galleryCoord < 3 * gallerySize / 5):
				speed = 0;
			break;
			case (mCoord - galleryCoord >= 3 * gallerySize / 5 && mCoord - galleryCoord < 4 * gallerySize / 5):
				speed = -midSpeed;
			break;
			case (mCoord - galleryCoord >= 4 * gallerySize / 5):
				speed = -highSpeed;
			break;
		}
	}
	else
	{
		//window.status = "outside";
	}
	return speed;
} 


function findPos(obj, absolute) 
{
	if (absolute == undefined)
		absolute = false;
		
	var curleft = curtop = 0;
	if (obj.offsetParent)
		do 
		{
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
		} 
		while (obj = obj.offsetParent);

	if (!absolute)
		return [curleft, curtop];
	else
		return [curleft - pageXOffset, curtop - pageYOffset];
}


