// e4eTicker v1.0 // &copy; Stuart Elmore (c/o Bluestone Creative) // 09/02/2010

$.fn.e4eTicker = function(){
	if($(this).length === 0) return;
	var thisTicker = this, options = arguments[0] || {};
	this.defaults = {
		pauseTime: 5000,
		speed: 25,
		loading: false,
		preHTML: '',
		postHTML: ''
	};
	this.tickerElem = $(this);
	this.dataArr = [];
	this.tickerCounter = 0;
	this.tickerChar = 0;
	this.partHTML = false;
	$.extend(this.defaults, options),
	this.tickerElem.children('li').each(function(){
		thisTicker.dataArr.push($(this).html());
	});
	this.tickerRotator = function(){
		thisTicker.tickerElem.html($('<li />').append('&nbsp'));
		if(thisTicker.tickerCounter == thisTicker.dataArr.length) thisTicker.tickerCounter = 0;
		thisTicker.tickerChar = 0;
		thisTicker.writeOut(thisTicker.dataArr[thisTicker.tickerCounter]);
	};
	this.writeOut = function(dataString){
		var writeChar = dataString.substr(thisTicker.tickerChar, 1);
		if(writeChar == '<'){thisTicker.partHTML = true;}else if(writeChar == '>'){thisTicker.partHTML = false;}
		$('li',thisTicker.tickerElem).html((thisTicker.defaults.preHTML == '' ? '&nbsp;' : '') + thisTicker.defaults.preHTML + dataString.substr(0, thisTicker.tickerChar++) + (thisTicker.tickerChar < dataString.length ? (thisTicker.defaults.loading !== false ? thisTicker.defaults.loading : '') : '') + thisTicker.defaults.postHTML);
		if(thisTicker.tickerChar < (dataString.length + 1)){
			(thisTicker.partHTML === true ?  
				thisTicker.writeOut(dataString)
			: 
				window.setTimeout(function(){
					thisTicker.writeOut(dataString);
				}, thisTicker.defaults.speed)
			)
		}else{
			thisTicker.tickerChar = 0;
			thisTicker.tickerCounter++;
			window.setTimeout(function(){
				thisTicker.tickerRotator();
			},thisTicker.defaults.pauseTime);
		}
	};
	this.tickerRotator();
};


