function IconMapping(sFileName){
	var sFilePath=GetRootVDir()+"image/"+sFileName;
	if(sFileName.charAt(0)=="/" || sFileName.substring(0,5)=="http:") sFilePath = sFileName ;
	
	return "<img src='"+sFilePath+"' valign='bottom' border='0'>";
}
///  Make Page Block 
/*	
	sample:
	var Tabs=new cTabs();
	Tabs.sRootVDir="/LMS2.0/";
	Tabs.sPicName="1";
	Tabs.init();
	//title,href,selected,target
	Tabs.add("JavaScript","javascript:alert('aaa')",false);
	Tabs.add("left","javascript:alert('aaa')",false,"menu");//title,href,selected,target
	Tabs.add("Kimo","http://www.kimo.com.tw",false,"menu");
*/
var sMakePage=new Array();
function cTabs(){
	this.sMakePage=sMakePage;
	//---- Base Date can't change block  ----//
	this.sMakePage["LayerCount"]="0";
	this.sMakePage["ClickPage"]="";
	this.sMakePage["lastClickPage"]="";
	//---- end of Base Date can't change block ----//
	/*
	.tabClickColor{color:#55290B;cursor:hand}
	.tabOverColor{color:#1B2533;cursor:hand}
	.tabOutColor{color:#1B2533;cursor:hand}

	*/
	this.sRootVDir=GetRootVDir();
	this.sPicName="contenttabs";
	this.nTabHeight=20; //senyann update 2004-08-17
	this.sLmsStyle = GetCurrentStyle();
	this.sClickColor="#1B2533";
	this.sOverColor="#ffffff";
	this.sOutColor="#1B2533";
	this.init=cTabs_Init;
	this.add=cTabs_Add;	
	this.setFont=cTabs_SetFont;	
	this.iconMapping=IconMapping;
}
function cTabs_Init(){
	//update by senyann
	if(this.sPicName=="menutabs" || this.sPicName=="menuTabs") 
		sPicName = this.sPicName;
	else 
		sPicName= "contenttabs";	
	var sImageDir=this.sRootVDir+"image/style"+this.sLmsStyle+"/"+sPicName+"/";//senyann update 2004-08-17
	this.sMakePage["blockLeftClickPic"] =sImageDir+"tab_left_click.gif";
	this.sMakePage["blockLeftOverPic"] =sImageDir+"tab_left_over.gif";
	this.sMakePage["blockLeftOutPic"] =sImageDir+"tab_left_out.gif";		
	this.sMakePage["blockMidClickPic"]  =sImageDir+"tab_middle_click.gif";
	this.sMakePage["blockMidOverPic"]  =sImageDir+"tab_middle_over.gif";
	this.sMakePage["blockMidOutPic"]  =sImageDir+"tab_middle_out.gif";
	this.sMakePage["blockRightClickPic"]=sImageDir+"tab_right_click.gif";
	this.sMakePage["blockRightOverPic"]=sImageDir+"tab_right_over.gif";
	this.sMakePage["blockRightOutPic"]=sImageDir+"tab_right_out.gif";
	this.setFont();
}
function cTabs_SetFont(){
	//alert(this.sLmsStyle)
	switch(this.sLmsStyle){
		case "1":
			this.sMakePage["blockClickColor"]="#55290B";
			this.sMakePage["blockOverColor"]="#1B2533";
			this.sMakePage["blockOutColor"]="#1B2533";
		break;
		case "2":
			this.sMakePage["blockClickColor"]="#55290B";
			this.sMakePage["blockOverColor"]="#1B2533";
			this.sMakePage["blockOutColor"]="#1B2533";
		break;
		default: // conmon text
	}
	if(this.sLmsStyle=="top_button"){}
}

