// JavaScript Document
var obj;
var delayhide;
var tmpParent;
var tableMenu;
var srcParent;
var menuAktif = null;
var prevParent = null;

var namaSubMenu = "mtn_1_01";
var lebarSubMenu = "200";
var disappeardelay = 250;
var ada = false;
var counter = 0;
var	ie4=document.all;
var	ns6=document.getElementById&&!document.all;
var tableOpened = false;

var menu = "<div id=\""+namaSubMenu+"\" name=\""+namaSubMenu+"\" style=\"border: solid 1px black; display: \'none\'; position: \'absolute\';\"  "+
           " onmouseover=\"javascript:clearhidemenu();\" onmouseout=\"dynamichide(event);\">"+
           "</div>";

var menuUtama = "<table cellpadding=2 cellspacing=0 border=0 id=\"menuTbl\" name=\"menuTbl\" "+
                "width=150></table>";

function openTable(){
    document.write(menu);
    document.write(menuUtama);
    tableMenu = document.getElementById("menuTbl");
    return;
}

function logOver(src,e){
    var curObject = (window.event)?e.srcElement:e.target;
    var parent = getParent(curObject);
    //obj = null;
    curObject.style.backgroundColor = "ivory";
    if (curObject.previousSibling)
        curObject.previousSibling.style.backgroundColor = "ivory";
    else if (curObject.nextSibling)
        curObject.nextSibling.style.backgroundColor = "ivory";

	if (obj != null){
		obj.style.display="none";
		clearhidemenu();
	}
		
    if (prevParent != getParent(curObject) && prevParent != null){
        menuAktif.style.backgroundColor = "lightgrey";
        
        if (menuAktif.previousSibling)
            menuAktif.previousSibling.style.backgroundColor = "lightgrey";
        else if (menuAktif.nextSibling)
            menuAktif.nextSibling.style.backgroundColor = "lightgrey";
    }

    menuAktif = curObject;
    prevParent = parent;
    if (ie4) {showSelect();}
    return;
}


function subMenuLogOut(link){
	if (confirm('Anda ingin Logout?'))
		location = link;
}

function mainMenu(link){
    document.getElementById("frmIsi").src = link;
}


function addMenu(id, namaMenu, link){
    
    if (!tableOpened){
        openTable();
    }
    
    tableOpened = true;
    
    /*-- BARIS --*/
    var row = tableMenu.insertRow(tableMenu.rows.length);
    tableMenu.style.border = "1px black solid";

	row.onmouseout = new Function("delayhidemenu();");

	if (link!=null){
        if (ie4){
			row.onmouseover = new Function("logOver('"+id+"',event);");//row.style.backgroundColor = "ivory";
			if  (namaMenu.indexOf("Log Out") != -1)
                row.onclick = new Function("subMenuLogOut('"+link+"');");
            else
                row.onclick = new Function("mainMenu('"+link+"');");
        }else{
			row.setAttribute("onmouseover","logOver('"+id+"',event);");
            if  (namaMenu.indexOf("Log Out") != -1)
			    row.setAttribute("onclick","subMenuLogOut('"+link+"');");
            else
                row.setAttribute("onclick","mainMenu('"+link+"');");  
		}
	}
    
    row.id = id;
    row.name = id;
    if (ie4){
        row.style.cursor = "pointer";
        row.style.backgroundColor = "lightgrey";
     }else{
        row.setAttribute("class","menu_row");
    }

    /*-- KOLOM --*/
    //---> Nama menu
    var cell = row.insertCell(0);
	cell.innerHTML = namaMenu;
	//cell.innerHTML = (link==null) ? namaMenu : "<a href="+link+" id=logOut>"+namaMenu+"</a>";
	cell.align = "left";
    cell.width = "100";
    cell.style.fontWeight="bold";
    cell.style.color="black";
    cell.style.backgroundColor = "lightgrey";
    
	if (ie4){
        //cell.style.borderBottom = "black 2px dotted";
	    cell.style.borderLeft= "1px white solid";
	    cell.style.borderTop= "1px white solid";
     }else{
        cell.setAttribute("class","menu_cell");
    }

	    //---> Letakkan gambar panah untuk menu
	    cell = row.insertCell(1);
	    cell.width = 10;
	    cell.align = "right";

	if (link==null){
	    cell.style.background = "url(\/images\/arrow.gif)";
        cell.style.backgroundRepeat = "no-repeat";
	}

	 	cell.innerHTML = "&nbsp;&nbsp;";
	    //cell.innerHTML = "<img src=\/images\/arrow.gif align=absmiddle >";
		if (ie4){
	        //cell.style.borderBottom = "black 2px dotted";
		    cell.style.borderRight= "1px white solid";
		    cell.style.borderTop= "1px white solid";
	     }else{
	        cell.setAttribute("class","menu_cell_1");
	    }
	return;       
}

