/*****************************************
 * Remember, stealing code is for wimps. *
 *****************************************/

var mouseOverTimer;
var menuBar;
var menus;


function initializeMenus() {
    menuBar = new Array(7);

    // The following section builds the menu system.  
    // A MenuItem specifies a menu item with a hyperlink, a Menu specifies a menu item that is the root of a submenu.
    // The Menu takes as a second argument, an array of the MenuItems that will be in the submenu.
    // Maximum nesting is two deep (menu and submenu only.)

    // Menu Heading						// Menu Text			// Hyperlink
    
    // About SoM Menu
    menuBar[0] = new Array(2);
    	menuBar[0][0] = new MenuItem( "Director's Welcome",     "./index.php");
    	menuBar[0][1] = new MenuItem( "NWC Building Info",      "./facilities.php");
    	menuBar[0][2] = new MenuItem( "Computing Resources",    "./computing.php");

    // People Menu
    menuBar[1] = new Array(3);
    	menuBar[1][0] = new MenuItem( "Faculty/Staff",			"./faculty.php");
    	menuBar[1][1] = new MenuItem( "Students",				"./studentsGrads.php");
    	menuBar[1][2] = new MenuItem( "Research Topics",   		"./research.php");
    	
        //menuBar[1][0] = new MenuItem( "Regular Faculty",       "./faculty.php");
        //menuBar[1][1] = new MenuItem( "Research, Affiliated, and Adjunct Faculty",       "./facultyResearch.php");
        //menuBar[1][2] = new MenuItem( "Emeritus Faculty",       "./facultyEmeritus.php");
        //menuBar[1][3] = new MenuItem( "Staff",       			"./facultyStaff.php");
        //menuBar[1][4] = new MenuItem( "Research Topics",   		"./research.php");
        //menuBar[1][5] = new MenuItem( "Undergrads",             "./studentsUnderGrads.php");
		//menuBar[1][6] = new MenuItem( "Grad Students",          "./studentsGrads.php");
		//menuBar[1][7] = new MenuItem( "Alumni", 				"./studentsAlumni.php");

    // Organizations Menu
    menuBar[3] = new Array(3);
        // Student organizations submenu:
	var stuorgs = new Array(3);
	    stuorgs[0] = new MenuItem( "OWL", 						"http://owl.ou.edu");
	    stuorgs[1] = new MenuItem( "SAC", 						"../%7Esac");
		stuorgs[2] = new MenuItem( "SCAMS", 					"../%7Eouscams/");
		

	// Research organizations submenu:
	var researchorgs = new Array(4);
		researchorgs[0] = new MenuItem( "ARRC",					"http://arrc.ou.edu");
		researchorgs[1] = new MenuItem( "CAPS",					"http://caps.ou.edu");
		researchorgs[2] = new MenuItem( "CASA",					"http://casa.ou.edu");
		researchorgs[3] = new MenuItem( "CIMMS",				"http://cimms.ou.edu");
		

	// Government organizations submenu:
	var govorgs = new Array(3);
		govorgs[0] = new MenuItem( "NSSL", 						"http://www.nssl.noaa.gov");
		govorgs[1] = new MenuItem( "NWS",						"http://www.nws.noaa.gov");
		govorgs[2] = new MenuItem( "SPC",						"http://www.spc.noaa.gov");
	
	menuBar[3][0] = new Menu( "Student:", stuorgs);
        menuBar[3][1] = new Menu( "Research:", researchorgs);
        menuBar[3][2] = new Menu( "Government:", govorgs);

    // Academics Menu
    menuBar[4] = new Array(7);
        // Information Submenu
        //var info = new Array(3);
        //    info[0] = new MenuItem("Undergrads",            	"./studiesUnderGrad.php");
        //    info[1] = new MenuItem("Grads",                  	"./studiesGrad.php");
        //   info[2] = new MenuItem("Prospective Students",   	"./studiesProspective.php");

        //menuBar[4][0] = new MenuItem("Information for:");
        menuBar[4][0] = new MenuItem("Undergrad Students",         	"./studiesUnderGrad.php");
        menuBar[4][1] = new MenuItem("Graduate Students",          	"./studiesGrad.php");
        menuBar[4][2] = new MenuItem("Prospective Students<hr/>",   	"./studiesProspective.php");
        menuBar[4][3] = new MenuItem( "Academic Calendar",		"http://www.ou.edu/admissions/home/academic_calendar.html");
		menuBar[4][4] = new MenuItem( "Course Catalog",			"./courseCatalog.php");
		menuBar[4][5] = new MenuItem( "Course Schedule/Syllabi","./courseSchedule.php");
		menuBar[4][6] = new MenuItem( "Student Awards",			"./studentAwards.php");


    // Events Menu
    menuBar[5] = new Array(2);
        menuBar[5][0] = new MenuItem( "News",                   "./news.php");
        menuBar[5][1] = new MenuItem( "Calendar of Events",     "./eventsCalendar.php");
       // menuBar[5][2] = new MenuItem( "Links",                  "./");

    // Jobs Menu
    menuBar[6] = new Array(5);
        menuBar[6][0] = new MenuItem( "For Undergrads",         "./careersUnderGrad.php");
        menuBar[6][1] = new MenuItem( "For BS Grads",           "./careersBS.php");
        menuBar[6][2] = new MenuItem( "For MS Grads",           "./careersMS.php");
        menuBar[6][3] = new MenuItem( "For Ph.D. Grads",        "./careersPhD.php");
        menuBar[6][4] = new MenuItem( "Other Resources",        "./careersOther.php");

    // Contact Us Menu
    menuBar[7] = new Array(2);
    	menuBar[7][0] = new MenuItem("SoM Contact",       "./contact.php");
        menuBar[7][1] = new MenuItem("Webmaster Contact", "http://support.rcs.ou.edu");
        

    // The menu system has now been built.

    menus = new Array(8);
    for (var a = 0; a < 8; a++) {
        menus[a] = document.getElementById("menu" + a);
    }

    menuDiv = document.getElementById("menu");
    submenuDiv = document.getElementById("submenu");
}

