var nS=new Array;
var nL=new Array;
var hL=new Array;
var nTCode=new Array;
var AnimStep=0;
var AnimHnd=0;
var HTHnd=new Array;
var DoFormsTweak=true;
var MenusReady=false;
var SelSndId=0;
var mFrame;
var cFrame;
var OpenMenus=new Array;
var nOM=0;
var mX;
var mY;
var HideSpeed=300;
var TimerHideDelay=2000;
var TimerHideHnd=0;
var IsOverHS=false;
var cntxMenu='';
var IsContext=false;
var BV=parseInt(navigator.appVersion.indexOf("MSIE")>0?navigator.appVersion.split(";")[1].substr(6):navigator.appVersion);
var BN=navigator.appName;
var IsWin=(navigator.userAgent.indexOf('Win')!=-1);
var IsMac=(navigator.userAgent.indexOf('Mac')!=-1);
var OP=(navigator.userAgent.indexOf('Opera')!=-1&&BV>=4)?true:false;
var NS=(BN.indexOf('Netscape')!=-1&&(BV==4)&&!OP)?true:false;
var SM=(BN.indexOf('Netscape')!=-1&&(BV>=5)||OP)?true:false;
var IE=(BN.indexOf('Explorer')!=-1&&(BV>=4)||SM)?true:false;
if(!eval(frames['self'])) {
	frames.self=window;
	frames.top=top;
}
cFrame=eval(frames['self']);
var fx=2;
var tbUseToolbar=false;
var lmcHS=null;

function GetCurCmd(e) {
 	if(SM)
		 cc=e;
	else {
		 cc=mFrame.window.event;
		if(!cc) cc=cFrame.window.event;
		cc=cc.srcElement;
	}
	while(cc.id=="") {
		cc=cc.parentElement;
		if(cc==null) break;
	}
	return cc;
}

function HoverSel(mode,e) {
	var imgLName = "";
	var imgRName = "";
	var nS;
	var mc;
	if(mode==0&&OpenMenus[nOM].SelCommand!=null)
		HoverSel(1);
	if(mode==0) {
		mc=GetCurCmd(e);
		if(nOM>1)
			if(mc==OpenMenus[nOM-1].SelCommand)
				return false;
			if(BV>4)
				while(mc.parentNode.parentNode.id!=OpenMenus[nOM].mName)
					Hide();
			else
				if(OpenMenus[nOM].SelCommand||nOM>1)
					while(!InMenu()&&nOM>1)
						Hide();
			if(OP)
				mc.hasBorder=true;
			else {
				mc.hasBorder=mc.style.borderLeft.split(" ").length==3;
				if(OpenMenus[nOM].frmt.backgroundImage!="")
					mc.hasBorder=false;
			}
			OpenMenus[nOM].SelCommand=mc;
			OpenMenus[nOM].SelCommandPar=[imgLName,imgRName,SM?mc.getAttribute("style"):mc.style.cssText];
			if(SelSndId)
				PlaySound();
	}
	else {
		mc=(mode==1)?OpenMenus[nOM].SelCommand:OpenMenus[nOM].Opener;
		imgLName=(mode==1)?OpenMenus[nOM].SelCommandPar[0]:OpenMenus[nOM].OpenerPar[0];
		imgRName=(mode==1)?OpenMenus[nOM].SelCommandPar[1]:OpenMenus[nOM].OpenerPar[1];
		nS=(mode==1)?OpenMenus[nOM].SelCommandPar[2]:OpenMenus[nOM].OpenerPar[2];
		mc.style.background="";
		if(SM)
			mc.setAttribute("style",nS);
		else
			mc.style.cssText=nS;
		window.status="";
		OpenMenus[nOM].SelCommand=null;
	}
	return true;
}

