
// sets width of items in menu for Netscape
var menuWidthNN = 200;

//  sets height of items in menu for Netscape
var menuHeightNN = 19;

if (document.images) 
{ 
    var dormantImage = new Array();
    dormantImage[0] = new Image();
    dormantImage[0].src = "/images/dept.gif";
    dormantImage[1] = new Image();
    dormantImage[1].src = "/images/academics.gif";
    dormantImage[2] = new Image();
    dormantImage[2].src = "/images/people.gif";
    dormantImage[3] = new Image();
    dormantImage[3].src = "/images/research.gif";
    dormantImage[4] = new Image();
    dormantImage[4].src = "/images/join.gif";
    dormantImage[5] = new Image();
    dormantImage[5].src = "/images/contact.gif";
    dormantImage[6] = new Image();
    dormantImage[6].src = "/images/community.gif";

    //  -> The activeImage array contains the images for
    //     the menu buttons' active appearance.
    var activeImage = new Array();
    activeImage[0] = new Image();
    activeImage[0].src = "/images/dept_on.gif";
    activeImage[1] = new Image();
    activeImage[1].src = "/images/academics_on.gif";
    activeImage[2] = new Image();
    activeImage[2].src = "/images/people_on.gif";
    activeImage[3] = new Image();
    activeImage[3].src = "/images/research_on.gif";
    activeImage[4] = new Image();
    activeImage[4].src = "/images/join_on.gif";
    activeImage[5] = new Image();
    activeImage[5].src = "/images/contact_on.gif";
    activeImage[6] = new Image();
    activeImage[6].src = "/images/community_on.gif";
}

//define number and length of menus in an array
var menuLengths = new Array(8,5,6,10,5,5,4);

// creates empty array
var menuLinks = new Array(menuLengths.length);

for (i=0;i<menuLengths.length;i++) 
    menuLinks[i] = new Array(menuLengths[i]);

/*this section lists the menu links in a 2D array for IE and Opera; since Netscape is not DOM-compliant, the links are repeated lower down in the page for Navigator*/

//links for "The Department" menu
menuLinks[0][0] = "/about/history.php";
menuLinks[0][1] = "/about/future.php";
menuLinks[0][2] = "/about/administration.php";
menuLinks[0][3] = "/about/resources.php";
menuLinks[0][4] = "/about/pol_pro.php";
menuLinks[0][5] = "http://www.panic.unc.edu/";
menuLinks[0][6] = "/about/guest_night.php";
menuLinks[0][7] = "/about/news_events.php";

//links for the "Academics" menu
menuLinks[1][0] = "/academics/grad.php";
menuLinks[1][1] = "/academics/ugrad.php";
menuLinks[1][2] = "/academics/gsa.php";
menuLinks[1][3] = "/ugrad/sps/";
menuLinks[1][4] = "/classes/courses.php";

//links for the "People" menu
menuLinks[2][0] = "/directory/directory.php?section=1&mode=text&param=99";
menuLinks[2][1] = "/directory/directory.php?section=2&mode=text&param=99";
menuLinks[2][2] = "/directory/directory.php?section=3&mode=text&param=99";
menuLinks[2][3] = "/directory/directory.php?section=5&mode=text&param=99";
menuLinks[2][4] = "/directory/directory.php?section=11&mode=text&param=99";
menuLinks[2][5] = "/directory/directory.php?section=10&mode=text&param=99";

//links for the "Research" menu
menuLinks[3][0] = "/research/astro/";
menuLinks[3][1] = "/research/biophysics/index.php";
menuLinks[3][2] = "/research/nano/";
menuLinks[3][3] = "/research/theory/gravity.php";
menuLinks[3][4] = "/research/theory/energy.php";
menuLinks[3][5] = "/research/nuclear/"; 
menuLinks[3][6] = "/research/theory/string.php";
menuLinks[3][7] = "/research/theory/";
menuLinks[3][8] = "/research/ugrad_research.php";
menuLinks[3][9] = "/about/grants";

//links for the "Join Us" menu
menuLinks[4][0] = "/academics/grad.php";
menuLinks[4][1] = "/academics/ugrad.php";
menuLinks[4][2] = "/join/faculty_pos.php";
menuLinks[4][3] = "/join/staff_pos.php";
menuLinks[4][4] = "/join/postdoc_pos.php";

