;(function($) {

	$.fn.Rotator = function(settings) {
		var options = $.extend({}, $.fn.Rotator.defaults, settings);

		if (this.length == 0) {
			debug('Invalid selector!');
			return;
		} else if (this.length > 1) {
			return this.each(function() {
				$.fn.Rotator.apply($(this), [settings]);
			});
		}

		$global = $(this);

		$global.css({
			'cursor':	options.cursor,
			'overflow':	'hidden'
		});

		var elements	= $global.children("div"),
			$this		= $global,
			timer		= 0,
			isAnimate	= false;

		elements.each(function(i) {
			$(this)
			.attr('id', $global.attr('id') + '-' + (i + 1))
			.hide();
		});

		elements.first().show().end();

		go(elements, options, 0);

		var stop = function() {
			var target = $(this),index,last;

			clearTimeout(timer);

			if (target.is('li')) {
				index	= target.index();
				last	= target.parent().children('.rotator-link-selected').index();
			} else {
				index	= $this.next('ul').children('.rotator-link-selected').index();
				last	= index;
			}

			if (index != last) {
				target.addClass('rotator-link-selected').parent().children().eq(last).removeClass('rotator-link-selected');
				change(elements, options, index, last);
			}
		},
		start = function() {
			var index	= $this.next('ul').children('.rotator-link-selected').index(),isBanner;
			if (!$(this).is('li')) {
				isBanner = true;
			}
			go(elements, options, index, isBanner);
		};

		if (options.menu) {
			
			var imgs	= elements.find('img'),
				menu	= '',
				target	= (options.target != '') ? 'target="' + options.target + '"' : '',
				parent,
				img;

				debug(target);
			imgs.each(function() {
				img		= $(this);
				debug(img.attr('src'));
				parent	= img.parent("div");
				menu += '<li><a href="' + parent.attr(parent.is('a') ? 'href' : 'title') + '" ' + target + '><img src="Content/Images/empty.gif" width="18" height="18" border="0" alt="'+img.attr('title')+'"/></a></li>';
			});

			//$('<p>Test</p>').appendTo($global);
			//$global.
			$global.after('<ul class="rotator-menu">' + menu + '</ul>');

			var	links	= $('ul.rotator-menu').children('li');
			links.hover(stop, start).mousemove(stop).first().addClass('rotator-link-selected').end().last();
		}

		if (options.pause) {
			$global.hover(stop, start);
		}

		function go(elements, options, index, isBanner) {
			change(elements, options, index, index - 1);

			if (isBanner == undefined) {
				selectMenu(index);
			}

			index = (index < elements.length - 1) ? index + 1 : 0;

			timer =	setTimeout(function() {
						go(elements, options, index);
					}, options.time);
		};

		function change(elements, options, index, last) {
			if (!isAnimate) {
				isAnimate = true;
				if (options.animation == 'fade') {
					elements.eq(last).fadeOut(options.speed);
					elements.eq(index).fadeIn(options.speed, function() {
						selectMenu(index);
						isAnimate = false;
					});
				} else {
					elements.eq(last).hide();
					elements.eq(index).show();
					isAnimate = false;
				}
			}
		};

		function selectMenu(index) {
			$this.next('ul').children().removeClass('rotator-link-selected').eq(index).addClass('rotator-link-selected');
		};

		return $global;
	};

	function debug(message) {
		if (window.console && window.console.log) {
			window.console.log(message);
		}
	};

	$.fn.Rotator.defaults = {
		cursor:		'default',
		menu:		false,
		pause:		false,
		speed:		600,
		target:		'',
		time:		3600,
		animation:	'normal'
	};

})(jQuery);
