/*!
 * webTicker 1.0
 * Examples and documentation at: 
 * http://jonmifsud.com/blog/jquery-web-ticker/
 * 2011 Jonathan Mifsud
 * Version: 1.0.0 (9-APRIL-2011) *  Copyright (C) 2011  Jonathan Mifsud *  This program is free software: you can redistribute it and/or modify *  it under the terms of the GNU General Public License as published by *  the Free Software Foundation, either version 3 of the License, or *  (at your option) any later version. *  This program is distributed in the hope that it will be useful, *  but WITHOUT ANY WARRANTY; without even the implied warranty of *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the *  GNU General Public License for more details. *  You should have received a copy of the GNU General Public License *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  *  Whilst this library is free to use - knowledge of websites using  *  this library would be appriciated. Just drop an email with the website *  link to info@jonmifsud.com
 * Requires:
 * jQuery v1.2.x or later
 * 
 */


jQuery.fn.webTicker = function(settings) {
		settings = jQuery.extend({
		travelocity: 0.041
		}, 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);					
				function scrollnews(spazio, tempo){
					$strip.animate({left: '-='+ spazio, width: "120%"}, 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);
				});			
		});	
};
