(function(){
	$.fn.rotatingText = function(options) {
		this.each(function(){
			var rotatingText = $.data(this, 'rotatingText');
			if (rotatingText) return rotatingText;
			
			rotatingText = new $.rotatingText(this, options);
			$.data(this, 'rotatingText', rotatingText);
			return rotatingText;
		});
		
		return this;
	};
	
	$.rotatingText = function(elem, options) {
		this.element = elem;
		this.options = $.extend({}, $.rotatingText.defaults, options );
		this.init();
	};
	
	$.extend( $.rotatingText, {
		defaults: {
			hideDuration:600,
			showDuration:800,
			autoTimeout:10000,		//set 0 to disable auto mode
			autoInterval:5000,
			stopAutoWhenClick:true
		},
		prototype: {
			init: function(){
				this.items = $(".snapshot_items>li", this.elem);
				if (!this.items.length) {
					return;
				}
				
				this.items.css({
					display:"none"
				});
				
				this.current = this.items.filter(".snapshot_item_selected");
				if (!this.current.length) {
					this.current = $(this.items[0]);
				}
				
				this.current.css({
					display: "block"
				});
				
				this.generateNav();
				
				this.nav = $("a", this.navContainer);
				this.currentIndex = this.items.index(this.current);
				$(this.nav[this.currentIndex]).addClass("snapshot_nav_selected");
				
				var self = this;
				this.nav.bind("click", function(event){
					self.nav_onClick.apply(self, [this, event]);
					return false;
				});
				
				this.resetAutoMode();
			},
			
			generateNav: function() {
				if ( !$(".snapshot_nav", this.elem).length ) {
					$(this.elem).append( $("<div></div>").addClass("snapshot_nav") );
				}
				this.navContainer = $(".snapshot_nav", this.elem);
				for (var i=0, n=this.items.length; i < n; i++) {
					this.navContainer.append("<a href='#' title=''>"+(i+1)+"</a>");
				}
				
				return true;
			},
			
			nav_onClick: function(nav, event) {
				nav.blur();
				var idx = this.nav.index(nav);
				if (!$(this.items[idx]).is(".snapshot_item_selected")) {
					var toHide = this.items.filter(".snapshot_item_selected");
					var toShow = $(this.items[idx]);
					
					if (toHide && toHide.length) {
						toHide
							.removeClass("snapshot_item_selected")
							.stop()
							.animate({
								opacity:0},{
								duration:this.options.hideDuration,
								queue:false,
								complete:function(){
									$(this).css({
										display: "none",
										opacity: ""
									});
								}
							});
					}
					
					if (toShow && toShow.length) {
						if ( !$(toShow).is(":visible") ) {
							$(toShow).css("display", "block");
						}
						toShow.css({opacity:0}).addClass("snapshot_item_selected");
						toShow.stop().animate({opacity:1},{duration:this.options.showDuration,queue:false,complete:function(){
							$(this).css({
								display: "block",
								opacity: ""
							});
						}});
					}
					
					this.nav.removeClass("snapshot_nav_selected");
					$(nav).addClass("snapshot_nav_selected");
					
					this.currentIndex = idx;
					this.current = toShow;
				}
				
				if (event && !this.options.stopAutoWhenClick) {
					this.resetAutoMode();
				}
				
				if (this.options.stopAutoWhenClick) {
					this.clearTimeout();
				}
			},
			
			resetAutoMode: function() {
				var self = this;
				self.clearTimeout();
				if (this.options.autoTimeout > 0) {
					this.autoModeTimeout = window.setTimeout(function(){
						self.autoNext.apply(self,[]);
					}, this.options.autoTimeout);
				}
			},
			
			autoNext: function() {
				var idx = this.currentIndex;
				var self = this;
				self.clearTimeout();
				
				idx++;
				if (idx > self.items.length-1) {
					idx = 0;
				}
				
				self.nav_onClick.apply(self, [self.nav[idx], null]);
				
				
				self.autoModeTimeout = window.setTimeout(function(){
					self.autoNext.apply(self,[]);
				}, self.options.autoInterval);
			},
			
			clearTimeout: function() {
				if (this.autoModeTimeout && this.autoModeTimeout > 0) {
					window.clearTimeout(this.autoModeTimeout);
				}
			}
		}
	});
	
	$(document).ready(function(){
		$(".snapshot").rotatingText();
	});
})(jQuery);