var Ticker = {
	
	leftPos: 0,
	marginLeft: 15,
	speed: 30, /* Higher = slower */
	step: 1,
	timeout: null,
	tickerList: null,
	randomStart: true,
	stopped: false,
	initDelay: 500,

	init: function() {
		Ticker.tickerList = document.getElementById('tickerlist');
		var totalLength = 0;
		for(var i = 0; i < Ticker.tickerList.children.length; i++) {
			var c = Ticker.tickerList.children[i];
			totalLength += c.offsetWidth;
			c.style.marginLeft = Ticker.marginLeft + 'px';
			c.setAttribute('onmouseover', 'Ticker.stop(this)');
			c.setAttribute('onmouseout', 'Ticker.restart(this)');
		}
		Ticker.tickerList.style.width = totalLength + (Ticker.tickerList.children.length * Ticker.marginLeft) + 'px';
		
		if(Ticker.randomStart) {
			var rnd = Math.floor(Math.random() * Ticker.tickerList.children.length);
			for(; rnd > 0; rnd--) {
				Ticker.tickerList.appendChild(Ticker.tickerList.children[0]);
			}
		} else {
			Ticker.leftPos = Math.ceil(document.getElementById('ticker').offsetWidth / 3);
			Ticker.tickerList.style.left = Ticker.leftPos + 'px';
		}
		
		setTimeout(function() {
			Ticker.timeout = setInterval(Ticker.doStep, Ticker.speed);
		}, Ticker.initDelay);
		
	},
	
	doStep: function() {
		if(Ticker.stopped) {
			return;
		}
		Ticker.leftPos -= Ticker.step;
		if(Ticker.tickerList.children[0].offsetWidth + Ticker.marginLeft + Ticker.leftPos < 0) {
			Ticker.leftPos += Ticker.tickerList.children[0].offsetWidth  + Ticker.marginLeft;
			Ticker.tickerList.appendChild(Ticker.tickerList.children[0]);
		}
		Ticker.tickerList.style.left = Ticker.leftPos + 'px';
	},
	
	stop: function(el) {
		Ticker.stopped = true;
	},
	
	restart: function(el) {
		Ticker.stopped = false;
	}
};

window.onload = Ticker.init;

