var sRunning = 0;
var sLeft = true;
var sBoxWidth = 177;
var movement = sBoxWidth / 3;
var sBoxes = 1;
var scroller;
var sLArr;
var sRArr;

var idx = 0;

var it = false;

$(document).ready
(
	function() 
	{
		scroller = document.getElementById( "scroller" );
		sLArr = document.getElementById( "sLArr" );
		sRArr = document.getElementById( "sRArr" );
		
		if( scroller != null )
			setTimeout( autoScroll, 5000 );
	}
);

function autoScroll()
{
	if( !it )
	{
		moveDir(-1);
		setTimeout( autoScroll, 5000 );
	}
}

function sMoveLeft()
{
	it = true;
	idx--;
					
	var s = $(scroller);
				
	var a = null;
	
	do
	{
		var c = s.children("li");
		a = $(c[c.length-1]);
		a.remove();
	}
	while( a.hasClass("e") );

	a.prependTo(s);
	
	scroller.style.left = s.position().left - sBoxWidth  +"px";
	
	if( idx == -7 )
	{
		idx = 0;
	}
	
	moveDir( 1 );
}

function sMoveRight()
{
	it = true;
	moveDir( -1 );
}

function moveDir( dir )
{
	sLArr.src = "/site/img/left_arrow.png";		
	sRArr.src = "/site/img/right_arrow.png";

	if( sRunning )
		return;

	sRunning = sBoxWidth * sBoxes;

	sAnimate( dir );
}

function getLeft( o )
{
	var l = o.style.left;
	return l.substring(0, l.length - 2 );
}

// right = -1, left = 1
function sAnimate( dir )
{
	var keepGoing = true;
	
	if( sRunning > 0 )
	{
		var pos = $(scroller).position();
		scroller.style.left = ( pos.left + dir * movement ) + "px";
		sRunning -= movement;
	}
	else
	{
		keepGoing = false;
	}
		
	if( keepGoing )
		setTimeout( "sAnimate( " + dir + ")", 100 );
	else
	{
		sRunning = 0;
		
		if( dir < 0 )
		{			
			idx++;
							
			var s = $(scroller);
						
			var a = null;
			
			do
			{
				var c = s.children("li");
				a = $(c[0]);
				a.remove();
			}
			while( a.hasClass("e") );

			a.appendTo(s);
			
			if( idx == 7 )
			{
				idx = 0;
			}
			
			scroller.style.left = s.position().left + sBoxWidth  +"px";
		}
		else if( dir > 0 )
		{
			
		}
		
		/*if( dir < 0 && scroller.offsetLeft + scroller.offsetWidth - 750 <= 50 )
		{
			sRArr.src = "/site/img/stop_arrow.png";
		}
		else if( dir > 0 && scroller.offsetLeft >= 0 )
		{
			sLArr.src = "/site/img/stop_arrow.png";
		}*/
	}
}
