var homeUrl = "";
var homeLogo = ""
var homeLogoAltText = "The D.&nbsp;E.&nbsp;Shaw Group";
var currentMenu = "";
var currentTopic = "";
var currentSubtopic = "";

// Add jquery.js to the current page
var head = document.getElementsByTagName("head")[0];         
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "/jquery.js";
head.appendChild(script);

// Defining class for storing Menu parameters
function Menu(name, target, style, flyoutDivClass, ulId, defaultTopicImage, spacerColor, overColor, outColor) {
    this.name = name; // the display name of the menu
    this.targetUrl = target; // the target url 
    this.styleClass = style; // the CSS class for the menu
    this.ulId = ulId; // the unordered list element id associated with the left menus
    this.flyoutMenuClass = flyoutDivClass; // the CSS class for the flyout menu items
    this.defaultTopicImage = defaultTopicImage; // the default image for the menu
    this.spacerColor = spacerColor; // the background color of the spacer depending on the current menu
    this.overColor = overColor; // the background color when the cursor is over the menu
    this.outColor = outColor; // the default background color when the cursor is out of the menu
        
    this.topics = new Array(); // defining the array for adding topics for each menu

    this.addTopic = addTopic; // Adding a method to add topics
}

function Topic(name, leftname, target, image) {
	return Topic(name, leftname, target, image, "");
}

function Topic(name, leftname, target, image, className) {
    this.name = name; // the display name of the topic
		this.leftname = leftname;
    this.targetUrl = target; // the target url
    this.image = image; // the default image for the topic

    this.sub = new Array(); // defining the array for adding subtopics for each topic

    this.addSubtopic = addSubtopic; // Including a method to add sub topics
    if (className) this.className = className;
}

function addTopic(topic) {
    this.topics[this.topics.length] = topic;
}

function addSubtopic(topic) {
    this.subtopics[this.subtopics.length] = topic;
}

var menubar;