function NSHoverSel(mode,mc,bcolor) {
	var n;
	var LayerHTML;
	window.clearTimeout(CBparseInt(HTHnd[nOM]));
	HTHnd[nOM]=0;
	if(!nOM)
		return false;
	if(mode==0&&OpenMenus[nOM].SelCommand!=null)
		NSHoverSel(1);
	if(mode==0) {
		//mc=mc.parentLayer.layers[mc.name.substr(0,mc.name.indexOf("EH"))];
		//n=CBparseInt(mc.name.substr(2));
		if(nOM>1)
			if(mc==OpenMenus[nOM-1].SelCommand)
				return false;
		while(!InMenu()&&nOM>1)
			Hide();
		//LayerHTML=hL[n];
		OpenMenus[nOM].SelCommand=mc;
		OpenMenus[nOM].SelCommandPar=mc.bgColor;
		//mc.bgColor=(LayerHTML.split("|")[1]!="")?null:bcolor;
		if(SelSndId)
			PlaySound();
	}
	else {
		mc=(mode==1)?OpenMenus[nOM].SelCommand:OpenMenus[nOM].Opener;
		//bcolor=(mode==1)?OpenMenus[nOM].SelCommandPar:OpenMenus[nOM].OpenerPar;
		//n=CBparseInt(mc.name.substr(2));
		//LayerHTML=nL[n];
		//mc.bgColor=(mc.parentLayer.background.src!=""||LayerHTML.split("|")[1]!="")?null:bcolor;
		//window.status="";
		OpenMenus[nOM].SelCommand=null;
	}
	//mc.clip.width=mc.parentLayer.clip.width-2*mc.left;
	//mc.document.open();
	//mc.document.write(LayerHTML.split("|")[0]);
	//mc.document.close();
	//if(LayerHTML.split("|")[1]!="")
	//	mc.background.src=LayerHTML.split("|")[1];
	return true;
}

function Hide() {
	window.clearTimeout(HTHnd[nOM]);
	HTHnd[nOM]=0;
	window.clearTimeout(AnimHnd);
	AnimHnd=0;
	window.clearTimeout(TimerHideHnd);
	if(OpenMenus[nOM].SelCommand!=null) {
		if(IE) HoverSel(1);
		if(NS) NSHoverSel(1);
	}
	if(OpenMenus[nOM].Opener!=null) {
		if(IE) HoverSel(3);
		if(NS) NSHoverSel(3);
	}
	OpenMenus[nOM].visibility="hidden";
	nOM--;
	if(nOM>0)
		if(!InMenu())
			HTHnd[nOM]=window.setTimeout("Hide()",HideSpeed/4);
	if(nOM==0) {
		if(tbUseToolbar&&lmcHS) {
			if(IE) hsHoverSel(1);
			if(NS) hsNSHoverSel(1);
		}
		//FormsTweak("visible");
	}
}
	
