// AutoScroller.js
//
// JQuery plugin to achieve a smoothly animated continuous scroll of list items, in any style, with any dimentions.
//
// Sep 5, 2011, 
// Zinovi Tauber
//////////////////////////////////////

;(function() 
{		  
	$.extend($.fn, {
		
		AutoScroll2: function ()
		{
			var selfProps = $(this).AutoScroller.properties;
			selfProps.itemOne = $(this).children(selfProps.item + ":first");
			selfProps.itemHeight = selfProps.itemOne.outerHeight(true);
			selfProps.itemOne.AutoScroller.properties = selfProps;
			setTimeout(function (selfProps) {
				return function() {
					selfProps.itemOne.animate({
						marginTop: "-="+selfProps.itemHeight
					}, selfProps.speed / 100 * selfProps.itemHeight, selfProps.easing, function () {
						var selfProps = $(this).AutoScroller.properties;
						if(selfProps.mode == 'cycle')
						{
							$(this).appendTo(selfProps.self).css('marginTop', '+='+selfProps.itemHeight);
						}
						
						selfProps.self.AutoScroll();
					})
			}}(selfProps), selfProps.delay);
		},
		
		AutoScroll: function ()
		{
			var selfProps = $(this).AutoScroller.properties;
			selfProps.itemOne = $(this).children(selfProps.item + ":first");
			selfProps.itemHeight = selfProps.itemOne.outerHeight(true);
			selfProps.dec = 1;
			var maxFps = 50.0;
			
			// Restrict to 10fps
			if(selfProps.speed > maxFps)
			{
				selfProps.dec = selfProps.speed / maxFps;
				selfProps.speed = maxFps;
			}
			selfProps.margTop = -selfProps.dec;
			
			// See if we need to sleep between items.
			if(selfProps.delay > 0)
			{
				clearInterval(selfProps.intervalId);
				setInterval(selfProps.delay);
			}
			
			$(this).StartScroll();
		},
		
		StartScroll: function ()
		{	
			var selfProps = $(this).AutoScroller.properties;
			
			if(selfProps.intervalId < 0)
				return;
			
			selfProps.intervalId = setInterval(function (selfProps) {
				return function() {
					selfProps.itemOne.css("marginTop", function (i) {return i+selfProps.margTop});
					selfProps.margTop -= selfProps.dec;
					
					if(-selfProps.margTop > selfProps.itemHeight)
					{	
						var nextItem = selfProps.itemOne.next();
						selfProps.itemOne.appendTo(selfProps.self).css("marginTop", function (i) {return i});
						selfProps.margTop = -1;						
						selfProps.itemOne = nextItem;
						selfProps.itemHeight = selfProps.itemOne.outerHeight(true);
						
						// See if we need to sleep between items.
						if(selfProps.delay > 0)
						{
							clearInterval(selfProps.intervalId);
							setTimeout(selfProps.self.StartScroll, selfProps.delay);
						}
					}
				}
			}(selfProps), 1000 / selfProps.speed);
		},
		
		// Initialize the autoscroller.
		AutoScroller: function (settings)
		{
			var asp = $(this).AutoScroller.properties;
			$.extend(asp, asProperties, settings);
			asp.self = $(this);
			asp.intervalId = 0;
			
			// bind this object instance to the hover 
			asp.self.hover(function ($s) {
							  	return function () {
									if($s.intervalId >= 0)
									{
										clearInterval($s.intervalId);
										$s.intervalId = -1;
									}
								}
							}(asp), function ($s) {
							  	return function () {
									if($s.intervalId < 0)
									{
										$s.intervalId = 0;
										$s.self.StartScroll();
									}									
								}						   
							}(asp)
						   );
			
			if(asp.auto)
				$(this).AutoScroll();

			return $(this);
		}
	});
	
	var asProperties = $.fn.AutoScroller.properties = {
		auto: true,					// To auto scroll or not.
		mode: 'cycle',				// To cycle through all the items or start reversing when getting to the end or scroll to the top.
		delay: 0,					// How long in ms to sit before moving on to the next item.
		item: 'li',					// The item elements that we scroll.
		speed: 10,					// The animation scroll speed (fps).
		type: 'scroll',				// Should items scroll or fade.
		easing: 'linear',			// Any of the animation easing functions jquery takes by default.
		vertical: true,				// Whether scrolling is vertical or horizontal.
		nextBtn: '.nextTickItem',	// The classname or unique id in jquery selector format of the button to go to next item.
		prevBrn: '.prevTickItem',	// The classname or unique id in jquery selector format of the button to go to previous item.
		maxFps:	50					// The maxFps for the animation.  Under 50 would have good performance in all browsers.
	};
})();