// Function to initialize the menus
function initMenuItems(currentMenu) {

	// Creating the Menu array 
	menubar = new Array(8);
	
	// Initializing the Menu array
	
	menubar[0] = new Menu("About Us", "index.html", "menu_WWA",  "section_links_WWA",
	     "/images/WWA.jpg", "#FFFFFF", "#5EC72A", "#C9F284");
	/*menubar[0].addTopic(new Topic("Overview", "Who We Are", "/WhoWeAre.html", "/images/WWA.jpg"));
	menubar[0].addTopic(new Topic("Founder", "Founder", "/Founder.html", "/images/david.jpg"));
	menubar[0].addTopic(new Topic("Staff", "Staff", "/Staff.html", "/images/Staff.jpg"));
	menubar[0].addTopic(new Topic("News and Press", "News and Press", "/NewsandPress.html", "/images/News.jpg"));
	*/

	/*menubar[1] = new Menu("", "/WhatWeDo.html", "menu_WWD", "menulist_WWD", "section_links_WWD",
	     "images/WWD.jpg", "#FFFFFF", "#F47721", "#C9F284");
	menubar[1].addTopic(new Topic("Overview", "What We Do", "/WhatWeDo.html", "/images/WWD.jpg"));
	menubar[1].addTopic(new Topic("Quantitative Strategies", "Quantitative Strategies", "/QuantitativeStrategies.html", "/images/quant.jpg"));
	menubar[1].addTopic(new Topic("Qualitative Strategies", "Qualitative Strategies", "/QualitativeStrategies.html", "/images/Qual.jpg"));
	*/
	menubar[1] = new Menu("Spacer1", "", "solid_spacer", "", "",
	    "images/", "#000000", "#E1384E", "#C9F284");
	
	menubar[2] = new Menu("Recruiting", "recruit.html", "menu_R", "section_links_R",
	     "/images/Recruit.jpg", "#FFFFFF", "#5393C3", "#C9F284");
	
	/*
	menubar[3].addTopic(new Topic("Overview", "Recruiting", "/Recruiting.html", "/images/recruit2.jpg"));
	menubar[3].addTopic(new Topic("Culture", "Culture", "/Culture.html", "/images/Culture.jpg"));
	menubar[3].addTopic(new Topic("Employee Profiles", "Employee Profiles", "/EmployeeProfiles.html", "/images/Employee.jpg"));
	menubar[3].addTopic(new Topic("Careers", "Careers", "/careers.html", "images/Apply.jpg"));
	menubar[3].addTopic(new Topic("Job Opportunities", "Job Opportunities", "/recruit/JobOpportunities.html", "/images/Jobs.jpg", "jobops"));
	menubar[3].addTopic(new Topic("On-Campus Recruiting", "On-Campus Recruiting", "/OnCampus.html", "/images/OnCampus.jpg", "oncampus"));
	menubar[3].addTopic(new Topic("FAQs", "FAQs", "/FAQs.html", "/images/OnCampus.jpg", "faqs"));
	
	*/
	/*menubar[4] = new Menu("Investor Relations", "/InvestorRelations.html", "menu_IR", "section_links_IR", "",
	    "images/IR2.jpg", "#FFFFFF", "#008469", "#C9F284");
	//menubar[5] = new Menu("Spacer", "", "menu_spacer", "", "",
	  //"images/", "#FFFFFF", "#E1384E", "#C9F284");
	  
	  */

	menubar[3] = new Menu("Contact Us", "Contact_us.html", "menu_CU", "section_links_CU", "",
	    "images/Contact.jpg", "#FFFFFF", "#E1384E", "#C9F284");

	menubar[4] = new Menu("ContainerOpen", "", "menu_SM_S_container", "", "", "", "", "", "");

	menubar[5] = new Menu("Sitemap", "sitemap.html", "menu_SM", "section_links_SM", "",
	    "/images/20.jpg", "#FFFFFF", "#666F74", "#C9F284");

	menubar[6] = new Menu("NewLine", "", "", "", "", "", "", "", "");

	/*menubar[7] = new Menu("Search", "/Search.html", "menu_S", "section_links_S", "",
	    "images/Search.jpg", "#FFFFFF", "#7D51E1", "#C9F284");
		*/
	menubar[7] = new Menu("ContainerClose", "", "", "", "", "", "", "", "");
	
}

// Event hanlers for capturing mouse events
function menuEvent(e) {
	
    if (!e) { 
        e = window.event;
    }
    var obj;
    if (e.srcElement) { 
        obj = e.srcElement;
    } else {
        obj = e.target;
    }
    if (e.type == "mouseover") {
        showMenu(obj.id.match('\\d+$'));
        
    } else if (e.type == "mouseout") {
        hideMenu(obj.id.match('\\d+$'));
    }
}

// Specific event handlers for IE
function menuEventIE(menuName){
	var e = window.event;
	if (e.type == "mouseover") {
        showMenu(menuName.match('\\d+$'));
    } else if (e.type == "mouseout") {
        hideMenu(menuName.match('\\d+$'));
    }
}