function addSubMenu(parentMenu, arrayName, subMenuName, page){
    var nextPage;
    var srcParent = document.getElementById(parentMenu);
    if (tmpParent != parentMenu){
        tmpParent = parentMenu;
        counter = 0;
        if (ie4)
            srcParent.onmouseover = new Function("showSubMenu('"+arrayName+"',event);");
        else
            srcParent.setAttribute("onmouseover","showSubMenu('"+arrayName+"',event);");
    }

    if (counter==0){
        document.write("<input type=hidden id=\""+arrayName+"\">");
    }
    
    var arraySubMenu = document.getElementById(arrayName);

    if (page != null)
        nextPage = page;
    else
        nextPage = "#";
        
    if (counter==0){
        if (nextPage != "#"){
            arraySubMenu.value = "<table cellpadding=0 cellspacing=0 border=0 ><tr><td style='width: 15px; background-color: gold;'>&nbsp;</td><td width=100% height=20px bgcolor=ivory><p><a href='"+nextPage+"'  target=frmIsi onclick=\"hidemenu();\">"+subMenuName+"<\/a><\/p><\/td><\/tr><\/table>";
        }else{ 
			if (nextPage == "#" && subMenuName == "-")
				arraySubMenu.value = "<image src=\"\/images\/lineshort.gif\" align=center style=\"border: 0; height: 1px; width: 100%; \">";//"<hr id=\"solid\">";
			else
            	arraySubMenu.value = "<table cellpadding=0 cellspacing=0 border=0 ><tr><td style='width: 15px; background-color: gold;'>&nbsp;</td><td width=100% height=20px bgcolor=ivory><a>"+subMenuName+"<\/a><\/td><\/tr><\/table>";
        }
    }else
        if (nextPage != "#"){
            arraySubMenu.value += "|<table cellpadding=0 cellspacing=0 border=0 ><tr><td style='width: 15px; background-color: gold;'>&nbsp;</td><td width=100% height=20px bgcolor=ivory><p><a href='"+nextPage+"' target=frmIsi onclick=\"hidemenu();\">"+subMenuName+" <\/a><\/p><\/td><\/tr><\/table>";
        }else{
			if (nextPage == "#" && subMenuName == "-")
				arraySubMenu.value += "|<image src=\"\/images\/lineshort.gif\" align=center style=\"border: 0; height: 1px; width: 100%; \">";//<hr id=\"solid\">";
			else
            	arraySubMenu.value += "|<table cellpadding=0 cellspacing=0 border=0 ><tr><td style='width: 15px; background-color: gold;'>&nbsp;</td><td width=100% bgcolor=ivory><a>"+subMenuName+"<\/a><\/td><\/tr><\/table>";
       }
    counter++;
    return;
}
/*
function openNewPage(page){
    document.getElementById("frmIsi").src=page;
    hidemenu();
}*/

function showSubMenu(data, e)
{
    var targetArray = document.getElementById(data).value;
    var subMenu = new Array();
    subMenu = targetArray.split("|");
    
    var curObject = (window.event)?e.srcElement:e.target;
    var parent = getParent(curObject);
    curObject.style.backgroundColor = "ivory";

    //---> Digunakan untuk membuat warna tulisan menjadi berwarna merah jika mouse berada 
    //---> di atas gambar anak panah atau nama menu. Kemudian membuat warna tulisan yang sebelumnya dikunjungi
    //---> menjadi normal kembali
    if (curObject.previousSibling)
        curObject.previousSibling.style.backgroundColor = "ivory";
    else if (curObject.nextSibling)
        curObject.nextSibling.style.backgroundColor = "ivory";

    if (prevParent != getParent(curObject) && prevParent != null){
        menuAktif.style.backgroundColor = "lightgrey";
        
        if (menuAktif.previousSibling)
            menuAktif.previousSibling.style.backgroundColor = "lightgrey";
        else if (menuAktif.nextSibling)
            menuAktif.nextSibling.style.backgroundColor = "lightgrey";
    }
    
    obj = document.getElementById(namaSubMenu);
    obj.style.left = getLeft(curObject)+tableMenu.offsetWidth;
    obj.style.top = getTop(curObject);
    obj.style.display='';
    obj.style.width = lebarSubMenu;
    obj.style.backgroundColor = "lightgrey";
    //obj.style.cursor = "pointer";

    obj.align = "justify";
    obj.innerHTML = subMenu.join("");
    clearhidemenu();
    menuAktif = curObject;
    prevParent = parent;
    
    if (ie4) {hideSelect(event)};
}

