
function get_index_in_array(arrs,val) {
	var i = 0;
	var index = -1;	
	for (i=0; i<arrs.length; i++) {	    
		if (arrs[i] == val) {
			index = i;
			break;
		}
	}	
	return index;
}

// 显示工具条效果图
function show_toolbar_effect(isShowRule,isShowMenu,appRelPath,buttonCodes,buttonStyles,captions,icons,widths,isPostfixSeps) {
	var i;
	var isSetBtns = false;
	var index = -1;
	var onMouseOutjs = "";	
	var btnCount = 0;
	if (buttonCodes != null && buttonCodes != undefined && buttonCodes.length > 0) {
		isSetBtns = true;	
		btnCount = buttonCodes.length;
	}	
	if (isShowRule == null || isShowRule == undefined)
		isShowRule = 0;
	
	if (isShowMenu) {
		onMouseOutjs = " onmouseout=\"HideComponentBySpanID('divTBActionMenu');\" ";
	}	
	
	document.write("<!-- 工具条效果 -->");
	document.write("<table border='0' width='678' height='40' id='tbTBEffect'" + onMouseOutjs + " background='../../images/tbeffect/toolbar_bg.gif' cellspacing='0' cellpadding='0'>");
	document.write(" <tr><td height='4'></td></tr>");
	document.write(" <tr>");
	document.write("	<td valign='top'>");
	document.write("	<table border='0' width='100%' id='tbTBBarW' cellspacing='0' cellpadding='0'>");
	document.write("		<tr>");
	document.write("			<td width='12'>　</td>");
	document.write("			<td>");
	document.write("			<table border='0' id='tbTBBar' width='100%'>");
	document.write("				<tr>");
	document.write("					<td id='tdTBItem_LEFT'><img alt='' src='../../images/tbeffect/toolbarLeft.gif'></td>");
		
	for (i=0; i<buttonCodes.length; i++) {
		write_button_item(isShowMenu,appRelPath,buttonCodes[i],buttonStyles[i],captions[i],icons[i],widths[i],isPostfixSeps[i],i,btnCount);
	}
	
	document.write("					<td width='100%'>　</td>");
	document.write("				</tr>");
	document.write("			</table>");
	document.write("			</td>");
	document.write("		</tr>");
	document.write("	</table>");
	document.write("	</td>");
	document.write(" </tr>");
	document.write("</table>");
	
	if (isShowRule) {
		document.write("<table border='0' width='678' height='20' id='tbTBRule' cellspacing='0' cellpadding='0'>");
		document.write(" <tr><td width='10'></td><td background='../../images/tbeffect/ruler2.gif'>&nbsp; </td></tr>");
		document.write("</table>");
	}
	
	
	document.write("<!-- 工具条效果 -->");
}