//links for the "Contact" menu
menuLinks[5][0] = "/academics/grad.php";
menuLinks[5][1] = "/academics/ugrad.php";
menuLinks[5][2] = "/contact/location.php";
menuLinks[5][3] = "/contact/contact.php";
menuLinks[5][4] = "/directory/directory.php?section=1&mode=text&param=99";

//links for the "UNC Community" menu
menuLinks[6][0] = "http://www.unc.edu/";
menuLinks[6][1] = "http://artsandsci.unc.edu/";
menuLinks[6][2] = "http://www.chapelhill.com/";
menuLinks[6][3] = "http://www.wral.com/";

//this is the timer that makes the current menu disappear if the mouse is off of it for a certain amount of time
var menuTimers = new Array(menuLengths.length);

/*
This function is used throughout the script to obtain
a reference to a page element's style given its ID.
This reference can then be used to set style properties
like position and visibility.
*/

function getStyleObj(id) {
	if (document.getElementById || // DOM-compliant browsers (MSIE5, NSN6, O5)
	document.all) {            // or MSIE 4
		return getObj(id).style;
	} else return getObj(id); // NSN4
}

/**
 * This function is used throughout the script to obtain
 * a reference to a page element given its ID. This
 * reference can then be used to set variables, event
 * handlers, etc.
 */
function getObj(id) {
    if (document.getElementById) { // DOM-compliant browsers (MSIE5, NSN6, O5)
        return document.getElementById(id);
    } else if (document.all) { // MSIE4
        return document.all[id];
    } else if (document.layers) { // NSN4
        return document.layers[id];
    } else { // Trap DHTML-impaired browsers 
        //alert("Your browser does not support DHTML!");
        return false;
    }
}

/**
 * This function displays a menu given its 1-based index.
 * For example, showMenu(6) will display the menu that is
 * defined by <DIV ID="menu6"> in the document.
 */
function showMenu(index) {
    // Make sure this menu is not marked to be closed. This
    // cancels any previous request to close this menu.
    if (menuTimers[index-1]) clearTimeout(menuTimers[index-1]);

    // Immediately close any other menus so as to avoid
    // having more than one menu open at once (which is
    // quite ugly).
    for (i=1;i<=menuLengths.length;i++)
        if (i!=index) closeMenu(i);

    // Get a reference to the style of the menu to be displayed.
    var menu = getStyleObj("menu"+index);

    // Make sure we got a valid reference, and make the menu visible.
    if (menu) menu.visibility = "visible";

    // Display the 'active' version of the menu button.
    if (document.images) document.images["menubutton"+index].src =
                            activeImage[index-1].src;
}

//closes menu after brief period of time
function hideMenu(index) {
    // resets timer 
    if (menuTimers[index-1]) clearTimeout(menuTimers[index-1]);

    // closes menu
    menuTimers[index-1] = setTimeout("closeMenu('"+index+"');",200);
}

/**
 * This function closes a popup menu immediately if it
 * has been marked to be closed in the menuClosingFlags array.
 */
function closeMenu(index) {
    // Get a reference to the style of the menu to be closed.
    var menu = getStyleObj("menu"+index);

    // Make sure we got a valid reference, and hide the menu.
    if (menu) menu.visibility = "hidden";

    // Display the 'inactive' version of the menu button.
    if (document.images) document.images["menubutton"+index].src =
                            dormantImage[index-1].src;
}

/**
 * This function is called every time the user's mouse
 * is over a menu item, hilighting that menu item and
 * holding the menu that contains it open.
 */
function overMenuItem() {
    // makes highlighted version of menu item highlighted (since they are hidden normally)
    this.hilightitem.visibility='visible';

    //makes menu stay open when mouse is over it
    showMenu(this.menuid);
}

//makes high lighted menu item return to original dormant state
function outMenuItem() 
{
    // hides highlighted menu item when not active
    this.hilightitem.visibility='hidden';

    // hides menu
    hideMenu(this.menuid);
}

/**
 * This function is called when the user clicks on a
 * menu item. The URL associated with that menu item is
 * then loaded.
 */
function clickMenuItem() {
    if (this.url) { // There is a URL associated with this item
        window.location = this.url;
    }
}

//sets dimensions, etc. of menus in different browsers

