var intTotalSX = 0;
var objLastItem = 0;
var intTimeMove = 5000;
var intCurrentFirst = 0;
var intScSid = -1;

$(
	function()
	{
		if ($("div.cssScrollPane1").length > 0 )
		{
			$("div.cssScrollPane1").parent().css('overflow;hidden');
			$("div.cssScrollPane1").css('position' , 'absolute');
			
			//alert($("div.cssMasterPanel").offset().left);
			if ($.browser.msie)
				$("div.cssScrollPane1").css('left' , '0');
			$("div.cssScrollPane1").css('width' , '988px');
			$("div.cssScrollPane1").css('height' , '108px');
			$("div.cssScrollPane1").css('overflow' , 'hidden');
			//$("div.cssScrollPane").css('margin-left' , '-170px');
			
			intCurrentFirst = 0;
			intTotalSX = 2;
			$("div.cssScrollPane1").find("div").each(
				function(i)
				{
					$(this).css('position' , 'absolute');
					$(this).css('left' ,intTotalSX); // $(this).parent().offset().left + 
					$(this).css('top' , '1px');//$(this).parent().offset().top + 2 );
					intTotalSX += $(this).width() + 2; 
					
					
					objLastItem = $(this);
					
					
					/*
					//$(this).parent().offset().left - 
					$(this).animate(
						{
							left : -($(this).width() + 2 )
							
						} , (i+1) * intTimeMove  , 'linear' , function()
						{
							//stopAll($(this));
							moveNow($(this));
						}
					);
					*/
					
					$(this).mouseover(
						function()
						{
							$(this).css('cursor' , 'pointer');
							stopAll($(this));
						}
					);
					
					$(this).mouseout(
						function()
						{
							resumeAll($(this));
						}
					);
					
					$(this).click(
						function()
						{
							//alert("Clicked");
						}
					);
				}
			);
			clearInterval(intScSid);
			//intScSid = setInterval( "ST_movenow()" , 100 );
			//ST_movenow();
		}
	}
);

function stopAll(objItem)
{
	if (intScrollNow == 1)
	{
	clearInterval(intScSid);
	intScSid = setInterval( "ST_movenow()" , 100 );
	//objItem.parent().find("div").stop();
	}
}

function resumeAll(objItem)
{
	if (intScrollNow == 1)
	{
	clearInterval(intScSid);
	}
	//intScSid = setInterval( "ST_movenow()" , 100 );
	/*
	var intF = (intCurrentFirst + 1) % objItem.parent().find("div").length ;
	var objF = objItem.parent().find("div:eq("+(intF-1)+")");
	var intX = objF.offset().left;
	//alert((objItem.parent().offset().left - intX)+" "+objF.width() );
	var intDistance = (1 - Math.abs(intX / - objF.width()));
	//intDistance = 1;
	objItem.parent().find("div").each(
		function(i)
		{
			//alert(intF+" "+(i+1));
			if (intF <= i+1 )
				j = (i+1 - intF + intDistance )* intTimeMove;
			else
				j = ((objItem.parent().find("div").length - intF + 1) + i + intDistance )* intTimeMove ;
			
			//alert(intDistance+" "+j);
			//alert(((i+1) - intF + intDistance ));
			//alert( i+1 - intF );
			//alert($(this).html());
				
			$(this).animate(
				{
					left : -($(this).width() + 2 )
						
				} , j  , 'linear' , function()
				{
					//stopAll($(this));
					moveNow($(this));
				}
			);
			
		}
	);
	*/
}

function moveNow(objItem)
{
	//alert(objLastItem.html());
	intCurrentFirst++;
	objItem.css('left' , intTotalSX - objLastItem.width());// + objLastItem.offset().left + 2 );
	objLastItem = objItem;
	
	//objItem.parent().offset().left
	objItem.animate(
		{
			left :  - (objItem.width() + 2 )
				
		} , (objItem.parent().find("div").length) * intTimeMove  , 'linear' , function()
		{
			moveNow($(this));
		}
	);
	
}

function ST_movenow()
{
	
	//intX = 0;
	$("div.cssScrollPane1").find("div").each(
		function(i)
		{
			var intStep = 12;//$(this).width() / 5;		
			var intOL = parseInt($(this).css('left'));
			//alert(intStep+" "+ $(this).offset().left);
			var intT = intOL  - intStep;
			
			//alert(intT);
			
			if (i == 0)
				$("#z").html(intOL);
			
			if (intT - 2 <  -$(this).width() * 2)
			{
				//$(this).css('left',intTotalSX - objLastItem.width() * 2 - $("div.cssScrollPane").offset().left );
				//$(this).css('left', 2 + ( 170 + 2 ) * ($("div.cssScrollPane").find("div").length - 2) );
				
				var intOL = parseInt(objLastItem.css('left'));
				$(this).css('left' , intOL + 2 + objLastItem.width());
				
				objLastItem = $(this);
			}
			else
				$(this).css('left',intT);
		}
	);
}