// 显示工具条效果图
function show_tb_wizard_effect(isShowRule,isShowMenu,appRelPath,buttonCodes,buttonStyles,captions,icons,widths,isPostfixSeps) {
	var isSetBtns = false;
	var index = -1;
	if (buttonCodes != null && buttonCodes != undefined && buttonCodes.length > 0)
		isSetBtns = true;	
	if (isShowRule == null || isShowRule == undefined)
		isShowRule = 0;
	
	document.write("<!-- 工具条效果 -->");
	document.write("<table border='0' width='678' height='40' id='tbTBEffect' background='../../images/tbeffect/toolbar_bg.gif' cellspacing='0' cellpadding='0'>");
	document.write(" <tr><td height='4'></td></tr>");
	document.write(" <tr>");
	document.write("	<td valign='top'>");
	document.write("	<table border='0' width='100%' id='tbTBBarW' cellspacing='0' cellpadding='0'>");
	document.write("		<tr>");
	document.write("			<td width='12'>　</td>");
	document.write("			<td>");
	document.write("			<table border='0' id='tbTBBar' width='100%'>");
	document.write("				<tr>");
	document.write("					<td id='tdTBItem_LEFT'><img alt='' src='../../images/tbeffect/toolbarLeft.gif'></td>");
		
	if (isSetBtns && (index = get_index_in_array(buttonCodes, "LOGO")) >= 0) {
		write_button_item(isShowMenu,appRelPath,buttonCodes[index],buttonStyles[index],captions[index],icons[index],widths[index],isPostfixSeps[index]);
	}
	else {
		document.write("					<td id='tdTBItem_LOGO_icon'><img alt='' src='../../images/tbeffect/logo_ghost.gif' border='0'></td>");
		document.write("					<td id='tdTBItem_LOGO_arrow'><img alt='' src='../../images/tbeffect/arrow_ghost.gif' border='0'></td>");
	}
	if (isSetBtns && (index = get_index_in_array(buttonCodes, "INPUT_BOX")) >= 0) {
		write_button_item(isShowMenu,appRelPath,buttonCodes[index],buttonStyles[index],captions[index],icons[index],widths[index],isPostfixSeps[index]);
	}
	else
		document.write("					<td id='tdTBItem_INPUT_BOX_icon'><img alt='' src='../../images/tbeffect/input_box_ghost.gif' border='0'></td>");
	
	if (isSetBtns && (index = get_index_in_array(buttonCodes, "SEARCH")) >= 0) {		
		write_button_item(isShowMenu,appRelPath,buttonCodes[index],buttonStyles[index],captions[index],icons[index],widths[index],isPostfixSeps[index]);
	}
	else
		document.write("					<td id='tdTBItem_SEARCH_icon'><img alt='' src='../../images/tbeffect/search_ghost.gif' border='0'></td>");
	
	if (isSetBtns && (index = get_index_in_array(buttonCodes, "BUTTON_WIZARD")) >= 0) {		
		write_button_item(isShowMenu,appRelPath,buttonCodes[index],buttonStyles[index],captions[index],icons[index],widths[index],isPostfixSeps[index]);
	}
	else
		document.write("					<td id='tdTBItem_BUTTON_WIZARD_icon'><img alt='' src='../../images/tbeffect/button_wizard_ghost.gif' border='0'></td>");
	document.write("					<td width='100%'>&nbsp;</td>");
	document.write("				</tr>");
	document.write("			</table>");
	document.write("			</td>");
	document.write("		</tr>");
	document.write("	</table>");
	document.write("	</td>");
	document.write(" </tr>");
	document.write("</table>");
	document.write("<!-- 工具条效果 -->");
}

function write_button_item(isShowMenu,appRelPath,buttonCode,buttonStyle,caption,icon,width,isPostfixSep,btnIndex,btnCount) {
	// alert(buttonStyle);
	// alert(icon);
	// isShowMenu = false;
	if (isShowMenu) { // 增加一个表
		document.write("<td>");
		document.write("  <table border='0' id='tbButton_" + buttonCode + "'cellspacing='0' cellpadding='0'"
		 + " onmouseover=\"ShowTBActionMenu('" + buttonCode + "','" + buttonStyle + "'," + btnIndex + "," + btnCount + ");\"><tr>");
	}
	
	if (buttonStyle == "TEXT_BOX")
		document.write("<td id='tdTBItem_" + buttonCode + "_icon'><input type='text' style='border: darkgray 1px solid; width: " + width + "px;' readonly></td>");
	else if (buttonStyle == "PASSWORD_BOX")
		document.write("<td id='tdTBItem_" + buttonCode + "_icon'><input type='text' style='border: darkgray 1px solid; width: " + width + "px;' value='***' readonly></td>");
	else if (buttonStyle == "COMBO_BOX")
		document.write("<td id='tdTBItem_" + buttonCode + "_icon'><select size='1' style='border: darkgray 1px solid; width: " + width + "px;' disabled></td>");
	else if (buttonStyle == "HTML_DYNAMIC")
		document.write("<td id='tdTBItem_" + buttonCode + "_icon' style='width:" + width + "px' align='center' nowrap><font color='#CC0000'>滚动新闻</font></td>");	
	else if (buttonStyle == "HTML_WEB")
		document.write("<td id='tdTBItem_" + buttonCode + "_icon' style='width:" + width + "px' align='center' nowrap><font color='#0033CC'>网页内容</font></td>");
	
	else if (icon != "" && (icon.indexOf("http://") == 0 || icon.indexOf("https://") == 0))
		document.write("					<td id='tdTBItem_" + buttonCode + "_icon'><img alt='' src='" + icon + "' height='16' border='0'></td>");
	else if (icon != "")
		document.write("					<td id='tdTBItem_" + buttonCode + "_icon'><img alt='' src='" + appRelPath + "icons/" + icon + "' height='16' border='0'></td>");
	
	if (caption != "")
	{
		if (caption.indexOf("[$VALUE|") >= 0 && caption.indexOf("|VALUE$]") >= 0)
			caption = caption.substring(0,caption.indexOf("[$VALUE|")) + "xxx" + caption.substring(caption.indexOf("|VALUE$]")+8);
		else if (caption.indexOf("[$ENABLE|") >= 0 && caption.indexOf("|ENABLE$]") >= 0)
			caption = caption.substring(0,caption.indexOf("[$ENABLE|")) + "E" + caption.substring(caption.indexOf("|ENABLE$]")+9);
		document.write("					<td id='tdTBItem_" + buttonCode + "_caption' nowrap>" + caption + "</td>");
	}
		
	if (buttonStyle == "DROPDOWN" || buttonStyle == "WHOLEDROPDOWN")
		document.write("					<td id='tdTBItem_" + buttonCode + "_arrow'><img alt='' src='../../images/tbeffect/arrow_entity.gif' border='0'></td>");		
		
	if (isPostfixSep == "1")
		document.write("					<td id='tdTBItem_" + buttonCode + "_sep'><img alt='' src='../../images/tbeffect/sep.gif' border='0'></td>");
	
	if (isShowMenu) { // 增加一个表
		document.write("  </tr></table>");
		document.write("</td>");
	}		
}

