/*
 * liTicker 
 * Version: 1.1.0 (11/10/2010)
 * Based on liScroll by Gian Carlo Mingati
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 * Requires:
 * jQuery v1.4.x or later
 *
 */

jQuery.fn.liTicker = function(settings) {
	settings = jQuery.extend({
		width : "500px",
		padding : "5px",
		borderWidth : "1px",
		borderStyle : "solid",
		borderColor : "black",
		backgroundColor : "white",
		fontFamily : "Verdana",
		fontSize : "12pt",
		fontWeight : "normal",
		textColor : "black",
		itemSpacing : "10px",
		scrollSpeed : 50 // pixels per second
	}, settings);

	return this.each(function() {
		var $ticker = jQuery(this);
		$ticker.wrap("<div><div></div></div>");
		var $mask = $ticker.parent();
		var $container = $mask.parent();
		$ticker.css({
			position: "relative",
			fontFamily: settings.fontFamily,
			fontSize: settings.fontSize,
			fontWeight: settings.fontWeight,
			color: settings.textColor,
			listStyleType: "none",
			marginTop : 0,
			marginRight : 0,
			marginBottom : 0,
			marginLeft : 0,
			paddingTop : 0,
			paddingRight : 0,
			paddingBottom : 0,
			paddingLeft : 0
		});
		var tickerWidth = 0;
		$ticker.find("li").each(function(i){
			jQuery(this).css({
				position : "relative",
				float: "left",
				marginTop : 0,
				marginRight : 0,
				marginBottom : 0,
				marginLeft : 0,
				paddingTop : 0,
				paddingRight : settings.itemSpacing,
				paddingBottom : 0,
				paddingLeft : 0,
				backgroundColor : settings.backgroundColor,
				whiteSpace: "nowrap"
			});
			// This must be done AFTER setting css styles
			tickerWidth += jQuery(this).outerWidth(true);
		});
		$ticker.hide();
		$mask.css({
			position : "relative",
			overflow : "hidden",
			backgroundColor : settings.backgroundColor,
			marginTop : settings.padding,
			marginRight : settings.padding,
			marginBottom : settings.padding,
			marginLeft : settings.padding,
			paddingTop : 0,
			paddingRight : 0,
			paddingBottom : 0,
			paddingLeft : 0,
			width : "100%"
		});
		$container.css({
			overflow : "hidden",
			borderColor : settings.borderColor,
			borderStyle : settings.borderStyle,
			borderWidth : settings.borderWidth,
			backgroundColor : settings.backgroundColor,
			marginTop : 0,
			marginRight : 0,
			marginBottom : 0,
			marginLeft : 0,
			paddingTop : 0,
			paddingRight : 0,
			paddingBottom : 0,
			paddingLeft : 0,
			width : settings.width
		});

		if ( settings.width == "100%" ) {
			//we use a timer to allow the document to actually RENDER before getting the container's absolute width
			setTimeout(sizeTickerAndStart, 500);
		} else {
			setTimeout(sizeTickerAndStart, 1);
		}

		function sizeTickerAndStart() {
			if ( settings.width == "100%" ) {
				//reset the container width to an absolute number of pixels
				//this avoids the size changing to fit the ticker when it is shown
				$container.width($container.outerWidth(true));
			}
			$ticker.show();
			var maskWidth = $mask.width();
			var totalTravel = tickerWidth + maskWidth;
			var scrollTime = (totalTravel / settings.scrollSpeed) * 1000;
			$ticker.width(tickerWidth);
			$ticker.css("left", maskWidth);
			function scrollTicker(pDistance, pTime){
				$ticker.animate({left: '-='+ pDistance}, pTime, "linear", function(){$ticker.css("left", maskWidth); scrollTicker(totalTravel, scrollTime);});
			};
			$container.hover(
				function(){
					$ticker.stop();
				},
				function(){
					var offset = $ticker.offset();
					var residualSpace = offset.left + tickerWidth;
					var residualTime = (residualSpace / settings.scrollSpeed) * 1000;
					scrollTicker(residualSpace, residualTime);
				}
			);
			scrollTicker(totalTravel, scrollTime);
		}
	});
};

