// Globals

// An optional string that gives the root of the menu files. This should be empty if 
// the "images", "css", and "js" folders are located at the root of the website. Otherwise
// specify the subfolder where those folders are located. Do not end the path with a "/".
var ROOTPREFIX = "";
//var ROOTPREFIX = "";

// The global timer that controls whether the active menu is shown
var TIMERID = null;
var TIMEOUT = 100; // timeout in ms

var SELECTEDNAVITEM;
//var VERTICALOFFSET = 0;

// 
//  Menu Navigation Objects
//

// NavMenu object.
// This is the main object that implements the dropdown menu. It encapsulates data
// and methods necessary to hide/show a particular menu at the appropriate time.
//
// Params:
//      sID: a string that must be unique among all the menus. It is used to construct element IDs, most 
//           notably for the div. Must be coordinated with image filenames for menu images (and rollovers).
//      nLeft: the pixel position of the left edge of the menu (with respect to entire document)
//      nTop: the pixel position of the top edge of the menu (with respect to entire document)
//      linkArray: an array of NavMenuItem objects to be used in the menu
//
function NavMenu(sID, nLeft, nTop, linkArray)
{
    // Allocate linkArray if not provided
    if( linkArray == null )
        linkArray = new Array(0);

    // Properties
    this.id = sID;
    this.left = nLeft;
    this.top = nTop;
    this.isActive = false;
    this.defaultStateIsOn = false;
    this.links = linkArray;
	this.sLink = null; // DINGO ADD: Selected sub item

    // Methods

    // writeDiv: writes the div to the page in a hidden state, ready to be shown at the correct location
    // when activated.
    this.writeDiv = function() 
    {
		// Figure out our index into the navMenus array
        var nIndex = -1;
        var nTemp = 0;
        for( nTemp = 0; nTemp < navMenus.length; nTemp++ )
        {
            if( navMenus[nTemp].id == this.id )
                nIndex = nTemp;
        }

        // If not found, return
        if( nIndex == -1 )
            return;
        // Write div
        document.writeln("<div id=\"navmenudiv_" + this.id + "\" style=\"position:absolute; left:" + this.left + "px; top:" + (VERTICALOFFSET + this.top) + "px; visibility:hidden;z-index:20;\" onmouseout=\"StartTimer();\" onmouseover=\"StopTimer();\" class=\"navmenu\">");
		//image added to top of menu to simulate the bottom of the button
		//document.writeln("<img style='position:absolute; top:0px;' src=\"" + ROOTPREFIX + "/images/nav_top_" + this.id + ".gif\">");
		
		if( this.links.length > 0 )
        {
			document.writeln("<div style='position:absolute; top:1px;' class=\"navcontainer\">");

			var sMouseEvents = "";
            for( nTemp = 0; nTemp < this.links.length; nTemp++ )
            {
				sMouseEvents = " onmouseover=\"navMenus[" + nIndex + "].links[" + nTemp + "].mouseOver();\" onmouseout=\"navMenus[" + nIndex + "].links[" + nTemp + "].mouseOut();\"";
                document.writeln("<div " + sMouseEvents);
				//document.writeln(" style=\"padding-top:5px;padding-bottom:5px;\" ");
				if (this.links.length == nTemp+1) {document.writeln(" style=\"border-bottom:none;;\" ");}
				document.writeln(" id=\"navmenuitemtd_" + this.links[nTemp].id + "\" class=\"navmenuitem\"><a id=\"navmenuitemlink_" + this.links[nTemp].id + "\" href=\"" + this.links[nTemp].href + "\">" + this.links[nTemp].linkText + "</a>");
                document.writeln("</div>");
            }

            document.writeln("</div>");
        }
    
        document.writeln("</div>");
    }

    // mouseOver: called when the user mouses over the navigation element for the menu (the menu image).
    // activates the menu and deactivates all the others.
    this.mouseOver = function() 
    {
        // Clear any existing timers
        StopTimer();

        // Activate this menu
        this.activate();

        // Deactivate the others
        var nTemp = 0;
        for( nTemp = 0; nTemp < navMenus.length; nTemp++ )
        {
            if ( navMenus[nTemp].id != this.id ) {
				//navMenus[nTemp].deactivate();
				navMenus[nTemp].onOverAnother() ;
			}
        }
    }

    // mouseOut: called when the user mouses out of the navigation element for the menu (the menu image)
    // starts the timer which checks whether the menu should stay open
    this.mouseOut = function()
    {
        // Start timer 
        StartTimer();
    }

	this.onOverAnother = function() 
	{
		this.deactivate() ;	
		
		// DINGO ADD: Set to low button state
		var imgElem = document.getElementById("nav_" + this.id);
		//alert( "imgElem: " + imgElem ) ;
		if( imgElem != null && !this.defaultStateIsOn) {
		   imgElem.src = ROOTPREFIX + "/images/buttons/nav_" + this.id + ".gif";
		} else {
		   imgElem.src = ROOTPREFIX + "/images/buttons/nav_" + this.id + "_active.gif";
		}
	}
	
	this.onOutAnother = function() 
	{
		//this.deactivate() ;	
		
		// DINGO ADD: Set to low button state
		// Switch to regular image
        this.resetImageToDefault();
	}
	

    // activate: set rollover image and show the menu div
    this.activate = function()
    {		
		// Switch to rollover image
	    var imgElem = document.getElementById("nav_" + this.id);
        if( imgElem != null )
           imgElem.src = ROOTPREFIX + "/images/buttons/nav_" + this.id + "_over.gif";  

        // Show the div
        var menuDiv = document.getElementById("navmenudiv_" + this.id);
        if( menuDiv != null )
		{
			for( var i=0; i<this.links.length; i++ ) 
			{
				if( i==this.sLink )
				{ 
					// Switch to rollover image
					var imgElem = document.getElementById("nav_" + this.links[i].id);
					if( imgElem != null )
					{
					   this.links[i].disable() ;
					   imgElem.src = ROOTPREFIX + "/images/buttons/nav_" + this.links[i].id + "_over.gif"; 
					}
				}
			}
			
			
            menuDiv.style.visibility = "visible";
		}

        // Mark self as active
        this.isActive = true;
    }

    // deactivate: set regular image and hide the menu div
    this.deactivate = function()
    {
		// Switch to regular image
        //this.resetImageToDefault();
		
		
        // Hide the div
        var menuDiv = document.getElementById("navmenudiv_" + this.id);
        if( menuDiv != null )
            menuDiv.style.visibility = "hidden";
				
        // Mark self as inactive
        this.isActive = false;
		
		   
	
    }

    this.setDefaultStateOn = function(bDefaultOn,sIndex)
    {
        // Set default state
        this.defaultStateIsOn = bDefaultOn;
		this.sLink = sIndex ;
		
    }

    this.resetImageToDefault = function()
    {
        // Switch to regular image
	    var imgElem = document.getElementById("nav_" + this.id);
        if( imgElem != null )
        {
			//alert( "this.defaultStateIsOn: " + this.defaultStateIsOn );
           if( this.defaultStateIsOn )
               imgElem.src = ROOTPREFIX + "/images/buttons/nav_" + this.id + "_active.gif"; 
           else
               imgElem.src = ROOTPREFIX + "/images/buttons/nav_" + this.id + ".gif"; 
        }
    }
}