// 显示菜单
function ShowTBActionMenu(buttonCode, buttonStyle, btnIndex, btnCount)
{	
	var TABLEACTIONSOFFSET = 12;
	var buttonItemID = "tbButton_" + buttonCode;	
	var buttonItemtb = document.all[buttonItemID];
	var actionMenuID = "divTBActionMenu";
	
	document.all["selectButtonCode"].value = buttonCode;
	pos		= GetElementPosition(buttonItemID);

	
	// the focus table
	var tbl			= document.all("tbActionMenuFocus"); //GetElementBySubID(document, "tblActionsFocus");
	tbl.style.width = buttonItemtb.offsetWidth + TABLEACTIONSOFFSET;	
	
	SetSpanActionPosition(actionMenuID, buttonItemtb);
														
	
	//60 instead of (document.all[spnID].offsetWidth / 2)	
	if (buttonCode == "LOGO")
		document.all["menuDelete"].parentElement.style.display				= "none";	
	else
		document.all["menuDelete"].parentElement.style.display				= "inline";	
	document.all["menuEdit"].parentElement.style.display				= "inline";	
	document.all["menuAdd"].parentElement.style.display					= "inline";
	if (btnIndex == 0 || buttonCode == "LOGO")
		document.all["menuMoveLeft"].parentElement.style.display				= "none";	
	else
		document.all["menuMoveLeft"].parentElement.style.display				= "inline";	
	
	if (btnIndex == btnCount-1 || buttonCode == "LOGO")
		document.all["menuMoveRight"].parentElement.style.display				= "none";
	else
		document.all["menuMoveRight"].parentElement.style.display				= "inline";
		
	//document.all["menuComunicate"].parentElement.style.display			= "none";	
	
	//SetMoveLeftMoveRightDisplay(moveLeftID,moveRightID,move);
	
	document.all[actionMenuID].style.display	= "inline";
	tbl.style.display	= "inline";

}

/* --- ComponentsActions --- */
function MenuItem_MouseOver()
{
	// We need to fine the TR
	var tr = event.srcElement;
	while(tr.tagName.toUpperCase() != "TR")
	{
		tr = tr.parentElement;
	}
	tr.style.backgroundColor = "#DAE7EA";
}
function MenuItem_MouseOut()
{
	// We need to fine the TR
	var tr = event.srcElement;
	while(tr.tagName.toUpperCase() != "TR")
	{
		tr = tr.parentElement;
	}
	tr.style.backgroundColor = "#DBD8D2";
}

function SetSpanActionsLeftPosition( spnID, img)
{

	var ofset = -3;	 // tblActions为具体菜单
	if (img.offsetWidth < parseInt(document.getElementById("tblActions").style.width))	//the width of the img is less than the width of the actions span
	{
		//offset will be a nagative number for how much to move the focus table back
		//offset is the difference between the width of the img and the actions table
		ofset =   (img.offsetWidth / 2) - (parseInt(document.getElementById("tblActions").style.width)/ 2) + 3;
	}									 
	document.all[spnID].style.left = pos.left+ofset

}


function SetSpanActionsTopPosition( spnID)
{
	var ofset = 0;	
	
	var tbl = document.all("tbTBEffect"); //GetElementBySubID(document, "tblToolbar");
	
	var tblPos = GetElementPosition(tbl.id);	
	
	document.all[spnID].style.top = tblPos.top+ofset
	
	
	
}

function SetSpanActionPosition( spnID, img)
{
	SetSpanActionsLeftPosition( spnID, img);
	
	SetSpanActionsTopPosition( spnID)
}