/******************************************************************
	Author:       李俊超
	Create Date : 2002-02-26
	Function:     卡片式对话框。
	Usage Example:

	var tcard = new TabControl("layout_1", 530, 380); 
	tcard.setCss("normalText");
	tcard.add("用户信息", "maintable");
	tcard.add("提醒对象信息", "remainder"); 
	tcard.show();			  

	Explain: The '*' express this parameter can be selected.
 ******************************************************************/

/* 显示初始框。 */
function show() {
	//写入
	for( i = 0 ; i < this.cards.length ; i++ ) {
		document.all(this.cards[i].cardName).style.position="absolute";
		document.all(this.cards[i].cardName).style.display="none";
	}	
	
  	str  = "<center><table id='"+this.layoutName+"' cellSpacing=0 cellPadding=0  border=0 width='"+this.width+"' height='"+this.height+"'>";
	str  += "<tr height='20'><td colspan='3'></td></tr>";
	str  += "<tr height='1'><td colspan='3' bgColor='#FFFFFF'></td></tr>";
	str  += "<tr height='"+(this.height-25)+"'>";
	str  += "	<td width='4' ></td>";
	str  += "	<td bgColor='#F7FBFF' style='background-color:#F7FBFF'> &nbsp;</td>";
	str  += "	<td width='4' ></td>";
	str  += "</tr>";
    str  += "<tr height='4'>";
	str  += "    <td width='4'></td>";
	str  += "    <td ></td>";
	str  += "    <td width='4'></td>";
    str  += "</tr>";
	str  += "</table></center> ";
  	document.write(str);

	buttonLeft = document.all(this.layoutName).offsetLeft+3;
	buttonTop = document.all(this.layoutName).offsetTop+2;
	for( i = 0 ; i < this.cards.length ; i++ ) {
		//以下设置大小,大小的设置原则如下:
		//如果源大小小于card大小，则直接放置,不设置大小
		//否则就需要设置到card大小
		source_width = document.all(this.cards[i].cardName).width;
		source_height = document.all(this.cards[i].cardName).height;
		
		
		target_width = document.all(this.layoutName).width;
		target_height = document.all(this.layoutName).height;
		
		var source_width=source_width;
		if(target_width>0)
			source_width = target_width-20;
		
		
		//source_height = target_height;
		document.all(this.cards[i].cardName).width=source_width;
		document.all(this.cards[i].cardName).height=source_height;
		document.all(this.cards[i].cardName).style.pixelLeft=document.all(this.layoutName).offsetLeft+10;
		document.all(this.cards[i].cardName).style.pixelTop=document.all(this.layoutName).offsetTop+30;
		document.all(this.cards[i].cardName).style.zIndex=this.index;
		document.all(this.cards[i].cardName).style.backgroundColor="#F7FBFF";
		//画出Button
  	str = "<table id='tabButton"+this.cards[i].cardName+"' cellSpacing=0 cellPadding=0 border=0 height='18' style='position:absolute;left:"+buttonLeft+";top:"+buttonTop+";z-index:"+this.zIndex+"' onclick=\"changeTab('"+this.index+"', '"+this.cards[i].index+"');_curOldTabButtonObj = this;//设置全局变量，在action.js中的方法'_checkTabButton'中使用\">";
  	str += "	<tr>";
  	str += "		<td rowspan='2' width='2' background='/view/js/control/tab/image/tabButton_left.gif' style='background-repeat:norepeat'></td>";
  	str += "		<td height='1' bgColor='#FFFFFF'></td>";
  	str += "		<td rowspan='2' width='2' background='/view/js/control/tab/image/tabButton_right.gif' style='background-repeat:norepeat'></td>";
  	str += "	</tr>";
  	str += "	<tr class=listtitleborder>";
  	str += "		<td bgColor='' style='cursor:hand' class='"+this.titleCss+"'>&nbsp;"+this.cards[i].name+"&nbsp;</td>";
  	str += "	</tr>";
  	str += "</table>";
  	document.write(str);
		this.zIndex += 1;
  	buttonLeft += document.all("tabButton"+this.cards[i].cardName).offsetWidth;
  	
  	//设置快捷键；"TAB页眉"的鼠标点击事件可由快捷键触发
    addSpeedKeyScript("tabButton"+this.cards[i].cardName, new Array(i+1, "ALT"));
    
  	document.all(this.cards[i].cardName).isTabControl = true;
  	
	}	
	
	
	document.all(this.cards[0].cardName).style.display="";
	_button = document.all("tabButton"+this.cards[0].cardName);
	_button.style.pixelLeft -= 2;
	_button.style.pixelTop -= 2;
	_button.width = _button.offsetWidth + 4;
	_button.height = _button.offsetHeight + 4;
	_button.style.zIndex = this.zIndex;
	this.zIndex += 1;
	this.oldControl = this.cards[0];
}

/*******************************************
 *  卡片式对话框对象                       *
 *******************************************/
tabControls = new Array();

function TabControl( layoutName, width, height ) {
  	this.layoutName = layoutName;
  	this.cards = new Array();
  	this.imgUrls = new Array();
  	this.width = width;
  	this.height = height;
  	this.zIndex = 0;
  	this.oldTab = null;
  	
  	this.index = tabControls.length;
	tabControls[tabControls.length] = this;
} 

/*******************************************
 *  卡片对象                               *
 *******************************************/
function Card(name, cardName, _index) {
  	this.name = name;
  	this.cardName = cardName;
  	this.index = _index;
}

/* 添加对话框。 */
function card_add( name, cardName, imgUrl ) {
	newCard = new Card( name, cardName, this.cards.length );
  	this.cards[this.cards.length] = newCard;	
	this.imgUrls[this.imgUrls.length] = imgUrl;
}

/* 设置卡片头显示的样式。 */
function css_set( cssName ) {
	this.titleCss = cssName;
}

/*选择tab时，切换显示*/
function changeTab( groupID, tabID ) {
	pControl = tabControls[groupID];
	_oldControl = pControl.oldControl;
	_control = pControl.cards[tabID];
	
	document.all(_oldControl.cardName).style.display="none";
	_button = document.all("tabButton"+_oldControl.cardName);
	_button.style.pixelLeft += 2;
	_button.style.pixelTop += 2;
	_button.width = _button.offsetWidth - 4;
	_button.height = _button.offsetHeight - 4;
	
	document.all(_control.cardName).style.display="";
	_button = document.all("tabButton"+_control.cardName);
	_button.style.pixelLeft -= 2;
	_button.style.pixelTop -= 2;
	_button.width = _button.offsetWidth + 4;
	_button.height = _button.offsetHeight + 4;
	_button.style.zIndex = pControl.zIndex;
	pControl.zIndex += 1;
	
	pControl.oldControl = _control;
	
}

TabControl.prototype.add = card_add;
TabControl.prototype.show = show;
TabControl.prototype.setCss = css_set;
