/* Official plugin not working. quicker to write own given how long i've spent trying to fix the real one... - dan */
/* this is coded to 'rotate' only once */
(function($){
	$.fn.liScroll = function(settings) {
		settings = jQuery.extend({
			velocity: 0.07,
			ticker_height: 20
		}, settings);		
		return this.each(function() {
			$this = $(this);
			//generate unique id, and wrap ul with wrapper
			unique_id = 'uid_'+ Math.round(Math.random()*999+1);
			$ticker_wrapper = $('<div class="ticker_wrapper" id="'+unique_id+'"></div>')
			$ticker_wrapper.css({
				height: settings.ticker_height,
				overflow: 'hidden',
				position: 'relative',
				paddingLeft: 5
			});
			//the parent div will start with display:none to help
			//layout
			$($this.parent('div')).css('display','block');
			$this
				.css({
					height: settings.ticker_height,
					width: $('li',this).length*$('li:eq(0)',this).width(),
					position: 'relative'
				})
				.wrap($ticker_wrapper)
				;
			//ie6 has a real issue with wrapping and overflows...
			$('#'+unique_id).wrap('<div id="outer_wrap"></div>');
			$('#outer_wrap').css({overflow: 'hidden', width: 960})
			//duplicate ul to fill width of wrapper*2
			check_width = $this.width();
			num_clones = 1;
			while ( check_width < $('#'+unique_id).width()*2 ) {
				$clone = $this.clone();
				$clone.attr('id', $clone.attr('id')+check_width);
				//correct position of clones
				$clone.css({
					top: settings.ticker_height*-1*num_clones,
					left: check_width
				});
				check_width += $this.width();
				num_clones++;
				//$this.after($clone);
				$('#'+unique_id).append($clone);
			}

			$('#'+unique_id)
				.bind('animate_ticker',function(event, distance, time) {
					$(this).animate({
						marginLeft: '-='+distance
					}, time, 'linear');
				})
				.hover(function() {
					$(this).stop();
				}, function() {
					distance = Number($(this).css('margin-left').replace(/[^0-9-.]/g,''));
					distance_left = distance+$('ul:eq(0)',this).width();
					time_left = distance_left/settings.velocity;
					$(this).trigger('animate_ticker', [
						distance_left,
						time_left
					]);
				})
				.trigger('animate_ticker', [
					$this.width(),
					$this.width()/settings.velocity
				])
				;
		});
	};
})(jQuery);