// Function to generate the MenuBar
function buildMenuBar(currentMenu) {
    initMenuItems(currentMenu);

	// Obtaining the menubar div element from the HTML page
    var menuHolder = document.getElementById("menubar");

    if (menuHolder) {
    	//Creating a Div element to hold the logo image
    	/*
        var logoDiv = document.createElement("DIV");
        logoDiv.className = "logo_home";
        
        // Adding a anchor element to the div and the image within the anchor
        var logoAnchor = document.createElement("A");
        logoAnchor.href = homeUrl;
        logoDiv.appendChild(logoAnchor);
		
        var logoImg = document.createElement("IMG");
        logoImg.alt = homeLogoAltText;
        logoImg.src = homeLogo;
        logoImg.border = 0;
        logoImg.width = 187;
        logoImg.height = 99;
        logoAnchor.appendChild(logoImg);
		*/

		var spacerStyle;
		var currentMenuIndex = 0;
		var containerDiv = null;
		var inContainer = false;
		// Looping through the menus in reverse order
		for (var i = 0 ; i < menubar.length; i++) 
		{
			var name = menubar[i].name;
			if (name == "ContainerOpen")
			{
				containerDiv = document.createElement("DIV");
				containerDiv.className = menubar[i].styleClass;
				inContainer = true;
				continue;
			}
			if (name == "ContainerClose")
			{
				inContainer = false;
				menuHolder.appendChild(containerDiv);
				continue;
			}
			if (name == "NewLine")
			{
				var newLine = document.createElement("BR");
				if (inContainer == true)
					containerDiv.appendChild(newLine);
				else
					menuHolder.appendChild(newLine);
				continue;
			}
		
			var menuDiv = document.createElement("DIV");
			menuDiv.className = menubar[i].styleClass;
			menuDiv.id = "menuDiv"+(i+1);
			
			if (inContainer == true)
				containerDiv.appendChild(menuDiv);
			else
				menuHolder.appendChild(menuDiv);
			if(menubar[i].name != "Spacer" && menubar[i].name != "Spacer1")
			{
				// Not adding event handlers for the Spacer div
				menuDiv.innerHTML = menubar[i].name;
				if (document.layers) 
				{ // only for NN 4.
					item.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT);
				}
				menuDiv.url = menubar[i].targetUrl;
				if (document.all)
				{ // Incase of IE.
	            	menuDiv.onmouseover = "menuEventIE('" + menuDiv.id + "');";
	            	menuDiv.onmouseout = "menuEventIE('" + menuDiv.id + "');";
	            	menuDiv.onclick = "clickMenuItemIE('" + menuDiv.id + "');";
				}
				else
				{
					menuDiv.onmouseover = menuEvent;
	    			menuDiv.onmouseout = menuEvent;
	    			menuDiv.onclick = clickMenuItem;
	        	}
        	}
        	else
        	{
        		// If the div is a Spacer, store the style object to change
        		// the background color of the Spacer as per the current Menu
        		spacerStyle = getStyleObj(menuDiv.id);
			}
			
			if(currentMenu == menubar[i].name)
			{
				// If the menu is the current one, change the class to the 
				// one would be if the cursor was over it
        		menuDiv.className = menubar[i].styleClass + "_over";
        		//var currMenuClass = getStyleObj(menuDiv.id);
        		//currMenuClass.color = "White";
        		currentMenuIndex = i;
        	}
        }
        // Assign the backgroundColor of the Spacer as per the current Menu.
		// spacerStyle.backgroundColor = menubar[currentMenuIndex].spacerColor;
		
        // Generating the flyout menus
        for (var i = 0; i < menubar.length; i++) {
        
        	// Creating a Div to hold the flyout menus for each menu
            var menuDiv = document.createElement("DIV");
            menuDiv.id = "menu" + (i + 1);
            menuDiv.className = menubar[i].flyoutMenuClass;
            
            // Looping through the topics and generating topic links


            for (var j = 0; j < menubar[i].topics.length; j++) {
                
                var menuItemAnchor = document.createElement("A");
                menuItemAnchor.id = "menu"+(i+1)+"item"+(j+1);
               	menuItemAnchor.href = menubar[i].topics[j].targetUrl;
               	menuItemAnchor.innerHTML = menubar[i].topics[j].name;
               	menuDiv.appendChild(menuItemAnchor);
                
                // Adding a breakline element after the anchor
               	var menuItemLineBreak = document.createElement("BR");
				menuDiv.appendChild(menuItemLineBreak);	
            }
            menuHolder.appendChild(menuDiv);
            //menuHolder.appendChild(logoDiv);
            
        }
        if(document.all)
	        menuHolder.innerHTML = menuHolder.innerHTML;
    }
    // Generating the left panel 
    buildLeftMenu();
}

