/*!
 * webTicker 1.0
 * Examples and documentation at:
 * http://www.jonmifsud.com
 * 2011 Jonathan Mifsud
 * Version: 1.0.0 (9-APRIL-2011)
 * 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.2.x or later
 *
 */


jQuery.fn.webTicker = function(settings) {
		settings = jQuery.extend({
		travelocity: 0.07
		}, settings);
		return this.each(function(){
				var $strip = jQuery(this);
				$strip.addClass("newsticker")
				var stripWidth = 0;
				var $mask = $strip.wrap("<div class='mask'></div>");
				var $tickercontainer = $strip.parent().wrap("<div class='tickercontainer'></div>");
				$strip.find("li").each(function(i){
					stripWidth += jQuery(this, i).outerWidth(true); // thanks to Michael Haszprunar
				});
				$strip.width(stripWidth+20);//20 used for ie9 fix
				function scrollnews(spazio, tempo){
					$strip.animate({left: '-='+ spazio}, tempo, "linear", function(){
						$strip.children().last().after($strip.children().first());
						var first = $strip.children().first();
						var width = first.outerWidth(true);
						var defTiming = width/settings.travelocity;
						var tmpleft = $strip.css("left");
						var outWidth = $strip.children().last().outerWidth(true);
						var left = parseInt(tmpleft.replace('px','')) + outWidth;
						$strip.css("left", left);
						scrollnews(width, defTiming);
					});
				}

				var first = $strip.children().first();
				var travel = first.outerWidth(true);
				var timing = travel/settings.travelocity;
				scrollnews(travel, timing);
				$strip.hover(function(){
					jQuery(this).stop();
				},
				function(){
					var offset = jQuery(this).offset();
					var first = $strip.children().first();
					var width = first.outerWidth(true);
					var residualSpace = offset.left + width;
					var residualTime = residualSpace/settings.travelocity;
					scrollnews(residualSpace, residualTime);
				});
		});
};