function showMenu(menuNumber) {
    mouseOver(menuNumber, -1);
    hideSubmenu();
    menuItems = new Array();

    var content = "<table cellspacing=0 cellpadding=4>";

    // Build the table of menu items
    for (var a = 0; a < menuBar[menuNumber].length; a++) {
        content += "<tr><td id=\"menuItem" + a + "\" class=\"menuRoot1\" onmouseover=\"this.className='menuRoot2'; mouseOver(" + menuNumber + ", " + a + ");\""; 
        content +=" onmouseout=\"this.className='menuRoot1'; mouseOut();\"";

        // Make it a link if it's an item and doesn't have a submenu attached to it
        if (typeof (menuBar[menuNumber][a].link) != "undefined"){
            content += " onclick=\"location.href='" + menuBar[menuNumber][a].link + "'\"";
        }

        content += ">" + menuBar[menuNumber][a].text + "</td></tr>";
    }

    content += "</table>";

    menuDiv.innerHTML = content;
    
    // Add the ">"'s after the fact (must be done this way because it relies on the menu <div> ... </div> tags not being empty.)
    for (var a = 0; a < menuBar[menuNumber].length; a++) {
        if (typeof (menuBar[menuNumber][a].items) != "undefined") {
            var menuItem = menuDiv.childNodes[0].childNodes[0].childNodes[a].childNodes[0]; 
            var coords = [menuItem.offsetLeft, menuItem.offsetTop];
            menuItem.innerHTML += "<div style=\"position:absolute; z-index:4; top:" + (coords[1] - 2) + "px; left:" + (coords[0] + 157) + "px;\">></div>";
        }
    }

    var coords = findPos(menus[menuNumber]);
    menuDiv.style.top = coords[1] + menus[menuNumber].offsetHeight - 1 + 'px';
    menuDiv.style.left = coords[0] + 'px';
    menuDiv.style.width = "170px";

}

function showSubmenu(menuParent, menuParentNumber) {
    var content = "<table cellspacing=0 cellpadding=4>";

    // Build the table of menu items
    for (var a = 0; a < menuParent.items.length; a++) {
        content += "<tr><td class=\"menuRoot1\" onmouseover=\"this.className='menuRoot2'; mouseOver(-1, -1);\"";
        content += " onmouseout=\"this.className='menuRoot1'; mouseOut();\"";
        content += " onclick=\"location.href='" + menuParent.items[a].link + "'\">" + menuParent.items[a].text + "</td></tr>";
    }

    content += "</table>";

    submenuDiv.innerHTML = content;

    var coords = findPos(menuDiv.childNodes[0].childNodes[0].childNodes[menuParentNumber].childNodes[0]);

    submenuDiv.style.top = coords[1] + 'px';
    submenuDiv.style.left = coords[0] + menuDiv.offsetWidth + 2 + 'px';
    submenuDiv.style.width = "140px";

}

function hideMenu() {
    // Clear the HTML out of the menu <div> ... </div> tags
    menuDiv.innerHTML = "";
    hideSubmenu();
}

function hideSubmenu() {
    submenuDiv.innerHTML = "";
}

function mouseOver(menuNumber, menuItem) {
    // If the timer has been set, clear it (prevents the menu from being hidden if the user has only moved the 
    // cursor from one menu item to another, as opposed to moving the cursor completely off the menu.)
    if (mouseOverTimer != undefined) {
        window.clearTimeout(mouseOverTimer);
    }

    // Hide the submenu if the cursor has changed menus
    if (menuItem >= 0 && menuNumber >= 0) {
        hideSubmenu();
    }

    // If the menu item is the root for a submenu, show the submenu
    if (menuItem >= 0 && typeof (menuBar[menuNumber][menuItem].items) != "undefined") {
        showSubmenu(menuBar[menuNumber][menuItem], menuItem);
    } 
}

function mouseOut(menuNumber) {
    // Set a timer for a X milliseconds.  When it fires, hide the menu.
    mouseOverTimer = window.setTimeout("hideMenu()", 100);
}

function MenuItem(newText, newLink) {
    this.text = newText;
    this.link = newLink;
}

function Menu(newText, newItems) {
    this.text = newText;
    this.items = newItems;
}

function findPos(obj) {
	var curleft = 0;
	var curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}
