// JavaScript Document
;(function($){
		   $.fn.jTab=function(options){
			   var settings={
				   actionType:"click",defaultSelectedTab:"",selectedClassSuffix:"",transitionType:"",speed:"fast",onShowTabCallBack:null
				   }
			   var op=$.extend(settings,options);
			   var oldobj=null;
			   var $this=$(this);
			   
			   
			   var showTabcontent=function(obj,callback){
				   
				   $this.find("#"+$(obj).attr("rel")).show();
				   
				   }
			   var showHiddenTabcontent=function(obj){
				   //alert($this.find("#tabcontent:first > .tab:visible").attr("id")+" - "+$(obj).attr("rel"))
				   if ($this.find("#tabcontent:first > .tab:visible").attr("id")==$(obj).attr("rel")){ //在同一个Tab上动作，就不执行显示隐藏内容动作
					   return false;
					   }
				   switch (op.transitionType){
					   case "slide":
					   			$this.find("#tabcontent:first > .tab:visible").slideUp(op.speed,function(){
																										 $this.find("#"+$(obj).attr("rel")).slideDown(op.speed,
																																					  function(){if ($.isFunction(op.onShowTabCallBack)) op.onShowTabCallBack(obj)}
																																					  );
																										 
																										 });
								break;
					   case "fade":			   
					   			$this.find("#tabcontent:first > .tab:visible").fadeOut(op.speed,function(){
																										 $this.find("#"+$(obj).attr("rel")).fadeIn(op.speed,
																																					  function(){if ($.isFunction(op.onShowTabCallBack)) op.onShowTabCallBack(obj)}
																																					  );
																										 });
								break;
//					   case "show":
//					   			alert("show");					   
//					   			$this.find("#tabcontent:first > div:visible").hide(op.speed);
//								break;
					   default :			   
					   			$this.find("#tabcontent:first > .tab:visible").hide();$this.find("#"+$(obj).attr("rel")).show();
								if ($.isFunction(op.onShowTabCallBack)) op.onShowTabCallBack(obj)
								break;
					   }
				   
				   
				   }
//			   var showHiddenTabcontent=function(obj,callback){
//				   //var isHidden = $($this.find("#"+$(obj).attr("rel"))).is(":hidden");
//				   //$($this.find("#"+$(obj).attr("rel")))[ isHidden ? "fadeIn" : "fadeOut" ]( isHidden ? "normal" : "normal");
//				   $this.find("#"+$(obj).attr("rel")).toggle();
//				   }
			   var changeTabClass=function(obj){ //改进 未作。可以考虑用showTabcontent的方法来改进
				   if (op.selectedClassSuffix!=""){
					   if ($(obj).hasClass($(obj).attr("rel")+op.selectedClassSuffix)){
						   $(obj).removeClass($(obj).attr("rel")+op.selectedClassSuffix);
						   }
					   else{
						   $(obj).addClass($(obj).attr("rel")+op.selectedClassSuffix);
						   }
//					   var classSuffix=$(obj).attr("class");
//					   alert(classSuffix);
//					   if (classSuffix.indexOf(op.selectedClassSuffix)>-1){ //找到selectedClassSuffix
//						   $(obj).removeClass(classSuffix+op.selectedClassSuffix);
//						   }
//					   else{
//						   $(obj).addClass(classSuffix+op.selectedClassSuffix);
//						   }
					   }
				   else{
					   $(obj).toggleClass("current");
					   }
				   if ($(obj).find("input[type=radio]").length>0){
					   $(obj).find("input[type=radio]").attr("checked","checked")
					   }
				   }
			   var initTabcontent=function(){
				   $this.find("#tabcontent:first").children(".tab").each(function(){
																				  if ($(this).attr("id")!=op.defaultSelectedTab){$(this).hide();}
																				  })
				   }
			   
			   initTabcontent();
			   return $(this).find("#tabs:first").find("li").each(function(i){
																		   
																		   
																		   
														  if ($(this).attr("rel")==op.defaultSelectedTab){
															  oldobj=this;
															  changeTabClass(this);
															  //showHiddenTabcontent(this);
															  }
														  $(this).bind(op.actionType,function(){
																							  var nowobj=this;
																							  if (oldobj==null){
																								  oldobj=this;
																								  changeTabClass(this);
																								  
																								  }
																							  else{
																								  //alert("old:"+$(oldobj).attr("rel")+" now:"+$(this).attr("rel"))
																								  //隐藏老的内容，然后显示新的
																								  changeTabClass(oldobj);
																								  changeTabClass(this);
																								  showHiddenTabcontent(this)
//																								  showHiddenTabcontent(oldobj);
//																								  showHiddenTabcontent(this);
//																								  if ($(oldobj).attr("rel")!=op.defaultSelectedTab){
//																									  changeTabClass(oldobj);
//																									  showHiddenTabcontent(oldobj);
//																									  }
																								  oldobj=this;
																								  } 
																							  //changeTabClass(this);
																							  //showHiddenTabcontent(this);
//																							  if ($(oldobj).attr("rel")!=op.defaultSelectedTab){
//																								  changeTabClass(this);
//																								  showHiddenTabcontent(this);
//																								  }
																							  return false;
																							  })
														  
														  })
			   
			   
			   
			   }
		   
		   
		   
		   })( jQuery );
