

function MyTab(Conf) {

	this.TabHeader = Conf['header'];
	this.TabContent = Conf['content'];
	this.activeClass = Conf['activeClass'];
	this.inactiveClass = Conf['inactiveClass'];
	this.startAt = Conf['startAt'];
	
	this.type = Conf['type'];
	
	this.timer = Conf['timer'];
	
	this.prevShowIndex = -1;
	
	this.timerID = null;
	
	this.init();
}

MyTab.prototype.init = function() {

	var _self = this;
	
	$(_self.TabHeader).css("cursor","pointer");
	
	//$(_self.TabContent).css("position","absolute");
	$(_self.TabContent).css("top",0);
	$(_self.TabContent).css("left",0);
	
	$(_self.TabContent).parent().css("position","relative");
	$(_self.TabContent).parent().css("overflow","hidden");
	
	// Hide all content
	$(_self.TabContent).not(":first").hide();
	
	// Add Event
	$(_self.TabHeader).each(function (i,el) {
	
		$(el).click(function() { _self.clickTab(i); } );
		
		$(el).mouseenter(function() { if (_self.timer != null) _self.stopTimer(); } );
		$(el).mouseout(function() { if (_self.timer != null) _self.startTimer(); } );
	
	});
	
	
	$(_self.TabContent).parent().hover( function() { if (_self.timer != null) _self.stopTimer(); } , function() { if (_self.timer != null) _self.startTimer(); } );


	if (_self.startAt == undefined) {
		_self.clickTab(0);
	} else {
		_self.clickTab(_self.startAt );
	}
	
	// Timer
	if ((_self.timer != null) && ($(_self.TabHeader).size() > 1)) _self.startTimer();
	
}


MyTab.prototype.clickTab = function(P_Index) {
	
	var _self = this;
	
	if (P_Index != _self.prevShowIndex) {
		
		var index = P_Index;
		var el = $(_self.TabHeader).eq(index);
		
		$(_self.TabHeader).attr("class",this.inactiveClass);
		$(el).attr("class",this.activeClass);
		
		
		var objContentShow = $(_self.TabContent).eq(index);
		var objPrevShow = $(_self.TabContent).eq(_self.prevShowIndex);
		
		objContentShow.css("position","relative");
		
		_self.prevShowIndex = index;
	
		if (_self.type == null) {
			
			// Hide all content
			$(_self.TabContent).hide();
			objContentShow.show();
			
		} else if (_self.type == 'slideup') {
		
			var parentHeight = $(objContentShow).parent().innerHeight();
			
			$(_self.TabContent).not($(objContentShow)).animate({ "marginTop" : parentHeight*-1,"opacity":0 });

			$(objContentShow).parent().height($(_self.TabContent).innerHeight());
			$(objContentShow).css("marginTop",parentHeight);
			$(objContentShow).css("position","absolute");
			$(objContentShow).css("display","block");
			$(objContentShow).css("marginLeft", $(objContentShow).parent().css("paddingLeft"));
			$(objContentShow).animate({  "marginTop" :  $(objContentShow).parent().css("paddingTop") , "opacity":1});
	
		}	else if (_self.type == 'fade') {
			
			objPrevShow.css("top","0px");
			objPrevShow.css("left","0px");
			objPrevShow.css("width","100%");
			objPrevShow.css("position","absolute");
		
			
			$(_self.TabContent).css("top","0px");
			$(_self.TabContent).css("left","0px");
			$(_self.TabContent).stop(true,true).not($(objContentShow)).fadeOut("fase");

			
			$(objContentShow).fadeIn("fast");
			
			if ($(objContentShow).height() != 0)
				$(objContentShow).parent().animate({"minHeight" : $(objContentShow).height() }); 

		}	else if (_self.type == 'slidedown') {
		
			var parentHeight = ($(objContentShow).parent().innerHeight()*-1);
			
			objPrevShow.css("top","0px");
			objPrevShow.css("left","0px");
			objPrevShow.css("position","absolute");
			
			$(_self.TabContent).stop(true,true).not($(objContentShow)).css("position","absolute").animate({ "top" : Math.abs(parentHeight),"opacity":0 } , function() { $(this).css("display","none");} );
			
			$(objContentShow).stop(true,true).css("top",parentHeight).css("display","block").animate({  top : 0 , "opacity":1});
			$(objContentShow).parent().animate({"minHeight" : $(objContentShow).height() });
	
		}	
		
		
		
		
	}
	
}

MyTab.prototype.startTimer = function() {

	var _self = this;
	
	var  MaxTab =  $(_self.TabHeader).size();
	
	_self.timerID = setInterval(function() {
	
											if (_self.prevShowIndex < (MaxTab-1)) {
												_self.clickTab(_self.prevShowIndex + 1);
											} else {
												_self.clickTab(0);
											} 
										
										}, _self.timer );

}

MyTab.prototype.stopTimer = function() {

	var _self = this;
	clearInterval(_self.timerID);

}