function ShowMenu(mName,x,y,isCascading,hsImgName,algn) {
	var MenuID = "";
	window.clearTimeout(CBparseInt(HTHnd[nOM]));
	HTHnd[nOM]=0;
	x=CBparseInt(x);
	y=CBparseInt(y);
	if(IE)
		if(BV>4)
			 Menu=mFrame.document.getElementById(mName);
		else
			 Menu=mFrame.document.all[mName];
	if(NS)
		var Menu=mFrame.document.layers[mName];
	if(!Menu)
		return false;
	MenuID = Menu.id;
	if(IE) {
		//if(MenuID != "toolbox")
			//Menu.style.width = document.images["img_" + mName].width + 2;
		Menu=Menu.style;
		if(BV>4)
			Menu.frmt=mFrame.document.getElementById(mName+"frmt").style;
		else
			Menu.frmt=mFrame.document.all[mName+"frmt"].style;
	}
	if(Menu==OpenMenus[nOM])
		return false;
//	if(MenuID == "toolbox")
//		fx = 4;
//	else
//		fx = 1;
	if(AnimHnd&&nOM>0) {
		AnimStep=100;
		Animate();
	}
	if(!isCascading) {
		var oldlmcHS=lmcHS;
		lmcHS=null;
		HideAll();
		lmcHS=oldlmcHS;
	}
	Menu.mName=mName;
	Menu.Opener=nOM>0?OpenMenus[nOM].SelCommand:null;
	Menu.OpenerPar=nOM>0?OpenMenus[nOM].SelCommandPar:null;
	Menu.SelCommand=null;
	if(OP) {
		Menu.width=Menu.pixelWidth;
		Menu.height=Menu.pixelHeight;
	}
	if(!isCascading) {
		if(hsImgName) {
			var imgObj=cFrame.document.images[hsImgName.split("|")[0]];
			if(imgObj) {
				var tbMode=hsImgName.split("|")[1];
				if(tbMode&2)
					x=AutoPos(Menu,imgObj,algn)[0];
				if(tbMode&1)
					y=AutoPos(Menu,imgObj,algn)[1];
			}
		}
	}
	var pW=GetWidthHeight()[0]+GetLeftTop()[0];
	var pH=GetWidthHeight()[1]+GetLeftTop()[1];
	if(IE) {
		if(isCascading) {
			x=CBparseInt(OpenMenus[nOM].left)+CBparseInt(OpenMenus[nOM].width)-6;
			y=CBparseInt(OpenMenus[nOM].SelCommand.style.top)+CBparseInt(OpenMenus[nOM].top)-y;
			x += -3;
			y += 1;
			Menu.left=(x+CBparseInt(Menu.width)>pW)?CBparseInt(OpenMenus[nOM].left)-CBparseInt(Menu.width):x;
			Menu.top= (y+CBparseInt(Menu.height)>pH)?pH-CBparseInt(Menu.height):y;
		}
		else {
			Menu.left=(x+CBparseInt(Menu.width)>pW)?pW-CBparseInt(Menu.width):x;
			Menu.top= (y+CBparseInt(Menu.height)>pH)?pH-CBparseInt(Menu.height)-2:y;
		}
		if(IsWin&&!SM)
			Menu.clip="rect(0 0 0 0)";
	}
	if(NS) {
		if(isCascading) {
			x=OpenMenus[nOM].left+OpenMenus[nOM].clip.width-6;
			y=OpenMenus[nOM].top+OpenMenus[nOM].SelCommand.top;
			x += -3;
			y += 1;
			x=(x+Menu.w>pW)?OpenMenus[nOM].left-Menu.w+6:x;
			y=(y+Menu.h>pH)?pH-Menu.h:y;
		}
		else {
			x=(x+Menu.w>pW)?pW-Menu.w:x;
			y=(y+Menu.h>pH)?pH-Menu.h:y;
		}
		Menu.clip.width=0;
		Menu.clip.height=0;
		Menu.moveToAbsolute(x,y);
	}
	if(isCascading)
		Menu.zIndex=CBparseInt(OpenMenus[nOM].zIndex)+1;
	OpenMenus[++nOM]=Menu;
	if(NS)
		FillCommands();
	if(SM&&!OP)
		FixCommands(mName);
	Menu.visibility="visible";
	HTHnd[nOM]=0;
	if((IE&&IsWin&&!SM)||NS)
		AnimHnd=window.setTimeout("Animate()",10);
	//FormsTweak("hidden");
	if(!isCascading&&!IsContext) {
		IsOverHS=true;
		IsContext=false;
	}
	window.clearTimeout(TimerHideHnd);
	TimerHideHnd=window.setTimeout("AutoHide()",TimerHideDelay);
	if(SelSndId)
		PlaySound();
	return true;
}
	
function FixCommands(mName) {
	var m=mFrame.document.getElementById(mName);
	if(!m.Fixed) {
		var j;
		var sd=m.getElementsByTagName("DIV");
		var b=GetBorderWidth(sd[0].style.borderLeft);
		sd[0].style.width=CBparseInt(sd[0].style.width)-2*b;
		sd[0].style.height=CBparseInt(sd[0].style.height)-2*b;
		for(i=0;i<(sd.length-1)/2;i++) {
			j=(i*2)+1;
			if(sd[j].style.borderLeft.indexOf("none")==-1) {
				sd[j].style.width=CBparseInt(sd[j].style.width)-2*GetBorderWidth(sd[j].style.borderLeft);
				sd[j].style.height=CBparseInt(sd[j].style.height)-2*GetBorderWidth(sd[j].style.borderTop);
			}
		}
	}
	m.Fixed=true;
}

