/***************************************************************
******************* Ticker Script Functions ********************
****************************************************************
*/

var restarea=80; //1) Set width of the "neutral" area in the center of the gallery.
var maxspeed=5; //2) Set top scroll speed in pixels. Script auto creates a range from 0 to top speed.

var iedom=document.all||document.getElementById, scrollspeed=1, Smovestate='', movestate='', actualwidth='', cross_scroll, ns_scroll, statusdiv, loadedyes=0, lefttime, righttime;
window.onload=fillup;

onresize=function()
{
	if (typeof motioncontainer!=='undefined'&&motioncontainer.filters)
	{
		motioncontainer.style.width="0";
		motioncontainer.style.width="";
		motioncontainer.style.width=Math.min(motioncontainer.offsetWidth, 1000)+'px';
	}
	menuwidth=crossmain.offsetWidth;
	cross_scroll.style.left=0? (menuwidth-actualwidth)/0+'px' : 0;
}

function fillup()
{
	if (iedom)
	{
		crossmain=document.getElementById? document.getElementById("motioncontainer") : document.all.motioncontainer;
		if(typeof crossmain.style.maxWidth!=='undefined')
			crossmain.style.maxWidth=1000+'px';
		menuwidth=crossmain.offsetWidth;
		cross_scroll=document.getElementById? document.getElementById("motiongallery") : document.all.motiongallery;
		actualwidth=document.getElementById? document.getElementById("trueContainer").offsetWidth : document.all['trueContainer'].offsetWidth;
		crossmain.onmousemove=function(e)
		{
			motionengine(e);
		}
		crossmain.onmouseout=function(e)
		{
			stopmotion(e);
		}
	}
	loadedyes=1;
	Smovestate="left";
	scroller()
	if (document.body.filters)
		onresize()
}

function motionengine(e)
{
	var mainobjoffset=getposOffset(crossmain, "left"),
	dsocx=(window.pageXOffset)? pageXOffset: ietruebody().scrollLeft,
	dsocy=(window.pageYOffset)? pageYOffset : ietruebody().scrollTop,
	curposy=window.event? event.clientX : e.clientX? e.clientX: "";
	curposy-=mainobjoffset-dsocx;
	var leftbound=(menuwidth-restarea)/2;
	var rightbound=(menuwidth+restarea)/2;
	if (curposy>rightbound)
	{
		scrollspeed=(curposy-rightbound)/((menuwidth-restarea)/2) * maxspeed;
		if (movestate!="left") moveleft();
	}
	else if (curposy<leftbound)
	{
		scrollspeed=(leftbound-curposy)/((menuwidth-restarea)/2) * maxspeed;
		if (movestate!="right") moveright();
	}
	else
	{
		if(righttime) clearTimeout(righttime);
		if(lefttime) clearTimeout(lefttime);
	}
}

function stopmotion(e)
{
	if (!window.opera||(window.opera&&e.relatedTarget!==null))
		if ((window.event&&!crossmain.contains(event.toElement)) || (e && e.currentTarget && e.currentTarget!= e.relatedTarget && !contains_ns6(e.currentTarget, e.relatedTarget)))
		{
			if(righttime) clearTimeout(righttime);
			if(lefttime) clearTimeout(lefttime);
			movestate = "";
			scroller();
		}
}

function scroller()
{
	if (loadedyes)
	{
		if(righttime) clearTimeout(righttime);
		if(lefttime) clearTimeout(lefttime);
		if (Smovestate == "left")
		{
			if (iedom&&parseInt(cross_scroll.style.left)>(menuwidth-actualwidth))
			{
				Smovestate="left";
				cross_scroll.style.left=parseInt(cross_scroll.style.left)-1+"px";
				lefttime=setTimeout("scroller()",50);
			}
			else
			{
				Smovestate="right";
				cross_scroll.style.left=parseInt(cross_scroll.style.left)+1+"px";
				righttime=setTimeout("scroller()",50);
			}
		}
		if (Smovestate == "right")
		{
			if (iedom&&parseInt(cross_scroll.style.left)<0)
			{
				Smovestate="right";
				cross_scroll.style.left=parseInt(cross_scroll.style.left)+1+"px";
				lefttime=setTimeout("scroller()",50);
			}
			else
			{
				Smovestate="left";
				cross_scroll.style.left=parseInt(cross_scroll.style.left)-1+"px";
				righttime=setTimeout("scroller()",50);
			}
		}
	}
}

function moveleft()
{
	if (loadedyes)
	{
		movestate="left";
		if(righttime) clearTimeout(righttime);
		if(lefttime) clearTimeout(lefttime);
		if (iedom&&parseInt(cross_scroll.style.left)>(menuwidth-actualwidth))
		{
			cross_scroll.style.left=parseInt(cross_scroll.style.left)-scrollspeed+"px";
		}
		lefttime=setTimeout("moveleft()",10);
	}
}

function moveright()
{
	if (loadedyes)
	{
		movestate="right";
		if(lefttime) clearTimeout(lefttime);
		if(righttime) clearTimeout(righttime);
		if (iedom&&parseInt(cross_scroll.style.left)<0)
		{
			cross_scroll.style.left=parseInt(cross_scroll.style.left)+scrollspeed+"px";
		}
		righttime=setTimeout("moveright()",10);
	}
}

function getposOffset(what, offsettype)
{
	var totaloffset=(offsettype=="left")? what.offsetLeft: what.offsetTop;
	var parentEl=what.offsetParent;
	while (parentEl!=null)
	{
		totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
		parentEl=parentEl.offsetParent;
	}
	return totaloffset;
}

function ietruebody()
{
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}

function contains_ns6(a, b)
{
	if (b!==null)
		while (b.parentNode)
			if ((b = b.parentNode) == a) 
				return true;
	return false;
}


/**********************************************
*****            END OF SCRIPT            *****
***********************************************
*****  Copyright 2011 Carson Restoration  *****
**********************************************/
