//Written by Evan Kirkconnell.  ekirkco@uark.edu
//This menu is intended to be absolutely free and the source should always remain
//totally open.
//////////////////////////////////////////////////////////////////////////////////
//This function is called when the mouse is over a main menu item.  The main menu
//acts slightly different from the other menus.
//Variable 'it' holds the div the mouse just moved onto, 'x' holds the index of the
//main menu item the mouse is now over.
function menu_onMain(it,x){
    window.clearTimeout(menu_globals.timeoutID);
    menu_closeAll();
    if(typeof(menu_array[0].target[x]) != "undefined"){
        target=menu_array[0].target[x]; //set target to the (x+1)'th element of the main menu(target follows name in menu)
        window.status=target;
        if(!isNaN(target)){  //if target is a number then we pop the submenu for that number
            container=menu_divs[target][0];
            //alert("MainMenuTop: "+menu_globals.mainMenuTop+"MainMenuLeft: "+menu_globals.mainMenuLeft+"  top: "+it.style.top+"  left: "+it.style.left);
            if(menu_globals.alignment==0){
                container.style.top=menu_globals.mainMenuTop+parseInt(it.style.top)+menu_globals.mainMenuBorder+menu_globals.mainVerticalOffset+"px";
                container.style.left=menu_globals.mainMenuLeft+menu_array[0].width+2*menu_globals.mainMenuBorder+2*menu_globals.menuBorder+menu_globals.mainHorizontalOffset+"px";
            }else{
                container.style.top=menu_globals.mainMenuTop+parseInt(it.style.top)+parseInt(it.style.height)+menu_globals.mainMenuBorder+menu_globals.mainVerticalOffset+"px";
                container.style.left=menu_globals.mainMenuLeft+parseInt(it.style.left)+2*menu_globals.menuBorder+menu_globals.mainHorizontalOffset+"px";
                //alert("top: "+menu_globals.mainMenuTop+parseInt(it.style.top)+parseInt(it.style.height)+menu_globals.mainMenuBorder+menu_globals.mainVerticalOffset+"px"+"  left: "+menu_globals.mainMenuLeft+parseInt(it.style.left)+2*menu_globals.menuBorder+menu_globals.mainHorizontalOffset+"px");
            }
            container.style.visibility='visible';  //make submenu visible
//            alert("On Main: visibility set");
            if(menu_globals.hideSelects!=0){
                menu_selects=document.getElementsByTagName('SELECT');
                for(x=0;x<menu_selects.length;x++){
                    menu_selects[x].style.visibility='hidden';
                }
            }
        }
    }
    it.style.color=menu_globals.mainMenuTextOver;
    it.style.backgroundColor=menu_globals.mainMenuBGOver;
	it.style.cursor='pointer';
}
//////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////
//This function is called when the mouse moves off a main menu item.
//Variable 'it' holds the div the mouse just moved off of.
function menu_offMain(it){
    menu_startClose();
    it.style.color=menu_globals.mainMenuTextColor;
    it.style.backgroundColor=menu_globals.mainMenuBGColor;
}
//////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////
//this functions will close all windows after a delay
function menu_startClose(){
    window.clearTimeout(menu_globals.timeoutID);
    menu_globals.timeoutID=setTimeout("menu_closeAll()",menu_globals.mouseTimeout);
}
//////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////
//This function will make all submenu's disappear.
function menu_closeAll(){  //close all submenus
    for(x=1;x<menu_array.length;x++){
        menu_divs[x][0].style.visibility="hidden";
    }
    if(menu_globals.hideSelects!=0){  //make select pull-down re-appear
        for(x=0;x<menu_selects.length;x++){
            menu_selects[x].style.visibility='visible';
        }
    }    
}
//////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////
//This function is called when the mouse moves onto a submenu item.
//Variable 'it' holds the div e the mouse just moved onto, 'x' holds the index of 
//the menu it just moved onto, 'y' holds the index of the menu item it just moved
//onto.
function menu_onSub(it,x,y){
    window.clearTimeout(menu_globals.timeoutID);
    it.style.color=menu_globals.subMenuTextOver;
	it.style.borderWidth= 'thick';
	it.style.cursor='pointer';
    it.style.backgroundColor=menu_globals.subMenuBGOver;
    if(typeof(menu_array[x].target[y]) != "undefined"){
        target=menu_array[x].target[y];
        window.status=target;
        for(count=x+1;count<menu_array.length;count++){
        menu_divs[count][0].style.visibility='hidden';                }
        if(!isNaN(target)){  //if target is a number then we pop the next submenu for that number
            container=menu_divs[target][0]; //target is the targeted submenu , divPos[] returns it's div position, div returns the div object of the submenu container
            above=menu_divs[x][0]; //above is now the div object of the container of the submenu the mouse is over
            container.style.top=parseInt(above.style.top)+parseInt(it.style.top)+menu_globals.verticalOffset+"px";
            container.style.left=parseInt(above.style.left)+parseInt(above.style.width)+menu_globals.horizontalOffset+"px";
            container.style.visibility='visible';
        }
    }

}
//////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////
//This function is called when the mouse moves off of a submenu item.
//Variable, 'it' holds the div that the mouse just moved off.
function menu_offSub(it){
    it.style.color=menu_globals.subMenuTextColor;
    it.style.backgroundColor=menu_globals.subMenuBGColor;
    menu_startClose();
}
//////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////
//This function is called when a menu item is clicked.
//Variable, 'it' holds the div clicked, 'x' holds the index of the menu clicked,
//'y' holds the index of the menu item clicked.
function menu_clicked(it,x,y){
    if(typeof(menu_array[x].target[y]) != "undefined"){
        target=menu_array[x].target[y];
        if(isNaN(target)){
            if(target.charAt(0) == ':') window.open(target.substring(1));  //parent.location =
            else document.getElementById('mainframe').src=target;
     //   get("break_link").href=target;
     }
	 else
		{
		 document.getElementById('mainframe').src=cat_link[x][y];
		// alert('array:' + x +' element:'+y+' cat_link='+cat_link[x][y]);
		}
    }
}
//////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////
//This function adds the menu's html to the menu_all div
function menu_addDivs(first){
    if(first){ document.write("<div id='menu_all' class='menu_all'>"+menu_getHTML()+"</div>");
    }else{ document.getElementById("menu_all").innerHTML = menu_getHTML(); }
}
//////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////
//This function constructs the html for the divs of the menu
function menu_getHTML(){
    html = "<div id='menu_0' name='menu_0' class='menu_container'><div id='menu_0' name='menu_0' class='menu_container'>";
    for(x=0;x<menu_array[0].size;x++){  //iterate through main menus
        if(typeof(menu_array[0].style[x]) != "undefined" && typeof(menu_array[0].target[x]) != "undefined" && !isNaN(menu_array[0].target[x])){
            //alert("Style: "+menu_styles[menu_array[0].style[x]]);
            html+="<div id='menu_0' name='menu_0' class='menu_main' onMouseover=menu_onMain(this,"+x+") onMouseout=menu_offMain(this) onClick=menu_clicked(this,0,"+x+") style='"+menu_styles[menu_array[0].style[x]]+"'>"+menu_array[0].name[x]+"<img name='menu_img' class='valign' height='"+menu_globals.subPicHeight+"px' width='"+menu_globals.subPicWidth+"px' src='"+menu_globals.subPic+"'></div>";
        }else if(typeof(menu_array[0].style[x]) != "undefined" && (typeof(menu_array[0].target[x]) == "undefined" || isNaN(menu_array[0].target[x]))){
            //alert("Style: "+menu_styles[menu_array[0].style[x]]);
            html+="<div id='menu_0' name='menu_0' class='menu_main' onMouseover=menu_onMain(this,"+x+") onMouseout=menu_offMain(this) onClick=menu_clicked(this,0,"+x+") style='"+menu_styles[menu_array[0].style[x]]+"'>"+menu_array[0].name[x]+"</div>";
        }else if(typeof(menu_array[0].style[x]) == "undefined" && typeof(menu_array[0].target[x]) != "undefined" && !isNaN(menu_array[0].target[x])){
            html+="<div id='menu_0' name='menu_0' class='menu_main' onMouseover=menu_onMain(this,"+x+") onMouseout=menu_offMain(this) onClick=menu_clicked(this,0,"+x+")>"+menu_array[0].name[x]+"<img name='menu_img' class='valign' height='"+menu_globals.subPicHeight+"px' width='"+menu_globals.subPicWidth+"px' src='"+menu_globals.subPic+"'></div>";
        }else{
            html+="<div id='menu_0' name='menu_0' class='menu_main' onMouseover=menu_onMain(this,"+x+") onMouseout=menu_offMain(this) onClick=menu_clicked(this,0,"+x+")><b>"+menu_array[0].name[x]+"</b></div>";
        }
    }
    html+="</div></div>";
    for(x=1;x<menu_array.length;x++){  //iterate thru each submenus
        html+="<div id='menu_"+x+"' name='menu_"+x+"' class='menu_container'><div id='menu_"+x+"' name='menu_"+x+"' class='menu_container'>";
        for(y=0;y<menu_array[x].size;y++){  //iterate half the number of subMenu items
            if(typeof(menu_array[x].style[y]) != "undefined" && typeof(menu_array[x].target[y]) != "undefined" && !isNaN(menu_array[x].target[y])){
                html+="<div id='menu_"+x+"' name='menu_"+x+"' class='menu_sub' onMouseover=menu_onSub(this,"+x+","+y+") onMouseout=menu_offSub(this,"+x+") onClick=menu_clicked(this,"+x+","+y+") style='"+menu_styles[menu_array[x].style[y]]+"'><b>"+menu_array[x].name[y]+"</b><img name='menu_img' class='valign' height="+menu_globals.subPicHeight+"px width="+menu_globals.subPicWidth+"px src='"+menu_globals.subPic+"'></div>";
            }else if(typeof(menu_array[x].style[y]) != "undefined" && (typeof(menu_array[x].target[y]) == "undefined" || isNaN(menu_array[x].target[y]))){
                html+="<div id='menu_"+x+"' name='menu_"+x+"' class='menu_sub' onMouseover=menu_onSub(this,"+x+","+y+") onMouseout=menu_offSub(this,"+x+") onClick=menu_clicked(this,"+x+","+y+") style='"+menu_styles[menu_array[x].style[y]]+"'><b>"+menu_array[x].name[y]+"</b></div>";
            }else if(typeof(menu_array[x].style[y]) == "undefined" && typeof(menu_array[x].target[y]) != "undefined" && !isNaN(menu_array[x].target[y])){
                html+="<div id='menu_"+x+"' name='menu_"+x+"' class='menu_sub' onMouseover=menu_onSub(this,"+x+","+y+") onMouseout=menu_offSub(this,"+x+") onClick=menu_clicked(this,"+x+","+y+")><b>"+menu_array[x].name[y]+"</b><img name='menu_img' class='valign' height="+menu_globals.subPicHeight+"px width="+menu_globals.subPicWidth+"px src='"+menu_globals.subPic+"'></div>";
            }else{
                html+="<div id='menu_"+x+"' name='menu_"+x+"' class='menu_sub' onMouseover=menu_onSub(this,"+x+","+y+") onMouseout=menu_offSub(this,"+x+") onClick=menu_clicked(this,"+x+","+y+")><b>"+menu_array[x].name[y]+"</b></div>";
            }
        }
        html+="</div></div>";
    }
    return html;
}

