(function($){

	$.fn.simplerotator = function(opts){
		
		opts = $.extend({
			interval:	6000,
			speed:		500,
			content:	'div.bannerItem',
			start:		0,
			menu:		null
		},opts);
		
		return $(this).each(function(){
			
			var $this 		= $(this),
				$content 	= $this.find(opts.content),
				num			= $content.length,
				active		= opts.start,
				timer;
			
			if(num == 1) {
				$('ul.control').hide();
			} else if(num == 2) {
				$('ul.control').show();
				$('#select-3').hide();
			} else if(num == 3) {
				$('ul.control').show();
				$('#select-1').show();
				$('#select-2').show();
				$('#select-3').show();	
			}
			
			
			// set the initial show/hide state
			$content.hide().eq(opts.start).show();
			
			// expose api
			$this.bind('rotator.set',function(e,v){change(v);});
			
			$content.mouseover(function(){clearTimeout(timer);});
			$content.mouseout(function(){timer=setTimeout(function(){change();},opts.interval);});
			
			// if rotator includes a menu, define input/outputs
			if(opts.menu != null) {
				var $menu = $(opts.menu);
				// add click events
				$menu.find('li a').each(function(n){
					$(this).click(function(){
						change(n);
						return false;
					});
				});
				// set selected state on change
				$this.bind('rotator.change',function(e,v){
					$menu.find('li').removeClass('selected').eq(v).addClass('selected');
				});
			}
			
			// start timer
			if(opts.interval) {
				setTimeout(function(){change();},opts.interval);
			}
			
			function change(i) {
				if(typeof i == 'undefined') {
					i = active+1;
				}
				i = i%num;
				if(i!=active) {
					clearTimeout(timer);
					$content.eq(active).fadeOut(opts.speed);
					$content.eq(i).fadeIn(opts.speed,function(){
						active=i;
						if(opts.interval) {
							timer=setTimeout(function(){change();},opts.interval);
						}
					});
					$this.trigger('rotator.change',[i,$content.eq(i)]);
				}
			}
			
		});
		
	}

})(jQuery);
