/*
 * jQuery WG Ticker plug-in 1.0
 * Copyright (c) 2008 Roberto Lee
 */
 /*
 * jQuery plug-in Ticker  1.0
 *
 * Copyright (c) 2008 Roberto Lee (webgenerator.com)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * Date: 2009-1-29
 * Rev: 1
 */


jQuery.fn.WG_Ticker = function(parameters) {// 'this' is a jQuery object at this point - with all the jQuery functions
	defaults = {
		speed: 0.06,
		margin_mask: 2, //in pixel
		items_break: "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;",
		if_empty: function() {}
	};
  	return this.each(function() {// return so we don't break the chain now we are inside of a jQuery function, the DOM element is the context so 'this' has changed to become a DOM element.
		jQuery.extend(defaults, parameters);
		var tickerlist = jQuery(this);
		var containerwidth = tickerlist.parent().width();
    	jQuery(this).css('list-style-type', 'none').css('margin', '0').css('padding','0').css('left', containerwidth + 'px').css('position', 'relative');

		var totalwidthlist = 0;
		var count_tickerlist_items = tickerlist.find("li").length;
		tickerlist.find("li").each(function (i) {
			jQuery(this).css('float', 'left').css('margin','0').css('padding','0').css('white-space','nowrap');
			jQuery(this).find("div,span").each(function (i) { //throw away div and span wrappers
				jQuery(this).parent().append( $(this).remove().html() );
			});
			if((i+1)<count_tickerlist_items)
			{
				jQuery(this).append(defaults.items_break);
			}
			totalwidthlist += (jQuery(this).width());
		});
		tickerlist.width(totalwidthlist);
		tickerlist.wrap("<div class='mask' style='overflow: hidden; position: relative; width: " + (containerwidth - (2*defaults.margin_mask)) + "px; margin: 0px " + defaults.margin_mask + "px 0px " + defaults.margin_mask + "px;'></div>");


		var scrolllength =  containerwidth + totalwidthlist;
		var timer_to_replay = scrolllength/defaults.speed;
		var initialoffset = tickerlist.offset();
		var factor = scrolllength/timer_to_replay;
		function scroll_ele(scrolllengthtotravel, timer_replay)
		{
			tickerlist.animate({left: '-=' + scrolllengthtotravel}, timer_replay,
													"linear",
													function()
													{
														tickerlist.css("left", containerwidth);
														scroll_ele(scrolllengthtotravel, timer_replay);
													}
			)
		}


		scroll_ele(scrolllength, timer_to_replay);
		var new_offset = 0;
		tickerlist.hover(
			function () {
				tickerlist.stop();
				new_offset = tickerlist.offset();
			},
			function () {
				var offsetdestination = scrolllength - initialoffset.left
				var traveled = initialoffset.left - new_offset.left;
				var residualSpace = scrolllength - traveled ;
				var residualTime = residualSpace / defaults.speed;
				scroll_ele(residualSpace, residualTime);

			}
		);
});
};





/*
	USAGE SAMPLE
    $(document).ready(function() {
			jQuery(".tickerbalk ul").WG_Ticker({
					speed:  0.06,
					items_break: "&nbsp"
			});
    });



.tickerbalk{
	position : absolute;
	left : 70px;
	bottom : 0px;
	padding : 0px 15px 0 15px;
	line-height : normal;
	font-size : 11px;
	font-family : Arial;
	color : #000000;
	width : 330px;
	height : 17px;
	line-height: 17px;
	overflow: hidden;
}

//.tickerbalk .mask {
//	position: relative;
//	width: 330px;
//	overflow: hidden;
//}

//.tickerbalk .mask  ul {
//	position: relative;
//	left : 330px;
//}

*/


