;(function($) {
	$.fn.cicRotate = function(options) {
		var defaults = {  
			interval: 5000,
			autoRotate: true,
			crossFade: false,
			ieNoFade: false
		};
		var options = $.extend(defaults, options);
		
		return $(this).each(function() {
			var e = $(this);
			var timer, i = 0, ct = e.find('.rotate-item').size(), t = options.interval;
			
			e.css({position: 'relative'});
			e.find('.rotate-item').css({zIndex:'2',position:'absolute',top:'0',left:'0'});
			e.find('.rotate-item:not(:first)').hide().hover(
				function() {
					stop();
				},
				function() {
					start();
				}
			);
			
			e.find('.rotator-prev').click(function() {
				tick(-1);
				stop();
			});
			e.find('.rotator-next').click(function(){
				tick();
				stop();
			});
			
			function start() {
				stop();
				if(options.autoRotate) {
					timer = setInterval(function() {
						tick();
					}, t);
				}
			}
			
			function stop() {
				clearInterval(timer);
			}
			
			function tick(steps) {
				if(!steps) steps = 1;
				
				var cur = e.find('.rotate-item:eq(' + i + ')');
				// Increment the index plus or minus steps
				i = (i + ct + steps) % ct;
				var next = e.find('.rotate-item:eq(' + i + ')');
				
				if(options.crossFade) {
					cur.css({'z-index': '2'});
					next.css({'z-index': '1'}).show();
					// hide current
					cur.fadeOut(1000);
				} else {
					// hide current
					cur.hide();

					// Show the next one
					if($.browser.msie && $.browser.version == '6.0' && options.ieNoFade) {
						next.animate({height:'show', width:'show'}, 200);
					} else {
						next.fadeIn(500);
					}
				}
			}
			
			start();
		});
	};
})(jQuery);