//---> Sembunyikan semua element HTML yang berada di bawah submenu. Cara ini dilakukan
//---> dengan membandingkan posisi element HTML dari sebelah kiri dan atas layar dan posisi
//---> submenu sebelah kiri dan atas. 
function hideSelect(e)
{
    var posLeft;
    var posTop;
    
    var frm = document.getElementById("frmIsi"); //---> Frame

	if (parent.frames[0].window.document.getElementById("searchMenu") != null){
		var curObj;
	    var frmObj = parent.frames[0].window.document.getElementById("searchMenu").all;

	    var subMenuLeft = parseInt(obj.style.left);
	    var subMenuRight = (parseInt(obj.style.left)+parseInt(obj.offsetWidth));
	    var subMenuTop = parseInt(obj.style.top);
	    var subMenuBottom = (e.screenY);//+parseInt(obj.offsetHeight));
	
	    for (x=0; x<frmObj.length; x++)
	    {
			curObj = frmObj[x];

			if (curObj.tagName == "SELECT")
	        {
	            /*
				posTop = getTop(frm)+getFrameTop(curObj);
	            posLeft = parseInt(tableMenu.offsetWidth)+getLeft(frm)+getLeft(curObj);
	            
				if (( posTop >= subMenuTop && posTop <= subMenuBottom)
	                || ( posLeft >= subMenuLeft && posLeft <= subMenuRight))
	            {
	                curObj.style.visibility = 'hidden';
	            } */
	            curObj.style.visibility = 'hidden';
	        }
	    }
	}
}

//---> Tampilkan kembali element HTML yang disembunyikan
function showSelect()
{
 	if (parent.frames[0].window.document.getElementById("searchMenu")  != null){
	   	var frmObj = parent.frames[0].window.document.getElementById("searchMenu").all;
	
	    for (x=0; x<frmObj.length; x++)
	    {
	        if (frmObj[x].tagName == "SELECT" && frmObj[x].style.visibility == "hidden")
	        {
	            frmObj[x].style.visibility = 'visible';
	        }
	    }
	}
}

function delayhidemenu(){
    delayhide=setTimeout("hidemenu()",disappeardelay);
}

//---> Batalkan timeout untuk sembunyikan waktu
function clearhidemenu(){
		clearTimeout(delayhide);
}

//---> Set mulai waktu time out untuk sembunyikan menu
function dynamichide(e)
{
	if (ie4&&!obj.contains(e.toElement))
		delayhidemenu();
	else if	(ns6&&e.currentTarget!=	e.relatedTarget&&!contains_ns6(e.currentTarget, e.relatedTarget))
		delayhidemenu();
}

function contains_ns6(a, b)	
{
	while (b.parentNode)
	{
      if ((b = b.parentNode) == a)
			    return true;
	}
	return false;
}

//---> Sembunyikan menu
function hidemenu(e)
{
    //document.getElementById('debug').value=document.getElementById('subMenu4').value;
	menuAktif.style.backgroundColor = "lightgrey";

    if (menuAktif.previousSibling)
        menuAktif.previousSibling.style.backgroundColor = "lightgrey";
    else if (menuAktif.nextSibling)
        menuAktif.nextSibling.style.backgroundColor = "lightgrey";

    obj.style.display="none";
    if (ie4) {showSelect();}
}


//---> Ambil letak object dari sebelah kiri layar
function getLeft(src)
{
    var objParent = src;
    var objLeft = 0;
    objParent = getParent(objParent);
    
    while (objParent.tagName != "BODY")
    {
        if (!ie4){
			if (objParent.tagName != "TD" && objParent.tagName != "TBODY" )
	            objLeft += objParent.offsetLeft;
        }else
            objLeft += objParent.offsetLeft;
        objParent = getParent(objParent);
    }
    return objLeft;
}

//---> Ambil letak object dari atas layar
function getTop(src)
{
    var objParent = src;
    var objTop = 0;
    objParent = getParent(objParent);

    while (objParent.tagName != "BODY")
    {
        if (objParent.tagName != "TD" && objParent.tagName != "TBODY" )
            objTop += objParent.offsetTop;
        
        objParent = getParent(objParent);
    }
    return objTop;
}

function getFrameTop(src)
{
    var objParent = src;
    var objTop = 0;
    objParent = getParent(objParent);

    while (objParent.tagName != "BODY")
    {
        if (objParent.tagName != "TBODY" )
            objTop += objParent.offsetTop;
        
        objParent = getParent(objParent);
    }
    return objTop;
}

function getParent(src){
    return (navigator.appName=="Netscape")?src.parentNode:src.parentElement;
}

function cekTarget(evt){
    return (navigator.appName=="Netscape")?evt.relatedTarget:evt.toElement;
}