//////////////////////////////////////////////////////////////////////////////////
//This function sets all menu div positions.
function menu_setPositions(){  //positions must be set by javascript or it can't tell where items are
                           //this function is run when the body of html is done loading
    menu_imgs = new Array();
    var width;
    for(x=0;x<menu_array.length;x++){
        menu_divs[x] = document.getElementsByName("menu_"+x);
    }
    if(menu_globals.alignment==0){ //if our main menu is a column
        //tmpHeight=menu_array[0].size*(menu_globals.mainMenuHeight+menu_globals.menuBorder)-menu_globals.menuBorder;
        menu_array[0].width=0;
        for(x=2;x<menu_array[0].size+2;x++){  //set menu width
            if(menu_divs[0][x].offsetWidth > menu_array[0].width){
                menu_array[0].width=menu_divs[0][x].offsetWidth;
            }
        }
        var top=menu_globals.menuBorder;
        for(x=2;x<menu_array[0].size+2;x++){  //set main menu appearance/position/size
            menu_divs[0][x].style.left=menu_globals.menuBorder+"px";
            menu_divs[0][x].style.top=top+"px";
            top=top+menu_divs[0][x].offsetHeight+menu_globals.menuBorder;
            menu_divs[0][x].style.height=menu_divs[0][x].offsetHeight+"px";
            if(typeof(menu_array[0].target[x-2]) != "undefined" && !isNaN(menu_array[0].target[x-2])){
                menu_imgs[menu_imgs.length] = menu_divs[0][x].offsetHeight;
            }
            menu_divs[0][x].style.width=menu_array[0].width+"px";
            menu_divs[0][x].style.color=menu_globals.mainMenuTextColor;
            menu_divs[0][x].style.backgroundColor=menu_globals.mainMenuBGColor;
            menu_divs[0][x].style.visibility='inherit';
        }
        menu_array[0].height=top-2*menu_globals.menuBorder;
    }else{  //if our main menu is a row
        menu_array[0].height=0;
        for(x=2;x<menu_array[0].size+2;x++){
            if(menu_divs[0][x].offsetHeight > menu_array[0].height){
                menu_array[0].height=menu_divs[0][x].offsetHeight+1;
            }
        }
        var left=menu_globals.menuBorder;
        //var leftArray = new Array();
        for(x=2;x<menu_array[0].size+2;x++){  //set main menu appearance/position/size
            menu_divs[0][x].style.left=left+"px";
            width = menu_divs[0][x].offsetWidth;
            menu_divs[0][x].style.width=width+"px";
            left=left+width+menu_globals.menuBorder;
            menu_divs[0][x].style.top=menu_globals.menuBorder+"px";
            menu_divs[0][x].style.height=menu_array[0].height+"px";
            if(typeof(menu_array[0].target[x-2]) != "undefined" && !isNaN(menu_array[0].target[x-2])){
                menu_imgs[menu_imgs.length] = menu_array[0].height;
            }
            menu_divs[0][x].style.color=menu_globals.mainMenuTextColor;
            menu_divs[0][x].style.backgroundColor=menu_globals.mainMenuBGColor;
            menu_divs[0][x].style.visibility='inherit';
        }
        menu_array[0].width=left-2*menu_globals.menuBorder;
    }
    //set up main menu container
    menu_divs[0][0].style.top=menu_globals.mainMenuTop+"px";
    menu_divs[0][0].style.left=menu_globals.mainMenuLeft+"px";
    menu_divs[0][0].style.width=menu_array[0].width+2*menu_globals.mainMenuBorder+2*menu_globals.menuBorder+"px";
    menu_divs[0][0].style.height=menu_array[0].height+2*menu_globals.mainMenuBorder+2*menu_globals.menuBorder+"px";
    menu_divs[0][0].style.backgroundColor=menu_globals.mainMenuBorderColor;
    menu_divs[0][0].style.visibility='visible';
    menu_divs[0][1].style.top=menu_globals.mainMenuBorder+"px";
    menu_divs[0][1].style.left=menu_globals.mainMenuBorder+"px";
    menu_divs[0][1].style.width=menu_array[0].width+2*menu_globals.menuBorder+"px";
    menu_divs[0][1].style.height=menu_array[0].height+2*menu_globals.menuBorder+"px";
    menu_divs[0][1].style.backgroundColor=menu_globals.menuBorderColor;
    menu_divs[0][1].style.visibility='inherit';
    for(x=1;x<menu_array.length;x++){//setup submenu positions and sizes
        var top=0;
        for(y=2;y<menu_array[x].size+2;y++){   //make submenu as wide as its widest item
            //menu_array[x].width=0;
            if((menu_divs[x][y].offsetWidth)>=menu_array[x].width){
                menu_array[x].width=menu_divs[x][y].offsetWidth; //+add;
            }            
        }
        var top=menu_globals.menuBorder;
        for(y=2;y<menu_array[x].size+2;y++){ //position and size each element within the container
            menu_divs[x][y].style.left=menu_globals.menuBorder+"px";
            menu_divs[x][y].style.top=top+"px";
            top=top+menu_divs[x][y].offsetHeight+menu_globals.menuBorder;
            menu_divs[x][y].style.height=menu_divs[x][y].offsetHeight+"px";
            menu_divs[x][y].style.width=menu_array[x].width+"px";
            if(typeof(menu_array[x].target[y-2]) != "undefined" && !isNaN(menu_array[x].target[y-2])){
                menu_imgs[menu_imgs.length] = menu_divs[x][y].offsetHeight;
            }
            menu_divs[x][y].style.color=menu_globals.subMenuTextColor;
            menu_divs[x][y].style.backgroundColor=menu_globals.subMenuBGColor;
            //menu_divs[x][y].style.visibility='inherit';
        }
        menu_array[x].height=top-2*menu_globals.menuBorder;
        //menu_divs[first] is the outer container of the submenu, menu_divs[first+1] is the inner containter div
        //note: there are 2 container divs to manually create a border due to differences in IE and NS.
        menu_divs[x][0].style.height=menu_array[x].height+2*menu_globals.subMenuBorder+2*menu_globals.menuBorder+"px";
        menu_divs[x][0].style.width=menu_array[x].width+2*menu_globals.subMenuBorder+2*menu_globals.menuBorder+"px";
        menu_divs[x][0].style.backgroundColor=menu_globals.subMenuBorderColor;
        menu_divs[x][0].style.visibility="hidden";
        menu_divs[x][1].style.top=menu_globals.subMenuBorder+"px";
        menu_divs[x][1].style.left=menu_globals.subMenuBorder+"px";
        menu_divs[x][1].style.height=menu_array[x].height+2*menu_globals.menuBorder+"px";
        menu_divs[x][1].style.width=menu_array[x].width+2*menu_globals.menuBorder+"px";
        menu_divs[x][1].style.backgroundColor=menu_globals.menuBorderColor;
        menu_divs[x][1].style.visibility='inherit';
    }
    var menu_images = document.getElementsByName("menu_img");
    for(x=0;x<menu_images.length;x++){
        menu_images[x].style.position = "absolute";
        menu_images[x].style.right = "0px";
        menu_images[x].style.top = (menu_imgs[x]/2-menu_images[x].offsetWidth/2)+"px";
    }
    //document.getElementById("menu_all").style.visibility = "visible";
}
//////////////////////////////////////////////////////////////////////////////////