function cTabs_Add(title,href,selected,target,icon){
	if(sMakePage["LayerCount"]) sMakePage["LayerCount"]=Math.ceil(sMakePage["LayerCount"])+1;
	//alert(sMakePage["LayerCount"])
	var id="Area"+sMakePage["LayerCount"]
	href=href.replace(/'/g,"\"");
	href=href.replace(/"/g,"\"");
	var leftSrc=sMakePage["blockLeftOutPic"];
	var midSrc=sMakePage["blockMidOutPic"];
	var rightSrc=sMakePage["blockRightOutPic"];
	
	//var fontColor=sMakePage["blockOutColor"];
	var fontColor="tabOutColor";
	
	if(selected){
		sMakePage["lastClickPage"]="block_"+id;
		sMakePage["ClickPage"]="block_"+id;
	
		leftSrc=sMakePage["blockLeftClickPic"];
		midSrc=sMakePage["blockMidClickPic"];
		rightSrc=sMakePage["blockRightClickPic"];
		//fontColor=sMakePage["blockClickColor"];
		fontColor="tabClickColor"
	}
	var TWidth=(getCharLen(title)+2)*7 
	if(icon!=null) TWidth+=20;
	
	//alert(TWidth);
	  var Str="<table id='block_"+id+"' block_href='"+href+"' block_targer='"+target+"' width='"+TWidth+"' height='"+this.nTabHeight+"' border='0' cellpadding='0' cellspacing='0' align='left'"
			 +" onmouseout='blockStatus(\"mouseOut\",this,\""+id+"\")'"
			 +" onmouseover='blockStatus(\"mouseOver\",this,\""+id+"\")'"
			 //+" style='color:"+fontColor+";cursor:hand'>\n"
	         +" class='"+fontColor+"'>\n"
	         +"  <tr>\n"
             +"	<td width='4'><img id='blockLeft_"+id+"' src='"+leftSrc+"' width='4' height='"+this.nTabHeight+"'></td>\n"
             +"	<td id='blockBG_"+id+"' align='center' valign='bottom' background='"+midSrc+"' nowrap='true'>\n		"
		if(icon!=null && icon !="") Str   +=this.iconMapping(icon)+"&nbsp;";
	   Str   +=title
             +"\n	</td>\n"
			 +"	<td width='4'><img id='blockRight_"+id+"' src='"+rightSrc+"' width='4' height='"+this.nTabHeight+"'></td>\n"
			 +"  </tr>\n"
			 +"</table>";
	//alert(Str)
	document.write(Str);
	document.all["block_"+id].onclick=blockStatus;
}
function blockStatus(type,Obj,id,href,target){
	if(Obj){
		sMakePage["href"]=String(Obj.block_href).replace(/javascript/i,"javascript");
		sMakePage["target"]=Obj.block_targer;
		sMakePage["id"]=Obj.id;
	}
	if(type=="mouseOver"){
		changeStyle(id,type)
	}
	else if(type=="mouseOut"){
		//alert(sMakePage["ClickPage"]);
		if(sMakePage["ClickPage"]==sMakePage["id"]){
			changeStyle(id,"click");
		}
		else if(sMakePage["ClickPage"]!=sMakePage["id"]){
			if(id!=""){
				changeStyle(id,type);
			}
		}else{
			sMakePage["lastClickPage"]=sMakePage["id"]
		}
	}
	else{
		sMakePage["ClickPage"]=sMakePage["id"];
		if(sMakePage["lastClickPage"]!="" && sMakePage["ClickPage"]!=sMakePage["lastClickPage"]){
			var name=String(sMakePage["lastClickPage"]).split("_");
			id=name[1];
			changeStyle(id,"mouseOut");
			changeStyle(String(sMakePage["ClickPage"]).replace("block_",""),"click");
			sMakePage["lastClickPage"]=sMakePage["id"];
		}		
		if(sMakePage["href"].indexOf("javascript")==-1){
			if(sMakePage["target"]=="undefined" || sMakePage["target"]==""){
				location=sMakePage["href"];
				waitProcess();
			}
			else{
				if(top.frames[sMakePage["target"]])
					top.frames[sMakePage["target"]].location.href=sMakePage["href"];
				else{
					var H=400;
					var W=405;
					window.open(sMakePage["href"],sMakePage["target"],"scrollbars=1,width="+W+",height="+H+",top="+(screen.height-H)/2+",left="+(screen.width-W)/2);	
				}
			}
		}else{
			eval(sMakePage["href"]);
		}
	}
}

function changeStyle(id,type){
	//alert(type);
	var leftSrc=sMakePage["blockLeftOutPic"];
	var midSrc=sMakePage["blockMidOutPic"];
	var rightSrc=sMakePage["blockRightOutPic"];
	var fontColor=sMakePage["blockOutColor"];
	var sTabClass="tabOutColor";
	if(type=="mouseOver"){
		leftSrc=sMakePage["blockLeftOverPic"];
		midSrc=sMakePage["blockMidOverPic"];
		rightSrc=sMakePage["blockRightOverPic"];
		fontColor=sMakePage["blockOverColor"];
		sTabClass="tabOverColor";
	}
	else if(type=="click"){
		leftSrc=sMakePage["blockLeftClickPic"];
		midSrc=sMakePage["blockMidClickPic"];
		rightSrc=sMakePage["blockRightClickPic"];
		fontColor=sMakePage["blockClickColor"];
		sTabClass="tabClickColor";
	}
	document.all["blockLeft_"+id].src=leftSrc;
	document.all["blockBG_"+id].background=midSrc;
	document.all["blockRight_"+id].src=rightSrc;
	//document.all["block_"+id].style.color=fontColor;
	document.all["block_"+id].className=sTabClass;
}
function getCharLen(pStr){	
	var index;							
	var EachChar;							
	var	iCharLens=0;						
	var iStrLen=pStr.length;					
	for(index=0;iStrLen>index ;index++){
		EachChar=pStr.substr(index,1)			
		if(escape(EachChar).length>3||'!' > escape(EachChar) ||escape(EachChar)>'~')
			iCharLens=iCharLens+2;				
		else
			iCharLens=iCharLens+1;				
	}
	return iCharLens;
}

///============================================================================================
/// cBlockTable block
/* 
	sample:

	document.onclick=showPopMenu; 
	function showManage(PK){
		var cBT=new cBlockTable();
		cBT.add('kimo','http://www.kimo.com.tw');
		cBT.add('kimo in new frame','http://www.kimo.com.tw','_new');
		cBT.add('<ldstr:text strID="modify" doc="default"/>','javascript:go("addMeta",'+PK+')','','ask_edit.gif');
		cBT.add('<ldstr:text strID="delete" doc="default"/>','javascript:DelCheck('+PK+')','','ask_del.gif');
		cBT.init();
	}
*/
function cBlockTable(){
	this.bExit=false;
	this.nWidth=200;
	this.nIconWidth=30;
	this.ItemContent=new Array();
	this.init=cBlockTable_Init;
	this.add=cBlockTable_Add;
	this.iconMapping=IconMapping;
}

function cBlockTable_Init(){
	if(!document.all["BlockMenu"])	makeBlock(this.nWidth,this.bExit);
	document.all["BlockMenu"].style.display="";
	document.all["BlockMenu"].style.width=this.nWidth;
	var ItemContent=this.ItemContent;
	
	/*
	if(event.screenX+this.nWidth > screen.Width){
		document.all["BlockMenu"].style.left='';
		document.all["BlockMenu"].style.right=10;
	}
	else{
		document.all["BlockMenu"].style.right='';
		document.all["BlockMenu"].style.left=event.x;
	}
	document.all["BlockMenu"].style.top=document.body.scrollTop+event.y;
	*/
	var width = document.body.clientWidth;
	var height = document.body.clientHeight;
	var x = window.event.clientX;
	var y = window.event.clientY;
	var e = document.documentElement, b = document.body;
	var posX = x + (e.scrollLeft || b.scrollLeft);
	var posY = y + (e.scrollTop || b.scrollTop);
     
    //alert("X="+x+",Y="+y+",posX="+posX+",posY="+posY);
    //alert(this.ItemContent.length);

	var sMenuInfo="<table id='BlockMenuTable' cellpadding='2' cellspacing='0' bgcolor='#FFFFFF'>\n"
	var oDate=new Date();
	for(i=0;i<ItemContent.length;i++){
		if(ItemContent[i][1].indexOf("javascript")==-1 && ItemContent[i][1]!=""){
			ItemContent[i][1] = (ItemContent[i][1].indexOf("?")==-1) ? ItemContent[i][1] +="?" : ItemContent[i][1] +="&" ;
			ItemContent[i][1] +="dateObjectGetTime="+oDate.getTime();
		}
		if(ItemContent[i][1]==""){
			sMenuInfo +="	<tr class='BlockTdTitle' id='popMenuItem'>\n"
					   +"		<td colspan='2'><div style='overflow:hidden; text-overflow:ellipsis; width:"+(this.nWidth - this.nIconWidth )+"px;' title='"+ItemContent[i][0]+"'>"+ItemContent[i][0]+"</div></td>\n"
					   +"	</tr>\n";
		}else{
			sMenuInfo +="	<tr class='BlockTd2' id='popMenuItem' onmouseover='this.className=\"BlockTd1\"' onmouseout='this.className=\"BlockTd2\"'";
			sMenuInfo +=" style='cursor:hand;'";

			if(ItemContent[i][1].indexOf("javascript")!=-1){
			    //
			    //  javascript:xxx
			    //
				ItemContent[i][1]=ItemContent[i][1].replace(/javascript:/g,"");
				sMenuInfo +=" onclick='"+ItemContent[i][1]+"'";
			    sMenuInfo +=">\n"
					    +"		<td valign='top' class='BlockTd1' width='"+this.nIconWidth+"' align='center'>"
			    if(ItemContent[i][3]!=""){
				    sMenuInfo +=this.iconMapping(ItemContent[i][3]);
			    }else{
				    sMenuInfo +="&nbsp;";
			    }
			    sMenuInfo +="</td>\n"
					    +"		<td id='popMenuItem' valign='top' nowrap='true'  width='"+(this.nWidth - this.nIconWidth )+"'>"
					    +"<div style='overflow:hidden; text-overflow:ellipsis; width:"+(this.nWidth - this.nIconWidth )+"px;' title='"+ItemContent[i][0]+"'>"+ItemContent[i][0]+"</div></td>\n"
					    +"	</tr>\n";	
			}
			else {//if(ItemContent[i][2]==""){
			    //
			    //  normal hyperlink
			    //
				//sMenuInfo +=" onclick='document.location=\""+ItemContent[i][1]+"\"'";
			    sMenuInfo +=">\n"
					    +"		<td valign='top' class='BlockTd1' width='"+this.nIconWidth+"' align='center'>"
			    if(ItemContent[i][3]!=""){
				    sMenuInfo +=this.iconMapping(ItemContent[i][3]);
			    }else{
				    sMenuInfo +="&nbsp;";
			    }
			    sMenuInfo +="</td>\n"
					    +"		<td id='popMenuItem' valign='top' nowrap='true'  width='"+(this.nWidth - this.nIconWidth )+"'>"
					    +"<a href='"+ItemContent[i][1]+"' target='"+ItemContent[i][2]+"' style='cursor:hand;'><div style='overflow:hidden; text-overflow:ellipsis; width:"+(this.nWidth - this.nIconWidth )+"px;' title='"+ItemContent[i][0]+"'>"+ItemContent[i][0]+"</div></a></td>\n"
					    +"	</tr>\n";	
			}
			/*
			else{
			    //
			    //  link with specified frame
			    //
				if(top.frames[ItemContent[i][2]])
					sMenuInfo +=" onclick='top.frames[\""+ItemContent[i][2]+"\"].location.href=\""+ItemContent[i][1]+"\"'";
				else{
					if(ItemContent[i][4]==""){
						var H=400;
						var W=600;
						sMenuInfo +=" onclick='window.open(\""+ItemContent[i][1]+"\",\""+ItemContent[i][2]+"\",\"scrollbars=1,resizable=1,width="+(screen.width-100)+",height="+(screen.height-100)+",top="+(screen.height-H)/2+",left="+(screen.width-W)/2+"\")'";	
					}else{
						sMenuInfo +=" onclick='window.open(\""+ItemContent[i][1]+"\",\""+ItemContent[i][2]+"\",\""+ItemContent[i][4]+"\")'";
					}
				}
			    sMenuInfo +=">\n"
					    +"		<td valign='top' class='BlockTd1' width='"+this.nIconWidth+"' align='center'>"
			    if(ItemContent[i][3]!=""){
				    sMenuInfo +=this.iconMapping(ItemContent[i][3]);
			    }else{
				    sMenuInfo +="&nbsp;";
			    }
			    sMenuInfo +="</td>\n"
					    +"		<td id='popMenuItem' valign='top' nowrap='true'  width='"+(this.nWidth - this.nIconWidth )+"'>"
					    +"<div style='overflow:hidden; text-overflow:ellipsis; width:"+(this.nWidth - this.nIconWidth )+"px;' title='"+ItemContent[i][0]+"'>"+ItemContent[i][0]+"</div></td>\n"
					    +"	</tr>\n";	
			}*/
		}
	}
	sMenuInfo+="</table>";
	//alert(sMenuInfo);
	document.all["BlockContent"].innerHTML="<div style='WIDTH:"+(this.nWidth+12)+"px' class='BlockTable'>"+sMenuInfo+"</div>";	
	
	var itemContentHeight = document.getElementById("BlockMenuTable").clientHeight;
	var itemContentWidth = document.getElementById("BlockMenuTable").clientWidth;
	if (x > (width - itemContentWidth )) posX = posX - itemContentWidth ;
	//if (y > (height - itemContentHeight )) posY = posY - itemContentHeight ;
	document.all["BlockMenu"].style.left = posX;
	document.all["BlockMenu"].style.top = posY;
	document.all["BlockContent"].childNodes[0].style.height = document.getElementById("BlockMenuTable").clientHeight + 4 + "px";
	document.all["BlockContent"].style.zIndex = document.all.length + 1;



    {
        //
        //  2007-10-03, add by andrew wu. update document.all.dummy_div.innerHTML to enforce DHTML rerender.
        //
        var div = document.all.dummy_div;
        if (div == null) {
            div = document.createElement("DIV");
            div.id = "dummy_div";
            document.body.appendChild(div);
        }	
        div.innerHTML = "";
    }
}
function cBlockTable_Add(name,href,target,icon,targetStyle){
	if(href==null) href="";
	else href=href.replace(/javascript/i,"javascript");
	
	if(target==null) target="";
	if(icon==null) icon="";
	if(targetStyle==null) targetStyle="";
	this.ItemContent[this.ItemContent.length]=new Array(name,href,target,icon,targetStyle);
}
function makeBlock(nWidth,bExit,bStatus){
	if(!document.all["BlockMenu"]){
		var Str= "<div id='BlockMenu' class='table' style='position:absolute;width:"+nWidth+"px;border: 1px; z-Index:999999;'>";
		bExit = false;
		if(bExit==true){
			//alert(bExit);
			Str+="	<div class='thead' style='text-align:right'>"
				+"		<img src='img/quit.gif' style='cursor:hand' onclick='document.all.BlockMenu.style.display=\"none\";'/>"
				+"	</div>";
		}
		 Str+="	<div id='BlockContent'></div>"
			 +"</div>";
		_tmpDiv = document.createElement('div');
		_tmpDiv.innerHTML = Str;
		
	    document.body.appendChild(_tmpDiv);
	}
	if(bStatus==false) document.all["BlockMenu"].style.display="none";
}
function MakeMenu(){
	window.event.returnValue = false;    
	var cBT=new cBlockTable();
	for(var jFor=0;jFor< arguments.length;jFor++){
		var oTmp=arguments[jFor].split(",");
		if(oTmp.length==1){
			cBT.add(oTmp[0]);	
		}else if(oTmp.length==2){
			cBT.add(oTmp[0],oTmp[1]);	
		}else if(oTmp.length==3){
			cBT.add(oTmp[0],oTmp[1],oTmp[2]);	
		}else if(oTmp.length==4){
			cBT.add(oTmp[0],oTmp[1],oTmp[2],oTmp[3]);	
		}
	}
	cBT.init();		
}
function showPopMenu(bTempExit){
	var bExit=(bTempExit)?bTempExit:false;
	bExit=false;
	var cBT=new cBlockTable();
	//alert(event.srcElement.id)
	if(event.srcElement.id=="popMenu"){
		eval(event.srcElement.MouseClick);
	}
	else if(event.srcElement.id=="popMenuItem"){}
	else makeBlock(cBT.nWidth,bExit,false)
}
/// End of cBlockTable block
