﻿/*
News ticker plugin , type writer style
Markus Auer ,2007
version 1.0

Options (defaults shown):
newsList: "#news" 	// assumes unordered list; specify the ul holding the news items
tickerRate: 80 		// time gap between display of each letter (ms)
startDelay: 100 	// delay before first run of the ticker (ms)
loopDelay: 3000 	// time for which full text of each item is shown at end of print-out (ms)
cursorChars: ['', '_']	// character placeholder shown on even loops

Sample usage:
$(document).ready(function() {
	var options = {
  		newsList: "#news",
 		startDelay: 10,
 		placeHolder1: " []"
	}
	$('#news').newsTicker(options);
});

for markup as follows:

<ul id="news">
<li><a href="http://www.makemineatriple.com">MakeMineATriple.com</a></li>
<li><a href="http://www.jquery.com">jQuery</a></li>
</ul>

Underline text decoration on the link is not recommended! :-)

*/



(function($) {
	
	$.fn.typeWriter = function(settings) {
		settings = jQuery.extend({
						tickerRate: 80,
						startDelay: 100,
						loopDelay: 3000,
						cursorChars: ['', '_']
					}, settings, { currentCursor: 0 });
		return this.each(function() {
			var list=$(this);
			list.find('li').each(function(){
	  		    $(this).append('<a href="javascript:;"></a><span style="visibility:hidden;">'+$(this).text()+'</span>');
			});
			list.find('li a').empty();
			list.find('li:not(:first)').hide();
			setTimeout(function(){runTypeWriter(list,settings); settings = null;},settings.startDelay);			  
		});
	};

	function runTypeWriter(list,settings) {
		var li=list.find('li:visible');
		var hide=li.find('span:eq(0)');
		var link=li.find('a:eq(0)');
		if(hide.text().length==0) {
			li=li.hide().next();
			if (!li[0]) li=list.find('li:first');
			li.show();
			hide.text(link.text());
			link.empty();
	 		hide=li.find('span:eq(0)');
			link=li.find('a');
		}
		var cursor=li.find('a:eq(1)');
		var text=hide.text();
		hide.text(text.substring(1));
		link.append(text.substring(0,1));
		if(hide.text().length>0) {
			cursor.text(settings.cursorChars[settings.currentCursor]);
			settings.currentCursor=(settings.currentCursor+1)%settings.cursorChars.length;
			setTimeout(function(){runTypeWriter(list,settings); settings = null;},settings.tickerRate);	
		} else {
			cursor.empty();
			setTimeout(function(){runTypeWriter(list,settings); settings = null;},settings.loopDelay);	
		}
	}

})(jQuery);