window.onload=setupMenus; // Assign to onLoad event handler
function setupMenus() {
    // this if statement deals with Netscape stuff only
    if (document.layers) { 
        for (menuid=1;menuid<=menuLengths.length;menuid++) 
	{ 
            // Get the menu object
            var menu = getObj("menu"+menuid);

            // fills menu background with specified color
            menu.clip.width = menuWidthNN + 2;
            menu.clip.height = (menuHeightNN+1) * menuLengths[menuid-1] + 1;

            // background color
            menu.document.bgColor="#000000";

	    //this sets the background color for each menu item
            for (i=1;i<=menuLengths[menuid-1];i++) 
	    { 
                var item = eval("menu.document.menu"+menuid+"item"+i);
                var onitem = eval("menu.document.menu"+menuid+"item"+i+"on");

                // this leaves a 1px spacer in Netscape for the menus
                item.top = (menuHeightNN + 1) * (i-1) + 1;
                onitem.top = (menuHeightNN + 1) * (i-1) + 1;

                // setting dimensions and colors of the menu items
                item.clip.width = menuWidthNN;
                item.clip.height = menuHeightNN;
                item.document.bgColor="#3366CC";
                onitem.clip.width = menuWidthNN;
                onitem.clip.height = menuHeightNN;
                onitem.document.bgColor="#0099FF";

                // shows highlighted version of selected menu item when mouse goes over it
                item.hilightitem = onitem;
                onitem.hilightitem = onitem;

                /* figures out what item is being triggered for the onverMenuItem() and outMenuItem() functions*/
                item.menuid = menuid;
                onitem.menuid = menuid;

                /* gets appropiate URL for each menu item, which is then used by clickMenuItem() function. Remember that 2D
		   array? This is where it comes in.*/
                var url = menuLinks[menuid-1][i-1];
                item.url = url;
                onitem.url = url;

                /* this basically makes it so the menu images stay lit even though you are not directly mousing over them,
		   but their respective menus*/
                var eTypes = Event.MOUSEOVER | Event.MOUSEOUT | Event.CLICK;
                item.captureEvents(eTypes);
                onitem.captureEvents(eTypes);
                item.onmouseover = overMenuItem;
                onitem.onmouseover = overMenuItem;
                item.onmouseout = outMenuItem;
                onitem.onmouseout = outMenuItem;
                item.onclick = clickMenuItem;
                onitem.onclick = clickMenuItem;
            }
        }
    } else { // This section is for all other browsers
        for (menuid=1;menuid<=menuLengths.length;menuid++) // For each menu
	{ 
		for (i=1;i<=menuLengths[menuid-1];i++) // For each menu item
		{ 
                	//this gets menu info
                	var item = getObj("menu"+menuid+"item"+i);
                	var onitem = getObj("menu"+menuid+"item"+i+"on");
                	var itemstyle = getStyleObj("menu"+menuid+"item"+i);
                	var onitemstyle = getStyleObj("menu"+menuid+"item"+i+"on");

                	//set vertical position
	                itemstyle.top = (menuHeightNN + 1) * (i-1) + 1;
        	        onitemstyle.top = (menuHeightNN + 1) * (i-1) + 1;
                
                
                	//this controls what should be high-lighted and what should not 
                	item.hilightitem = onitemstyle;
                	onitem.hilightitem = onitemstyle;

                	//keeps the right menu open when user is using it
                	item.menuid = menuid;
                	onitem.menuid = menuid;

                	/* gets appropiate URL for each menu item, which is then used by clickMenuItem() function. Remember 			   that 2D array? This is where it comes in.*/
                	var url = menuLinks[menuid-1][i-1];
                	item.url = url;
                	onitem.url = url;

                	/* this basically makes it so the menu images stay lit even though you are not directly mousing over 			   them, but their respective menus*/
                	item.onmouseover = overMenuItem;
                	onitem.onmouseover = overMenuItem;
                	item.onmouseout = outMenuItem;
                	onitem.onmouseout = outMenuItem;
                	item.onclick = clickMenuItem;
                	onitem.onclick = clickMenuItem;
            	}
        }
    }
}

/* since Netscape sucks, this function fixes the resize bug in that browser*/
function handleResize() 
{
	location.reload();
	return false;
}
if (document.layers) 
{
	window.onresize = handleResize;
}
