// JavaScript Document
/*
DfSoft TabsControl
Version: 1.1 (2008.04.10)
*/

function TabsControl(divID)
{
	// 初始化容器。
	this.container=document.getElementById(divID);
	if(!this.container)
	{
		alert("Invalid container.");
		return null;
	}
	try
	{
	    // 初始化当前选定的选项卡索引。
		this.selectedIndex=-1;
		// 创建内部对象。
		this.tabsArea=document.createElement("DIV");
		this.clientArea=document.createElement("DIV");
		this.tabsTable=document.createElement("TABLE");
		this.tabsTableBody=document.createElement("TBODY");
		this.tabsTable.appendChild(this.tabsTableBody);
		this.tabsArea.appendChild(this.tabsTable);
		this.container.appendChild(this.tabsArea);
		this.container.appendChild(this.clientArea);
		this.tabsTableBody.insertRow(-1);
		// 设置标签表格默认样式。
		this.tabsTable.style.borderCollapse="collapse";
		this.tabsTable.cellSpacing="0";
		this.tabsTable.cellPadding="0";
	}
	catch(e)
	{
		alert("Create inner elements failed!");
		return null;
	}

	// 返回控件包含的选项卡数量。
	this.GetTabsCount=function ()
	{
		return this.tabsTableBody.rows[0].cells.length;
	}
	
	// 应用选项卡样式。
	this.ApplyTabStyle=function (index,type)
	{
		if(index>=this.GetTabsCount() || index<0) return false;
		var styleString="";
		switch(type)
		{
		case 0:     // 普通状态。
		    styleString=this.tabsTableBody.rows[0].cells[index].normalStyle; break;
		case 1:     // 激活状态。
		    styleString=this.tabsTableBody.rows[0].cells[index].activeStyle; break;
		case 2:     // 鼠标悬停状态。
		    styleString=this.tabsTableBody.rows[0].cells[index].hoverStyle; break;
		}
		// 应用样式设置到 CSS 样式属性或 CSS 类名。
		if(styleString.indexOf(":")!=-1)
		    this.tabsTableBody.rows[0].cells[index].style.cssText=styleString;
		else
		    this.tabsTableBody.rows[0].cells[index].className=styleString;
		return true;
	}
	
	// 设置客户区 CSS 样式属性或 CSS 类名。
	this.SetClientAreaStyle=function (style)
	{
	    if(style.indexOf(":")!=-1)
		    this.clientArea.style.cssText=style;
		else
		    this.clientArea.className=style;
		this.clientArea.style.overflow="hidden";
		return true;
	}
	
	// 设置标签区 CSS 样式属性或 CSS 类名。
	this.SetTabsAreaStyle=function (style)
	{
	    if(style.indexOf(":")!=-1)
	        this.tabsArea.style.cssText=style;
	    else
	        this.tabsArea.className=style;
	    return true;
	}
	
	// 添加新选项卡。
	this.AddTab=function (text,frame,style,actStyle,hoverStyle)
	{
		var objFrame=document.getElementById(frame);
		if(objFrame==null) return false;
		try
		{
		    var tabCell=this.tabsTableBody.rows[0].insertCell(-1);
		    tabCell.controlObject=this;
		    tabCell.tabIndex=this.GetTabsCount()-1;                 // 设置唯一索引值。
		    tabCell.innerHTML=text;                                 // 设置文本内容。
		    tabCell.normalStyle=style;                              // 保存普通状态的样式设置。
		    tabCell.activeStyle=(actStyle!=null?actStyle:style);    // 保存激活状态的样式设置。
		    tabCell.hoverStyle=(hoverStyle!=null?hoverStyle:style); // 保存鼠标悬停状态的样式设置。
		    
		    tabCell.contentFrame=objFrame.parentNode.removeChild(objFrame);
		    tabCell.contentFrame.style.display="none";
			this.clientArea.appendChild(tabCell.contentFrame);
		    
		    this.ApplyTabStyle(tabCell.tabIndex,0);                 // 应用样式设置（普通状态）。
		    tabCell.onclick=function ()                             // 设置鼠标单击事件。
		    {
		        this.controlObject.SetActiveTab(this.tabIndex);
		        this.blur();                                        // 移除单击后获得的焦点。
		    }
		    tabCell.onmouseover=function ()                         // 设置鼠标移动事件。
		    {
		        if(this.controlObject.selectedIndex!=this.tabIndex)
					this.controlObject.ApplyTabStyle(this.tabIndex,2);
		    }
		    tabCell.onmouseout=function ()                          // 设置鼠标移出事件。
		    {
		        if(this.controlObject.selectedIndex!=this.tabIndex)
					this.controlObject.ApplyTabStyle(this.tabIndex,0);
		    }
			return true;
		}
		catch(e) {return false;}
	}
	
	// 切换到指定的选项卡。
	this.SetActiveTab=function (index)
	{
		if(index>=this.GetTabsCount() || index<0) return false;
		var tabCurr=null;
		// 获取当前显示的选项卡对象。
		if(this.selectedIndex>=0)
			tabCurr=this.tabsTableBody.rows[0].cells[this.selectedIndex];
		try
		{
			//this.clientArea.style.filter="BlendTrans(duration=0.3)";
			//this.clientArea.style.zoom="1";
			//this.clientArea.filters[0].Apply();
			// 隐藏当前显示的选项卡的内容对象。
			if(tabCurr!=null)
				tabCurr.contentFrame.style.display="none";
			// 显示新选项卡的内容对象。
			this.tabsTableBody.rows[0].cells[index].contentFrame.style.display="block";
			//this.clientArea.filters[0].Play();
		}
		catch(e)
		{
			// 隐藏当前显示的选项卡的内容对象。
			if(tabCurr!=null)
				tabCurr.contentFrame.style.display="none";
			// 显示新选项卡的内容对象。
			this.tabsTableBody.rows[0].cells[index].contentFrame.style.display="block";
		}
	    this.ApplyTabStyle(this.selectedIndex,0);	// 还原当前选项卡的样式为普通状态。
	    this.ApplyTabStyle(index,1);				// 设置新选定的选项卡的样式为激活状态。
	    this.selectedIndex=index;					// 更新当前选定的选项卡索引值。
		return true;
	}
}this.ApplyTabStyle(index,1);				// 设置新选定的选项卡的样式为激活状态。
	    this.selectedIndex=index;					// 更新当前选定的选项卡索引值。
		return true;
	}
}