/**
* ticker.js
* Made for Girlports project.
* Fetches data, and displays it in a scroll.
* Needs a div id=ticker in your html somewhere.
*/
var $ticker = YAHOO.namespace('GirlPorts.Ticker');

$ticker = function () {
	
	//To avoid timing issues, we make ticker a func, and exec it in build, 
	//at which point, it becomes an el.
	var tickerWindow = function() { tickerWindow = document.getElementById('tickerwindow') };

	//Current number of tries
	var tries = 0;
	//How many times to try to get data. On failure, will hide the ticker
	var tryLimit = 3
	//Speed - currently, px/sec
	var scrollSpeed = 30;
	
	return {

		data : {},

		init: function () {
			$ticker.loadData();
		},
		
		//Build the ticker, based on browser
		build : function () {
			
			$ticker.data = '<div style="display:inline;" id="tickerPadFront">&nbsp</div>'+ $ticker.data +'<div style="display:inline;" id="tickerPadBack">&nbsp</div>';
			
			document.getElementById('ticker').innerHTML += function() {
				if(YAHOO.env.ua.ie > 0) {
					//IE needs it this way. Saf, FF, Op are all good with the table.
					return '<div style="overflow:hidden;" class="tickerMask" id="tickerwindow"><div nowrap="nowrap" style="width:100%;"><span id="tickerData" width="100%">'+$ticker.data+'</span></div>';
				} else {
					return '<div class="tickerMask" id="tickerwindow"><table id="tickertable"><tr><td nowrap>'+$ticker.data+'</td></tr></table></div>';
				} 				
			}();
			
			//Init the ticker window element
			tickerWindow();
			
			//Set the Pad Element width based on tickerwindow width.
			$D.setStyle('tickerPadFront', 'padding-right', $D.getStyle(tickerWindow, 'width')); 
			$D.setStyle('tickerPadBack', 'padding-right', $D.getStyle(tickerWindow, 'width')); 
			//Start the scroll
			$ticker.scroll();
		},

		loadData : function() {
				YAHOO.util.Connect.asyncRequest('GET', 'action/get_ticker_data/', {  
				success : function(o) {					
					if(o.responseText !== undefined){
						//Using eval instead of $T.parseJSON because of safari crasher when it comes to regex to clean json. No real ned to sanitize
						//since in theory, this is trusted data.
						var response = eval(o.responseText);						
						$ticker.data = response;
						$ticker.build();
					}
				},

				failure : function(o) {
					if(o.responseText !== undefined){
						if(tries < tryLimit) {
							tries++;
							$ticker.loadData();
						} else {
							$D.setStyle('ticker', 'display', 'none');
						}
					}				
				}
			 });
		},

		scroll : function() {			
			var scrollDistance = tickerWindow.scrollWidth - parseInt($D.getStyle(tickerWindow, 'width').replace('px', ''));
			//how many px/sec
			var tickerTime = scrollDistance / scrollSpeed;
			var attributes = {
		        scroll: { to: [scrollDistance,0] }
		    };
		
			var toScroll = new YAHOO.util.Scroll(tickerWindow, attributes,tickerTime);

			toScroll.onComplete.subscribe( 
				function() {
					tickerWindow.scrollLeft=0;
					toScroll.animate();	
				}
			);
			
			toScroll.animate();	
		}
		
	};
	
} ();

/*
 * Load Init script once the container is ready
 */

YAHOO.util.Event.onContentReady('ticker', $ticker.init);