// JavaScript Document by Pietro Saccardi
// Slide left/right content

	/* REQUIREMENTS:
	   the two slideObject with the ID passed to setup() must have style:
		 overflow:hidden;.
	*/

	//CONFIGURABLE TIME PRECISION	
	var slidePeriod=20;
	//CENTRAL AREA WITH SPEED=0
	var neutralArea=0.25;
	//SPEED IN/DECREASING FACTOR
	var fullSpeed=0.05;
	
	//Main function
	var slideContainer=null;
	var slideObject=null;
	var slideContainerW=0;
	var slideObjectW=0;
	var currentSpeed=0;
	var currentPosition=0;
	var slideTimer=null;
	//Invoked for initializing the slideObjects
	function setup(cont,obj) {
		slideContainer=document.getElementById(cont);
		slideObject=document.getElementById(obj);
		if(!(slideContainer&&slideObject)) return 0;
		slideContainerW=slideContainer.clientWidth;
		slideObjectW=slideObject.scrollWidth;
		slideContainer.onmousemove=defineSpeed;
		slideContainer.onmouseover=function(){
			if(!slideTimer) slideTimer=setInterval('moveInternal()', slidePeriod);
		};
		slideContainer.onmouseout=function(){
			if(slideTimer) {
				clearInterval(slideTimer);
				slideTimer=0;
			}
		};
		//Set also basic position (middle)
		setPosition(0);
	}
	//Evaluates the speed on Mouse Move
	function defineSpeed(e) {
		if(!e) { e=window.event; }
		//Obtain effective x pos relative to slideContainer, basing on the only shared property between browsers, clientX
		var speed = e.clientX-findPos(slideContainer)[0];
		//Subtract to the middle to obtain absolute speed
		speed -= (slideContainerW/2);
		//Apply neutral area
		if(speed>0) {
			speed = Math.max(0, speed-slideContainerW*(neutralArea/2));
		} else {
			speed = Math.min(0, speed+slideContainerW*(neutralArea/2));
		}
		//And apply speed reduction
		speed*=fullSpeed;
		sign=speed/Math.abs(speed);
		currentSpeed=speed*(speed/2)*sign;
	}
	//Evaluates and set a position basing on its middle offset
	function setPosition(middleOffset) {
		//Evaluate effective offset
		var effectiveOffset=-((slideObjectW/2)+middleOffset-(slideContainerW/2));
		//Width is offset+slideContainerW
		slideObject.style.left=effectiveOffset+"px";
		effectiveOffset=slideContainerW-effectiveOffset;
		slideObject.style.width=effectiveOffset+"px";
		currentPosition=middleOffset;
	}
	//Responds to slideTimer event for evaluating the new position
	function moveInternal() {
		//Check speed..! trust me, it's right
		if((currentSpeed<0&&currentPosition>((slideContainerW-slideObjectW)/2))||(currentSpeed>0&&currentPosition<((slideObjectW-slideContainerW)/2))) {
			//And now correct it with offset
			var newOffset=currentPosition+currentSpeed;
			if(currentSpeed<0) {
				//Check with the minimum
				newOffset=Math.max(newOffset, (slideContainerW-slideObjectW)/2);
			} else {
				//Check with the minimum
				newOffset=Math.min(newOffset, (slideObjectW-slideContainerW)/2);
			}
			setPosition(newOffset);
		}
	}
	//Find the position and the effective size of an slideObject
	function findPos(obj) {
		//Init vars
		var curleft = curtop = 0;
		var width=obj.offsetWidth;
		var height=obj.offsetHeight;
		//Check if it has parent
		if (obj.offsetParent) {
			//Loop and add coords
			do {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
				//Assign and check
			} while (obj = obj.offsetParent);
			//Return coords
			return [curleft,curtop,width,height];
		} else {
			return [obj.offsetLeft,obj.offsetTop,width,height];
		}
	}