// Function to generate the left panel
function buildLeftMenu() {
	
	// Obtaining the left panel div element
    var menuHolder = document.getElementById("leftPanel");
    if (menuHolder) {
    	var menuIndex = -1;
    	// Obtaining the Current Menu Index
        for (var i = 0; i < menubar.length; i++) {
            if (menubar[i].name == currentMenu) {
                menuIndex = i;
                break;
            }
        }

        if (menuIndex == -1)
            return;

        var topicIndex = -1;
        // Obtaining the current topic index from the current menu index
        if (currentTopic) {
            for (var i = 0; i < menubar[menuIndex].topics.length; i++) {
                if (menubar[menuIndex].topics[i].name == currentTopic) {
                    topicIndex = i;
                    break;
                }
            }
        }

        var subtopicIndex = -1;
        // Obtaining the current sub topic index from the current menu and topic index
        if (topicIndex != -1 && currentSubtopic) {
            for (var i = 0; i < menubar[menuIndex].topics[topicIndex].subtopics.length; i++) {
                if (menubar[menuIndex].topics[topicIndex].subtopics[i].name == currentSubtopic) {
                    subtopicIndex = i;
                    break;
                }
            }
        }
        
        // Adding the section image for the current topic or menu
        var photoDiv = document.createElement("DIV");
        photoDiv.className = "section_image";
        var sideImg = document.createElement("IMG");
        if (topicIndex != -1) {
            sideImg.src = menubar[menuIndex].topics[topicIndex].image;
        } else {
            sideImg.src = menubar[menuIndex].defaultTopicImage;
        }
        photoDiv.appendChild(sideImg);
		menuHolder.appendChild(photoDiv);
		
		// Generating an unordered list
		var leftMenuList = document.createElement("UL");
		leftMenuList.className = "section-links";
		leftMenuList.dir = "rtl";
		leftMenuList.id = menubar[menuIndex].ulId;
		
        for (var i = 0; i < menubar[menuIndex].topics.length; i++) {
            //Creating the List item for each topic
            var leftMenuItem = document.createElement("LI");
			leftMenuItem.className = menubar[menuIndex].topics[i].className;
            var topicAnchor = document.createElement("A");
            topicAnchor.href = menubar[menuIndex].topics[i].targetUrl;
            topicAnchor.innerHTML = menubar[menuIndex].topics[i].leftname;
            leftMenuItem.appendChild(topicAnchor);
            //
            // Assinging a class if the topic is the current topic and
            // if no subtopic is found
            //
            if(menubar[menuIndex].topics[i].leftname == currentTopic 
            		&& currentSubTopic == ""){
            	leftMenuItem.className = "current";
            }
			
            leftMenuList.appendChild(leftMenuItem);            
            
            // Looping through the subtopics
			if (menubar[menuIndex].topics[i].subtopics && i == topicIndex && menubar[menuIndex].topics[i].subtopics.length) {
               for (var j = 0; j < menubar[menuIndex].topics[i].subtopics.length; j++) {
					var leftSubMenuItem = document.createElement("LI");
					
					if(currentSubTopic == menubar[menuIndex].topics[i].subtopics[j].name){
						// If a subtopic is the current one, assign the 'current' class
						leftSubMenuItem.className = "current";	
					}
               		var subtopicAnchor = document.createElement("A");
                    subtopicAnchor.className = "sub_list";
                    subtopicAnchor.href = menubar[menuIndex].topics[i].subtopics[j].targetUrl;
                    subtopicAnchor.innerHTML = menubar[menuIndex].topics[i].subtopics[j].name;
                    leftSubMenuItem.appendChild(subtopicAnchor);
                    leftMenuList.appendChild(leftSubMenuItem);            
                }
            }
        }
        // Add the generated unordered list to the left panel div
        menuHolder.appendChild(leftMenuList); 
	    menuHolder.outerHTML = menuHolder.innerHTML;
		if ($("td.content H1").hasClass("thanks_page"))
		{
			var x = $("#section_links_R").find("li.jobops");
			x.addClass("current");
		}
		if ($("td.content H1").hasClass("referral_page"))
		{
			var x = $("#section_links_R").find("li.jobops");
			x.addClass("current");
		}
        if ($("td.content H1").hasClass("timeoutjo_page"))
		{
			var x = $("#section_links_R").find("li.jobops");
			x.addClass("current");
		}
		if ($("td.content H1").hasClass("error_page"))
		{
			var x = $("#section_links_R").find("li.jobops");
			x.addClass("current");
		}
    }
}
