﻿
(function($) {

	$.fn.ixContentListScroller = function(options) {
		var opt = {
			displayDuration: 5750,
			viewportWidth: 400,
			viewportHeight: 400
		};
		
		$.extend(opt, options);
		$(this).each(function() {
			var that = $(this);
			var moveToNextItemTimer = null;
			var currentIndex = 0;
			var moveToListItem = function(li) {};
			
			var startMoveToNextTimer = function() {
				moveToNextItemTimer = setInterval(moveToNextItem, opt.displayDuration);
			};
			var moveToNextItem = function() {
				var children = that.children();
				currentIndex++;
				
				if (currentIndex >= children.length)
					currentIndex = 0;
				
				moveToListItem(children.eq(currentIndex));
			};
			
			moveToListItem = function(li) {
				var t = ((opt.viewportHeight - li.height()) / 2) + 'px';
				
				li.css('padding', t + ' 0px');
				that.fadeTo('fast', 0.3)
					.animate({top: -(li.position().top) + 'px'}, 'slow', 'swing', function() {
						that.fadeTo('fast', 1);
					});
			};
			
			$(this).css({
					position: 'absolute',
					'list-type': 'none',
					padding: 0,
					margin: 0
				})
				.mouseover(function() { clearInterval(moveToNextItemTimer); })
				.mouseout(function() { startMoveToNextTimer(); })
				.wrap(
					$('<div/>')
						.css({
							position: 'relative',
							overflow: 'hidden'
						})
						.width(opt.viewportWidth)
						.height(opt.viewportHeight)
				);
			
			var firstItem = $('li:first', this);
			
			moveToListItem(firstItem);
			startMoveToNextTimer();
		});
	};

})(jQuery);