function FillCommands() {
	var LayerHTML;
	var n;
	var m=OpenMenus[nOM].layers[0];
	if(!m.Filled) {
		for(var sl=0;sl<m.layers.length-1;sl++) {
			var slo=m.layers[sl];
			if(slo.name.indexOf("EH")==-1&&slo.name.indexOf("MC")==0) {
				slo.clip.width=slo.parentLayer.clip.width-2*slo.left;
				n=CBparseInt(slo.name.substr(2));
				LayerHTML=nL[n];
				slo.document.open();
				slo.document.write(LayerHTML.split("|")[0]);
				slo.document.close();
				if(LayerHTML.split("|")[1]!="")
					m.layers["MC"+n].background.src=LayerHTML.split("|")[1];
			}
		}
	}
	m.Filled=true;
}

function Animate() {
	var r='';
	var nw=nh=0;
	switch(fx) {
		case 1:
			if(IE)
				r="0 "+AnimStep+"% "+AnimStep+"% 0";
			if(NS)
				nw=AnimStep;
			nh=AnimStep;
			break;
		case 2:
			if(IE)
				r="0 100% "+AnimStep+"% 0";
			if(NS)
				nw=100;
			nh=AnimStep;
			break;
		case 3:
			if(IE)
				r="0 "+AnimStep+"% 100% 0";
			if(NS)
				nw=AnimStep;
			nh=100;
			break;
		case 4:
			if(IE)
				r="0 100% 100% "+(100 - AnimStep)+"%";
			if(NS)
				nw=AnimStep;
			nh=100;
			break;
		case 0:
			if(IE)
				r="0 100% 100% 0";
			if(NS)
				nw=100;
			nh=100;
			break;
	}
	if(OpenMenus[nOM]) {
		with(OpenMenus[nOM]) {
			if(IE) clip= "rect("+r+")";
			if(NS) {
				clip.width=w*(nw/100);
				clip.height=h*(nh/100);
			}
		}
		AnimStep += 10;
		if(AnimStep<=100)
			AnimHnd=window.setTimeout("Animate()",20);
		else {
			window.clearTimeout(AnimHnd);
			AnimStep=0;
			AnimHnd=0;
		}
	}
}


function InMenu() {
	var m=OpenMenus[nOM];
	if(!m)
		return false;
	if(IE&&!SM)
		SetPointerPos();
	var l=CBparseInt(m.left);
	var r=l+(IE?CBparseInt(m.width):m.clip.width);
	var t=CBparseInt(m.top);
	var b=t+(IE?CBparseInt(m.height):m.clip.height);
	return ((mX>=l&&mX<=r)&&(mY>=t&&mY<=b));
}

function SetPointerPos(e) {
	if(IE) {
		if(!SM) {
			if(mFrame!=cFrame||event==null)
				if(mFrame.window.event==null)
					return;
				else
					e=mFrame.window.event;
			else
				e=event;
		}
		mX=e.clientX+GetLeftTop()[0];
		mY=e.clientY+GetLeftTop()[1];
	}
	if(NS) {
		mX=e.pageX;
		mY=e.pageY;
	}
}

function HideMenus(e) {
	if(nOM>0) {
		SetPointerPos(e);
		if(OpenMenus[nOM].SelCommand!=null)
			if(!InMenu()&&!HTHnd[nOM])
				HTHnd[nOM]=window.setTimeout("if(nOM>0)if(!InMenu())Hide()",HideSpeed);
	}
}

function FormsTweak(state) {
	if(DoFormsTweak&&IE)
		for(var f=0;f <= (mFrame.document.forms.length-1);f++)
			for(var e=0;e <= (mFrame.document.forms[f].elements.length-1);e++)
				if(mFrame.document.forms[f].elements[e].type=="select-one")
					mFrame.document.forms[f].elements[e].style.visibility=state;
	if(DoFormsTweak&&NS)
		var f=mFrame.document.layers["theForm"];
	if(f)
		f.visibility=state;
}

