/************************************************
 
	tickertape of list items
	scrolls to the right or left
	
	version	: 00.01
	
	Required:
		- jquery 1.1.2 <
		- dimensions plugin
	
	TODO:
		- scroll up, down
		- pauze onHover
		- cookie position of ticker?
		- scroll left or right on mouse
		- multiple tickers on a page
	
	author:	Elja van Tol
	company: 7U	Digital Handmade Originals, www.7u.nl
	
************************************************/

var speed = -1; // geef negatieve waarde om naar links te scrollen
var speedOnHover = 10;
var interval = 20;

function move_tickerlist()
{
	// scrolling to the right
	if(  t_x >= 0 && g_x > 0 && t_x < g_x )
	{
		g_x = t_x - t_w ;
		//console.log(' set ghost to', g_x  );
	}
	else if(  g_x >= 0 && t_x > 0 && g_x < t_x )
	{
		t_x = g_x - t_w ;
		//console.log(' set org to', t_x  );
	}

	// scrolling to the left
	if( (t_x + t_w) <= box_w && g_x < box_w && t_x > g_x )
	{
		g_x = t_x + t_w ;
	}
	else if( (g_x + t_w) <= box_w && t_x < box_w && g_x > t_x )
	{
		t_x = g_x + t_w ;
	}

	
	t_x =  t_x + speed;
	$("#ticker").css("left", t_x + 'px' );

	g_x =  g_x + speed;
	$("#ticker_ghost").css("left", g_x + 'px' );
}


$(document).ready( function() 
{
	setTimeout( 'init_ticker()', 300 );//timeout nodig om images te laden
});

function init_ticker()
{
	
	box_x = $("#ticker_box").offset().left;
	box_w = $("#ticker_box").width();
	t_x = 0;//$("#ticker").offset().left  ;
	$("#ticker").css("left", t_x  );
	
	//dynamisch children tellen
	var tot_w = 0;
	$.each( $("#ticker").children("li"), function(i, n)
	{ // i is index
		tot_w += $(n).width();
	});
	
	$("#ticker").css("width",tot_w + 'px');
	t_w = tot_w ;
	
	
	//clone list items, zet ze achter het orgineel
	$("#ticker").after('<ul id="ticker_ghost" class="tickers"></ul>');
	$("#ticker").children().clone().appendTo( $("#ticker_ghost") );
	
	//bepaal positie ghost
	g_x = t_x - t_w ;
	$("#ticker_ghost").css("left", g_x );
	$("#ticker_ghost").css("width",tot_w + 'px');
	
	//console.log('start  :', t_x , box_w , box_x);
	//console.log('start g:', g_x , box_w , box_x);
	
	
	/* Pauze onHover */
	$("#sponsors_home .frame").hover(function()
	{
		clearInterval( t_interval );
	},function()
	{
		t_interval = setInterval( move_tickerlist, interval );
	});

	/* Scroll left */
	$(".btn_prev_sponsor a").hover(function()
	{
		speedNormal = speed;
		speed = - speedOnHover;
		
	},function()
	{
		speed = speedNormal;
	});	
	
	/* Scroll right */
	$(".btn_next_sponsor a").hover(function()
	{
		speedNormal = speed;
		speed = speedOnHover;
		
	},function()
	{
		speed = speedNormal;
	});	

	//start ticker
	t_interval = setInterval( move_tickerlist, interval );

}