(function($){
	$.fn.tickertype = function(options){
		
		// Plsease note, that this function can only be used once with one element.
		// Any more than that, then the function will not work as expected.
		// This is due to the way that the setTimeout() function works.
		
		var isInTag = false; // A boolean value to indicate whether or not the current set of characters being output is part of an HTML tag.
		var rotationTime = 10000; // The time, in milliseconds, for the rotation of each list item.
		var typingTime = 20; // The time, in milliseconds, for the output of each character in each list item.
		var tickerCSSSelector = ""; // The CSS selector string that will identify the container element that is to hold the current list item.
		
		// Set the default options.
		var defaults = { debug: false, rotationTime: rotationTime, typingTime: typingTime };
		// Combine the default options with the customised options.
		var options = $.extend(defaults,options);
		
		// This function will rotate each list item.
		function rotateTicker(){
			if(i == tickerItems.length){ i = 0; }
			tickerText = tickerItems[i];
			c = 0;
			typetext();
			setTimeout(rotateTicker, rotationTime);
			i++;
		}
		
		// This function will output each character within the current list item, one at a time.
		function typetext(){	
			var thisChar = tickerText.substr(c, 1);
			if(thisChar == '<'){ isInTag = true; }
			if(thisChar == '>'){ isInTag = false; }
			$(tickerCSSSelector).html(tickerText.substr(0, c++));
			if(c < tickerText.length+1){
				if(isInTag){ typetext(); } else{ setTimeout(typetext, typingTime); }
			} 
			else { c = 1; tickerText = ""; }	
		}
		
		return this.each(function(){
			// Get all of the child elements (i.e the list items).
			var tickerLIs = $(this).children();
			// There must be at least one list item to continue.
			if(tickerLIs.length > 0){
				var prependText = "";
				// The CSS selector for the container element that is to hold each list item must be obtained 
				// and assigned to tickerCSSSelector so that the ticker knows where to append each character of the current list item.
				tickerCSSSelector = "#"+ $(this).parent().attr("id");
				// Set the relevant customised options.
				if(options.prependText && options.prependText != ""){ prependText = options.prependText; }
				if(options.rotationTime && options.rotationTime > 0){ rotationTime = options.rotationTime; }
				if(options.typingTime && options.typingTime > 0 ){ typingTime = options.typingTime; }
				// Append each list item to the tickerItems array.
				// The array will be used to rotate through each list item.
				tickerItems = new Array();
				tickerLIs.each(function(el){
					var txt = $(this).html();
					$(this).html(prependText +""+ txt);
					tickerItems.push($(this).html());	
				});
				// Start rotating the list items starting with the first on in the list.
				i = 0;
				rotateTicker();
			}
		});
	}
})(jQuery);