function execURL(url,tframe) {
	window.setTimeout("execURL2('"+url+"','"+tframe+"')",100);
}

function execURL2(url,tframe) {
	var fObj=eval(rStr(tframe));
	url=rStr(url);
	url.indexOf("javascript")!=url.indexOf("vbscript")?eval(url):fObj.location.href=url;
}

function rStr(s) {
	s=xrep(s,"\x1E","'");
	s=xrep(s,"\x1D","\x22");
	s=xrep(s,"\x1C",",");
	return s;
}

function xrep(s,f,n) {
	var tmp=s.split(f);
	return tmp.join(n);
}

function CBparseInt(n) {
	if(typeof(n)=="number"||typeof(n)=="string")
		return (!IsWin&&!SM?n:parseInt(n))*1;
	return 0;
}

function HideAll() {
	while(nOM>0)
		Hide();
}

function GetLeftTop() {
	if(IE)
		return [OP?0:SM?mFrame.scrollX:mFrame.document.body.scrollLeft,OP?0:SM?mFrame.scrollY:mFrame.document.body.scrollTop];
	if(NS)
		return [mFrame.pageXOffset,mFrame.pageYOffset];
}

function tHideAll() {
	IsOverHS=false;
	HTHnd[nOM]=window.setTimeout("if(!InMenu()&&nOM==1)HideAll();else HTHnd[nOM]=0;",HideSpeed);
}

function GetWidthHeight() {
	if(IE&&!SM)
		return [mFrame.document.body.clientWidth,mFrame.document.body.clientHeight];
	if(NS||SM)
		return [mFrame.innerWidth,mFrame.innerHeight];
}

function GetBorderWidth(b) {
	if(OP) return 0;
	var w;
	var l=b.split(" ");
	for(var i=0;i<l.length;i++) {
		w=CBparseInt(l[i]);
		if(w>0)
			return w;
	}
	return 0;
}

function GetCStyle(cmc) {
	return "position:absolute;left:"+cmc.left+    ";top:"+cmc.top+    ";width:"+(OP?cmc.pixelWidth:cmc.width)+    ";height:"+(OP?cmc.pixelHeight:cmc.height)+";";
}

function AutoPos(Menu,imgObj,arAlignment) {
	var x=GetImgXY(imgObj)[0];
	var y=GetImgXY(imgObj)[1];
	var mW=CBparseInt(NS?Menu.w:Menu.width);
	var mH=CBparseInt(NS?Menu.h:Menu.height);
	switch(arAlignment) {
		case 0:
			y += GetImgWH(imgObj)[1];
			break;
		case 1:
			x += GetImgWH(imgObj)[0]-mW;
			y += GetImgWH(imgObj)[1];
			break;
		case 2:
			y -= mH;
			break;
		case 3:
			x += GetImgWH(imgObj)[0]-mW;
			y -= mH;
			break;
		case 4:
			x -= mW;
			break;
		case 5:
			x -= mW;
			y -= mH-GetImgWH(imgObj)[1];
			break;
		case 6:
			x += GetImgWH(imgObj)[0];
			break;
		case 7:
			x += GetImgWH(imgObj)[0];
			y -= mH-GetImgWH(imgObj)[1];
			break;
	}
	return [x,y];
}

function GetImgXY(imgObj) {
	var x;
	var y;
	if(IE){
		x=getOffset(imgObj)[0];
		y=getOffset(imgObj)[1];
	}
	else{
		x=imgObj.x;
		y=imgObj.y;
	}
	return [x,y];
}

function GetImgWH(imgObj) {
	return [imgObj.width,imgObj.height];
}

function getOffset(imgObj) {
	x=imgObj.offsetLeft;
	y=imgObj.offsetTop;
	ce =imgObj.offsetParent;
	while (ce!=null){
		y += ce.offsetTop;
		x += ce.offsetLeft;
		ce=ce.offsetParent;
	}
	return [x,y];
}

