(function($) {
	jQuery.fn.rotator = function(options)
	{
		var self = this;

		var options = options || {};
		options = $.extend({
			itemSelector: '.article',
			rotateSpeed: 3000,
			displayList: true,
			listSelect: 'mouseover'
		}, options);

		return self.each(function() {
			// for each node
			var myself = this;
			var rotatePosition = 0;
			var nextTimeout;

			// hide all non-active articles
			$(options.itemSelector).css({
				zIndex: 100
			});
			$(options.itemSelector+':not(:eq('+rotatePosition+'))', myself).hide();
			var img = $(options.itemSelector+':visible').find('img').attr('src');
			$('<div />')
				.addClass('bg')
				.css({
					backgroundImage: 'url('+img+')',
					height: '350px',
					width: '646px',
					position: 'absolute',
					top: 0,
					zIndex: 50
				})
				.appendTo(myself);
			
			

			$('', myself).addClass('ui-selected');

			// inject display list
			if(options.displayList) {
				var ul = $('<ul />')
					.addClass('list');

				$(options.itemSelector, myself).each(function() {

					// adjust the thumbnail url name
					var src = $(this).find('img').attr('src');
					var parts = src.split('/');
					parts[4] = 'imgrotator';
					parts[parts.length - 1] = 'tn-' + parts[parts.length - 1];
					//parts[parts.length - 1] = 'tn-' + parts[parts.length - 1];
					src = parts.join('/');
					
					var url = $(this).find('a').attr('href');

					var li = $('<li data-url="'+url+'"><img height="86" width="120" src="'+src+'" /></li>')
						.bind(options.listSelect, function() {
							show($(this).index(), false);
						})
						.bind('click', function() {
							var url = $(this).attr('data-url');
							document.location.href = url;
						})
						.css('cursor', 'pointer')
						.appendTo(ul);
				})
				ul.prependTo(myself);
			}

			$(this).hover(function() {
				stop();
			}, function() {
				start();
			})

			start();

			function stop()
			{
				if(nextTimeout > 0) {
					clearInterval(nextTimeout);
				}
			}

			function start()
			{
				//stop();
				nextTimeout = setInterval(function() {
					next();
				}, options.rotateSpeed)
			}

			function show(position, animate)
			{
				if(animate == undefined) {
					animate = true;
				}
				rotatePosition = position;
				$(options.itemSelector, myself).hide();
				var $show = $(options.itemSelector+':eq('+rotatePosition+')', myself);
				if(animate) {
					$show.fadeIn(1000, function() {
						$('.bg', myself).css({
							backgroundImage: 'url('+$('img', this).attr('src')+')'
						})
					});
				} else {
					$show.show();
					$('.bg', myself).css({
						backgroundImage: 'url('+$('img', $show).attr('src')+')'
					});
				}

				if(options.displayList) {
					$('.list li', myself).removeClass('ui-selected');
					$('.list li:eq('+rotatePosition+')', myself).addClass('ui-selected');
				}
			}

			function next()
			{
				var nextPos = ++rotatePosition;
				if(nextPos >= $(options.itemSelector, myself).size()) {
					nextPos = 0;
				}
				show(nextPos);

				if(rotatePosition >= $(options.itemSelector, myself).size()) {
					rotatePosition = -1;
				}
			}
		});
	}
})(jQuery);
