/***********************************************************************
 *								       *
 * mouseX, mouseY		Coordinates of the mouse	       *
 *								       *
 * getMousePos(e)		Updates mouseX, mouseY	 	       *
 * 	-> called everytime the user moves the mouse		       *
 *		(document.onmousemove = getMousePos)		       *
 * 								       *
 * [some extra function calls are added for different browsers]	       *
 *								       *
 ***********************************************************************/

// Variables
var mouseX, mouseY;

// Function
function getMousePos(e)
{
	if (!e)
	{
		e = window.event || window.Event;
	}

	if ('undefined' != typeof e.pageX)
	{
		mouseX = e.pageX;
	}
	else
	{
		mouseX = e.clientX + document.body.scrollLeft;
	}	
}

// Firefox-Fix
if (window.Event && document.captureEvents) 
{
	document.captureEvents(Event.MOUSEMOVE);
}

// Event Handles
document.onmousemove = getMousePos;

/***********************************************************************
 *								       *
 * findPosX(obj)	returns the X-coordinate of an elemant         *
 *								       *
 ***********************************************************************/
 
// Function
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (1) 
		{
			curleft += obj.offsetLeft;
			if (!obj.offsetParent)
			{
				break;
			}
			obj = obj.offsetParent;
		}
	}
    	else if (obj.x)
    	{
        	curleft += obj.x;
        }
    	return curleft;
}


/***********************************************************************
 *								       *
 * SCROLLSPEED		distance the element is moved everytime        *
 * SCROLLDELAY		delay between 2 movements		       *
 * SCROLLNEUTRAL	"neutral' zone in the middel where no events   *
 *			is called (and thus nothing happens)	       *
 * SCROLLWIDTH		width of the elements that is scrolled	       *
 * SCROLLFLAG		to scroll or not to scroll ?		       *
 *				-> boolean-flag set by hovering	       *
 *								       *
 * scroll(e)		does the actual moving, as long as SCROLLFLAG  *
 *			is true, it keeps looping with a delay of      *
 *			SCROLLDELAY				       *
 * startscroll()	set SCROLLFLAG to true and starts initial loop *
 * stopscroll()		set SCROLLFLAG to false			       *
 *								       *
 ***********************************************************************/

// Variables
var SCROLLSPEED 	= 5;
var SCROLLDELAY 	= 200;
var SCROLLNEUTRAL 	= 600;
var SCROLLWIDTH 	= 2604;

var DYNAMIC_ELEMENT 	= 'testarea_dynamic';
var STATIC_ELEMENT	= 'testarea_static';

var SCROLLFLAG 		= false;

// Functions
function scroll(e)
{	
	// test flag
	if (SCROLLFLAG)
	{
		// Get some values
		var dynamicX = findPosX(document.getElementById(DYNAMIC_ELEMENT));	// position dynamic-zone
		var staticX = findPosX(document.getElementById(STATIC_ELEMENT));	// position static-zone
		var staticW = document.getElementById(STATIC_ELEMENT).offsetWidth;   	// width static-zone
		
		// Calculate position of mouse "in" static zone compared to it's center
		tempX = (mouseX - staticX) - (staticW / 2); 

		// ignore a "neutral" zone in the middle
		if (tempX < 0-(SCROLLNEUTRAL / 2))
		{		
			// move to the right
			var newPos = dynamicX + SCROLLSPEED - staticX;
		}
		else if (tempX > (SCROLLNEUTRAL / 2))
		{
			// move to the left
			var newPos = dynamicX - SCROLLSPEED - staticX ;
		}

		// borders !!
		if (newPos > 0) 
		{
			newPos = 0;
		}
		if (newPos < (staticW - SCROLLWIDTH)) 
		{
			newPos = (staticW - SCROLLWIDTH);
		}
		
		// move it :)
		document.getElementById(DYNAMIC_ELEMENT).style.left = ''+newPos+'px';
		
		// start function again after a delay
		// note that if in the mean time the flag is set to false the function
		// will not be called again and therefore the loop will stop
		setTimeout('scroll()',SCROLLDELAY);
	}	
}

function startscroll(obj)
{
	SCROLLFLAG = true;
  DYNAMIC_ELEMENT = obj;
	scroll();
}
function stopscroll()
{
	SCROLLFLAG = false;
}