function AutoHide() {
	var original_nOM=nOM;
	var OktoClose=true;
	for(;nOM>0;nOM--)
		if(InMenu()) {
			OktoClose=false;
			break;
		}
	nOM=original_nOM;
	if(OktoClose&&!IsOverHS)
		HideAll();
	TimerHideHnd=window.setTimeout("AutoHide()",TimerHideDelay);
}

function PlaySound() {
	if(IsWin&&!SM&&(IE&&BV>4||NS)) {
		var s=mFrame.document.embeds["Snd"+SelSndId];
		if(s)
			s.play();
	}
	SelSndId=0;
}

function ShowContextMenu(e) {
	if(cntxMenu!='') {
		if(IE) {
			SetPointerPos();
			IsContext=true;
			cFrame.ShowMenu(cntxMenu,mX-1,mY-1,false);
			return false;
		}
		if(NS)
			if(e.which==3) {
				IsContext=true;
				cFrame.ShowMenu(cntxMenu,e.x-1,e.y-1,false);
				return false;
			}
	}
	return true;
}

function SetUpEvents() {
	if(!SM)
		onerror=errHandler;
	if(typeof(mFrame)=="undefined")
		mFrame=eval(frames['self']);
	if(typeof(mFrame)=="undefined")
		window.setTimeout("SetUpEvents()",10);
	else {
		if(NS) {
			mFrame.captureEvents(Event.MOUSEMOVE);
			mFrame.onmousemove=HideMenus;
			mFrame.window.captureEvents(Event.MOUSEDOWN);
			mFrame.window.onmousedown=ShowContextMenu;
			PrepareEvents();
		}
		if(IE) {
			document.onmousemove=HideMenus;
			mFrame.document.onmousemove=document.onmousemove;
			if(!SM&&!OP&&IsWin)
				mFrame.document.oncontextmenu=ShowContextMenu;
			if(SM) {
				var i=0;
				var m;
				while(mFrame.document.getElementById(i)) {
					m=mFrame.document.getElementById(i++);
					m.style.width=CBparseInt(m.style.width)-CBparseInt(m.style.paddingLeft)*2;
					m.style.height=CBparseInt(m.style.height)-CBparseInt(m.style.paddingTop)*2;
				}
			}
		}
		MenusReady=true;
	}
}

function errHandler(sMsg,sUrl,sLine) {
	if(sMsg.substr(0,16)!="Access is denied"&&sMsg!="Permission denied")
		alert("Erro de Javascript\n"+      "\nDescrição:"+sMsg+      "\nCaminho:"+sUrl+      "\nLinha:"+sLine);
	return true;
}

function FixPointSize(s) {
	var ls=s.split("point-size=")[0]+"point-size=";
	var ps=s.split("point-size=")[1].substr(0,1);
	var rs=s.split("point-size=")[1].substr(1);
	ps=parseInt(ps)+3;
	return ls+ps+rs;
}

function PrepareEvents() {
	for(var l=0;l<mFrame.document.layers.length;l++) {
		var lo=mFrame.document.layers[l];
		if(lo.layers.length) {
			lo.w=lo.clip.width;
			lo.h=lo.clip.height;
			for(var sl=0;sl<lo.layers[0].layers.length;sl++) {
				var slo=mFrame.document.layers[l].layers[0].layers[sl];
				if(slo.name.indexOf("EH")>0) {
					slo.document.onmouseup=hNSCClick;
					slo.document.TCode=nTCode[slo.name.split("EH")[1]];
				}
				else
					if((slo.name.indexOf("MC")!=-1)&&!IsWin)
						if(nL[slo.name.substr(2)].split("|")[0]!="") {
							nL[slo.name.substr(2)]=FixPointSize(nL[slo.name.substr(2)]);
							hL[slo.name.substr(2)]=FixPointSize(hL[slo.name.substr(2)]);
						}
			}
		}
	}
}

SetUpEvents();
