﻿// JavaScript Document

var Tab =function(options){
	this.options={
		selectedTab:null,
		selectedSub:null,
		Tabs:null,
		Subs:null,

		IsClick:true,
		IsMouseOver:true,
		IsLink:false,
		OnCss:null,
		OutCss:null,
		TabIndex:0,
		TabNum:0,
		
		IsInterval:true,
		Interval:null,
		IntervalTime:2000,
				
		IsAnimate:false,
		Animate:null, 
		IsBind:true 
	};
	this.options=$.extend({},this.options,options);
	this.ExtendAnimate();
	this.init();
}

Tab.prototype={
	select:function(tab,sub,tabindex){
		if(this.options.selectedTab){
			this.options.selectedTab.removeClass(this.options.OnCss);
			tab.addClass(this.options.OnCss);
		}
		if(tab.text()!=this.options.selectedTab.text()){	
			this.options.selectedTab=tab;
		
			if(this.options.selectedSub){
				if(this.options.IsAnimate){
					if(this.options.Animate){
						if($.fn.AnimateHide){
							this.options.selectedSub.css("z-index","2").AnimateHide(); 
						}else{
							this.options.selectedSub.css("z-index","2").fadeOut(this.options.IntervalTime / 3); 
						}
						if($.fn.AnimateShow){
							sub.css("z-index","1").AnimateShow(); 
						}else{
							sub.css("z-index","1").fadeIn(this.options.IntervalTime / 20); 
						}
					}else{
						var i = Math.floor(Math.random()*12) % 4;
						switch(i){
							case 0:
								this.options.selectedSub.css("z-index","2").hide(this.options.IntervalTime / 3);
								break;
							case 1:
								this.options.selectedSub.css("z-index","2").slideUp(this.options.IntervalTime / 3);
								break;
							case 2:
								this.options.selectedSub.css("z-index","2").fadeOut(this.options.IntervalTime / 3);
								break;
							case 3:
								var width=this.options.selectedSub.width();
								var sSub=this.options.selectedSub;
								this.options.selectedSub.css("z-index","2").animate({width:"0"}, this.options.IntervalTime / 3 , function(){
									sSub.css({"display":"none","width":width});
								}.binding(this));
								delete(width);
								delete(sSub);
								break;
						}
						sub.css("z-index","1").fadeIn(this.options.IntervalTime / 20);
					}
				}else{
					this.options.selectedSub.css("display","none");
					sub.css("display","");
				}
			}
			this.options.selectedSub=sub;
		}else{
			if(sub){
				sub.css("display","");
			}
		}
		this.options.TabIndex=tabindex;
		this.options.TabIndex++;		
	},
	init:function(){ 
		if(this.options.Tabs){
			this.options.TabNum=this.options.Tabs.length;
			
			if(this.options.OutCss){ 
				this.options.Tabs.addClass(this.options.OutCss);
			}
			if(this.options.Subs){
				this.options.Subs.css("display","none");	
			}
			this.options.selectedTab=this.options.Tabs.eq(this.options.TabIndex % this.options.TabNum);
			this.options.selectedSub=this.options.Subs? this.options.Subs.eq(this.options.TabIndex % this.options.TabNum) : null;
			this.select(this.options.selectedTab,this.options.selectedSub,this.options.TabIndex);
			
			if(this.options.IsBind&&this.options.Tabs&&this.options.Subs){
				this.options.Tabs.each(function(i,domEle){
					if(this.options.IsClick){
						$(domEle).bind("click",{index:i,dom:domEle},function(e){
							this.select($(e.data.dom),this.options.Subs.eq(e.data.index),e.data.index);
							if(!this.options.IsLink){
								return false;
							}
						}.binding(this));
					};
					
					$(domEle).bind("mouseover",{index:i,dom:domEle},function(e){
						if(this.options.IsMouseOver){
							this.select($(e.data.dom),this.options.Subs.eq(e.data.index),e.data.index);
						}
						clearInterval(this.options.Interval); 
					}.binding(this));
					if(this.options.IsInterval){
						$(domEle).bind("mouseout",function(){
							this.interval();
						}.binding(this));
					}
				}.binding(this))
			}
			
			if(this.options.IsInterval){
				this.interval();
			}
		}
	},
	ExtendAnimate:function(){ 
		if(this.options.Animate){
			if(typeof this.options.Animate.show =="function"){ 
				$.fn.extend({
					AnimateShow:this.options.Animate.show
				})	
			}
			if(typeof this.options.Animate.hide =="function"){
				$.fn.extend({
					AnimateHide:this.options.Animate.hide
				})	
			}
		}
	},
	tabtrigger:function(obj){ 
		if(this.options.IsClick){
			obj.trigger("click");
		}else if(this.options.IsMouseOver){
			obj.trigger("mouseover");
		}
	},
	interval:function(){ 
		this.options.Interval=setInterval(function(){this.tabtrigger(this.options.Tabs.eq(this.options.TabIndex % this.options.TabNum));}.binding(this),this.options.IntervalTime);	
	}
}
