(function(){
	$.fn.introBox = function() {
		return this.each(function(){
			var introbox = $.data(this, 'introbox');
			if (introbox) return introbox;
			
			introbox = new $.introBox(this);
			$.data(this, 'introbox', introbox);
			return introbox;
		});
	};
	
	$.introBox = function(elem) {
		this.element = elem;
		this.init();
	};
	
	$.extend( $.introBox, {
		prototype: {
			init: function(){
				var self = this;
				this.list = $(".nav>ul>li", this.element);
				
				if ( !this.list.length ) return;
				
				$("a.current", this.list).removeClass("current");
				this.current = this.list.filter(".current");
				
				if ( !this.current.length ) {
					$(this.list[0]).addClass("current");
					
					this.current = this.list.filter(".current");
				}
				
				this.list.each(function(){
					$(this).append($("<span class='bg'></span>").css({display:"block",opacity:1}));
					$(this).append($("<span class='bg_hover'></span>").css({display:"block",opacity:0}));
				});
				
				this.list.bind("click", function(event) {
					if ($(this).is(".current")) return false;
					
					this.blur();
					event.preventDefault();
					var content, a, toHide, toShow;
					
					$(self.current).removeClass("current");
					$("span.bg", self.current).stop();
					$("span.bg", self.current).animate({opacity:1},{queue:false,duration:800});
					$("span.bg_hover", self.current).stop();
					$("span.bg_hover", self.current).animate({opacity:0},{queue:false,duration:1200});
					a = $("a", self.current);
					
					if (a) {
						a.removeClass("current");
						content = self.getContent(a);
						if (content) {
							toHide = $(content[0]);
						}
					}
					
					self.current = $(this);
					$(self.current).addClass("current");
					$("span.bg", self.current).stop();
					$("span.bg", self.current).animate({opacity:0},{queue:false,duration:800});
					$("span.bg_hover", self.current).stop();
					$("span.bg_hover", self.current).animate({opacity:1},{queue:false,duration:1200});
					
					a = $("a", self.current);
					a.blur();
					if (a) {
						a.addClass("current");
						content = self.getContent(a);
						if (content) {
							toShow = $(content[0]);
						}
					}
					
					if (toHide) {
						toHide.stop();
						toHide.animate({ opacity:0 }, {queue: false, duration: 800});
					}
					
					if (toShow) {
						if (toShow.css("opacity") >= 0.99) {
							toShow.css("opacity",0);
						}
						toShow.stop();
						toShow.animate({ opacity:1 }, {queue: false, duration: 1200});
						toShow.show();
					}
					
					self.resetAuto();
					
					return false;
				});
				
				this.autoMode = false;
				this.autoModeTimeout = 0;
				
				this.resetAuto();
				
				this.show(false);
			},
			
			getContent: function(item) {
				var href = item.attr("href");
				var arr = href.split("#",2);
				if ( arr.length > 1 && arr[1] != "" ) {
					var content = $("#"+arr[1]);
					if (content.length) return content;
				}
				return undefined;
			},
			
			show: function(anim) {				
				if ( !this.list.length ) return;
				if ( !this.current.length ) return;
				
				anim = anim == undefined ? true : anim;
				
				this.current.addClass("current");
				
				var a = $("a", this.current);
				if ( !a.length ) return;
				
				a.addClass("current");
				
				var content = this.getContent(a);
				
				if (content) {
					if ( !anim ) {
						$(content[0]).show();
						$("span.bg", this.current).css({opacity:0});
						$("span.bg_hover", this.current).css({opacity:1});
					} else {
						$(content[0]).stop();
						$(content[0]).animate({
							opacity:1
						}, {queue: false, duration: 1000, easing:"easeInOutExpo", complete:function() {
							
						}});
					}
				}
			},
			
			getNext: function() {
				var self = this;
				var next;
				
				next = self.current.next();
				if (!next.length) {
					next = $(self.list[0]);
				}
				
				return next;
			},
			
			autoNext: function() {
				var content, a, toHide, toShow, next;
				var self = this;
				
				$(self.current).removeClass("current");
				$("span.bg", self.current).stop();
				$("span.bg", self.current).animate({opacity:1},{queue:false,duration:800});
				$("span.bg_hover", self.current).stop();
				$("span.bg_hover", self.current).animate({opacity:0},{queue:false,duration:1200});
					
				a = $("a", self.current);
				
				if (a) {
					a.removeClass("current");
					content = self.getContent(a);
					if (content) {
						toHide = $(content[0]);
					}
				}
				
				next = self.getNext();
				self.current = next;
				$(self.current).addClass("current");
				$("span.bg", self.current).stop();
				$("span.bg", self.current).animate({opacity:0},{queue:false,duration:800});
				$("span.bg_hover", self.current).stop();
				$("span.bg_hover", self.current).animate({opacity:1},{queue:false,duration:1200});
					
				a = $("a", self.current);
				a.blur();
				if (a) {
					a.addClass("current");
					content = self.getContent(a);
					if (content) {
						toShow = $(content[0]);
					}
				}
				
				if (toHide) {
					toHide.stop();
					toHide.animate({ opacity:0 }, { queue: false, duration: 800, complete:function() {
						
					}});
				}
				
				if (toShow) {
					if (toShow.css("opacity") >= 0.99) {
						toShow.css("opacity",0);
					}
					toShow.stop();
					toShow.animate({
						opacity:1
					}, {queue: false, duration: 1200, complete:function() {
						
					}});
					toShow.show();
				}
				
				self.resetAuto();
			},
			
			resetAuto: function(tmr) {
				var self = this;
				tmr = tmr || 6000;
				
				if (this.autoModeTimeout != 0) {
					window.clearTimeout(this.autoModeTimeout);
				}
				this.autoMode = false;
				
				this.autoModeTimeout = window.setTimeout(function(){
					self.autoNext();
				}, 6000);
			}
			
		}
	});
	
	$(document).ready(function(){
		$("#introBox").introBox();
	});
})(jQuery);