// NavMenuItem object.
// A simple object to store info about each link in the menu.
//
// Params:
//      sID: a string that must be unique among the full set menu items. It is used to construct element ids.
//      sHREF: the href where you want the link to go
//      sLinkText: the text to appear in the link
//
function NavMenuItem(sID, sHREF, sLinkText)
{
    // Properties
    this.id = sID;              // unique string used to construct ids for various elements; ex. "company_careers"
    this.href = sHREF;          // the href; ex. "company/careers.html"
    this.linkText = sLinkText;  // the link text; ex. "Careers"
	this.enabled = true ;
	
    // Methods

    // mouseOver: change styles of menu item
    this.mouseOver = function()
    {
		// Switch to rollover image
	    var imgElem = document.getElementById("nav_" + this.id);
        if( imgElem != null && this.enabled )
           imgElem.src = ROOTPREFIX + "/images/nav_" + this.id + "_over.gif"; 
    }

    // mouseOut: change styles of menu item
    this.mouseOut = function()
    {
        // Switch to regular image
	    var imgElem = document.getElementById("nav_" + this.id);
        if( imgElem != null && this.enabled )
			imgElem.src = ROOTPREFIX + "/images/nav_" + this.id + ".gif"; 
    }
	
	this.disable = function() 
	{
		this.enabled = false ;
	}
	
	this.enable = function() 
	{
		this.enabled = true ;
	}
}

