// JavaScript Document



var Ticker = {
	fps:60,
	speed:1,
	con:'',
	inner:'',
	p:'',
	pwidth:'',
	clone:'',
	int:'',
	paused: false,
	
	refresh: function(){
		Ticker.con = $('#stock-strip');
		Ticker.inner = $('#stock-strip-inner');
		Ticker.p = Ticker.inner.children('p');
		Ticker.pwidth = Ticker.p.width() + 20;
		Ticker.clone = Ticker.p.clone();
	},
	
	getMax: function(){
		return Ticker.inner.children('p').length * Ticker.pwidth;
	},
	
	maxScroll: function(){
		return Ticker.con[0].scrollWidth - Ticker.con.width();
	},
	
	addAnother: function(){
		Ticker.inner.width( Ticker.getMax() + Ticker.pwidth );
		Ticker.inner.append( Ticker.clone.clone() );
	},
	
	scrollElement: function(){
		if( Ticker.paused ) return;
		var n = Ticker.con.scrollLeft() + Ticker.speed;
		Ticker.con.scrollLeft(n);
		
		if( n >= Ticker.maxScroll() ){
			Ticker.addAnother();
		}
	},

	init: function(){
		Ticker.refresh();
		window.interval = window.setInterval(Ticker.scrollElement, (1000/Ticker.fps) );
		Ticker.con.mouseover( function(){ Ticker.paused = true });
		Ticker.con.mouseout( function(){ Ticker.paused = false });
	}
	



}

/*
jQuery.fn.ticker = function(){

	var fps = arguments.length > 1 ? arguments[1] : 50;
	var speed = arguments.length > 0 ? arguments[0] : 1;	
	var con = $(this[0]);
	var inner = $('#stock-strip-inner');
	var p = inner.children('p');
	var pwidth = p.width() + 20;
	var clone = p.clone();
	
	
	function getMax(){
		return inner.children('p').length * pwidth;
	}
	
	function maxScroll(){
		return con[0].scrollWidth - con.width();
	}
	
	function addAnother(){
		inner.width( getMax() + pwidth);
		inner.append( clone.clone() );
	}
	
	addAnother();
		
	
	function scrollElement(){
		if( window.tickerPaused == 'yes' ) return;
		var n = con.scrollLeft() + speed;
		con.scrollLeft(n);
		
		if( n >= maxScroll() ){
			addAnother();
		}
	}
	
	
	
	
	window.interval = window.setInterval(scrollElement, (1000/fps) );
	//window.setInterval(scrollElement, 500);
	
	con.mouseover( function(){ $(this).attr('paused', 'yes'); });
	con.mouseout( function(){ $(this).attr('paused', 'no'); });
}

*/
$(document).ready( function(){
	Ticker.paused = true;
	Ticker.init();
	$('#stock-selector a').click(function(){
		if( $(this).hasClass('active') ) return;
		$('#stock-strip').html('<p>Loading...</p>');
		$('#stock-strip').load('/index.php/home/stocks/'+$(this).text(), null, function(){ Ticker.refresh(); Ticker.paused = false; } );
		$('#stock-selector a').removeClass('active');
		$(this).addClass('active');
		
	});
	$('#stock-selector a:first').click();
});