// Global timer methods
function StartTimer()
{
    StopTimer();
    TIMERID = setTimeout("CheckMenuVis()", TIMEOUT);
}

function StopTimer()
{
    if( TIMERID != null )
    {
        clearTimeout(TIMERID);
        TIMERID = null;
    }
}

function CheckMenuVis()
{
    // Clear timer
    StopTimer();

    // Find active menu and deactivate it
    var nTemp = 0;
    for( nTemp = 0; nTemp < navMenus.length; nTemp++ )
    {
        if( navMenus[nTemp].isActive )
		{
            navMenus[nTemp].deactivate();
			
		}
		navMenus[nTemp].onOutAnother() ;
    }
	
}

//
// Below is an example of how to set up several drop down menus and their menu items.
// You will need to change this depending on the layout of the site. This, in conjunction
// with sample.html (which will also change depending on the layout of the site) is what 
// defines the menus.
//

// Construct Alumni Communities menu
var aLinks = new Array(6);
aLinks[0] = new NavMenuItem("ac_classes", ROOTPREFIX + "/main/alumni_communities/", "Overview");
aLinks[1] = new NavMenuItem("ac_classes", ROOTPREFIX + "/main/alumni_communities/classes/", "Classes");
aLinks[2] = new NavMenuItem("ac_reg_assoc", ROOTPREFIX + "/main/alumni_communities/regional_associations/", "Regional Associations");
aLinks[3] = new NavMenuItem("ac_grad_alumni", ROOTPREFIX + "/main/alumni_communities/graduate_alumni/", "Graduate Alumni");
aLinks[4] = new NavMenuItem("ac_aff_groups", ROOTPREFIX + "/main/alumni_communities/affiliate_groups/", "Affiliated Groups");
aLinks[5] = new NavMenuItem("ac_fut_alumni", ROOTPREFIX + "/main/alumni_communities/future_alumni/", "Future Alumni");
var menu1 = new NavMenu("communities", 180, 131, aLinks);

// Construct Goin' back menu
var bLinks = new Array(6);
bLinks[0] = new NavMenuItem("gb_alumniday", ROOTPREFIX + "/main/goinback/", "Overview");
bLinks[1] = new NavMenuItem("gb_alumniday", ROOTPREFIX + "/main/goinback/alumni_day/", "Alumni Day");
bLinks[2] = new NavMenuItem("gb_reunions", ROOTPREFIX + "/main/goinback/reunions/", "Reunions");
bLinks[3] = new NavMenuItem("gb_camp_events", ROOTPREFIX + "/main/goinback/football_weekends/", "Football Weekends");
bLinks[4] = new NavMenuItem("gb_camp_events", ROOTPREFIX + "/main/goinback/on_campus_events/", "On Campus Events");
bLinks[5] = new NavMenuItem("gb_camp_events", ROOTPREFIX + "/main/goinback/visiting_campus/", "Visiting Campus");
var menu2 = new NavMenu("goinback", 326, 131, bLinks);


// Construct Education & Travel menu
var cLinks = new Array(6);
cLinks[0] = new NavMenuItem("et_onl_prog", ROOTPREFIX + "/main/education_travel/", "Education & Travel Overview");
cLinks[1] = new NavMenuItem("et_onc_prog", ROOTPREFIX + "/main/education_travel/on_campus_programs/", "On Campus Events");
cLinks[2] = new NavMenuItem("et_hs_prog", ROOTPREFIX + "/main/education_travel/home_study_programs/", "Alumni Studies Courses");
cLinks[3] = new NavMenuItem("et_onl_prog", ROOTPREFIX + "/main/education_travel/online_programs/", "Lecture Archives");
cLinks[4] = new NavMenuItem("et_p_jour", ROOTPREFIX + "/main/education_travel/princeton_journeys/", "Princeton Journeys");
cLinks[5] = new NavMenuItem("et_ed_news", ROOTPREFIX + "/main/education_travel/alumnied_newsletter/", "Alumni Education Newsletter");
var menu3 = new NavMenu("edtravel", 413, 131, cLinks);

// Construct Volunteers menu
var dLinks = new Array(8);
dLinks[0] = new NavMenuItem("vol_ecol", ROOTPREFIX + "/main/volunteers/", "Overview");
dLinks[1] = new NavMenuItem("vol_ways", ROOTPREFIX + "/main/volunteers/ways_to_volunteer/", "Ways to Volunteer");
dLinks[2] = new NavMenuItem("vol_ecol", ROOTPREFIX + "/main/volunteers/alumni_council_committees/", "Alumni Council Committees");
dLinks[3] = new NavMenuItem("vol_class", ROOTPREFIX + "/main/volunteers/class_volunteers/", "Class Volunteers");
dLinks[4] = new NavMenuItem("vol_reu", ROOTPREFIX + "/main/volunteers/reunions_volunteers/", "Reunion Volunteers");
dLinks[5] = new NavMenuItem("vol_reg", ROOTPREFIX + "/main/volunteers/regional_volunteers/", "Regional Volunteers");
dLinks[6] = new NavMenuItem("vol_awards", ROOTPREFIX + "/main/volunteers/awards/", "Awards");
dLinks[7] = new NavMenuItem("vol_note", ROOTPREFIX + "/main/volunteers/take_note/", "Take Note Profiles");
var menu4 = new NavMenu("volunteers", 546, 131, dLinks);


// Construct Tigernet Services menu
var eLinks = new Array(7);
eLinks[0] = new NavMenuItem("ts_about", "http://alumni.princeton.edu/main/tigernet/", "TigerNet Overview");
eLinks[1] = new NavMenuItem("ts_about", "https://alumnicas.princeton.edu/tigernetcas/login?service=https://tigernet.princeton.edu/olc/pub/PRU/login/PRU-extauth.cgi%3furl=%26province=tigernetcas", "TigerNet Login");
eLinks[2] = new NavMenuItem("ts_about", "http://tigernet.princeton.edu/olc/pub/PRU/volunteer.html", "Volunteer Services");
eLinks[3] = new NavMenuItem("ts_about", "http://alumni.princeton.edu/main/tigernet/updates/", "TigerNet Services Bulletins");
eLinks[4] = new NavMenuItem("ts_join", "http://alumni.princeton.edu/main/tigernet/policy/", "TigerNet Policy");
eLinks[5] = new NavMenuItem("ts_join", "http://alumni.princeton.edu/main/tigernet/faqs/", "FAQs");
eLinks[6] = new NavMenuItem("ts_join", "https://secure.tigernet.princeton.edu/olc/pub/PRU/onlinegiving/showGivingForm.jsp?form_id=1870", "Contact Us");
var menu5 = new NavMenu("tigernet", 593, 131, eLinks);

// Construct array of menus
var navMenus = new Array(5);
navMenus[0] = menu1;
navMenus[1] = menu2;
navMenus[2] = menu3;
navMenus[3] = menu4;
navMenus[4] = menu5;
