// 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 = "http://museum.stanford.edu";

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

var SELECTEDNAVITEM;

// 
//  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 + "; top:" + this.top + "; visibility:hidden;z-index:20;\" onmouseout=\"StartTimer();\" onmouseover=\"StopTimer();\" class=\"navmenu\">");
		document.writeln("<div class=\"navbg\"></div>");

        if( this.links.length > 0 )
        {
			document.writeln("<div 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(" id=\"navmenuitemtd_" + this.links[nTemp].id + "\" class=\"navmenuitem\"><a id=\"navmenuitemlink_" + this.links[nTemp].id + "\" class=\"navmenuitem\" 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_main_" + this.id);
		//alert( "imgElem: " + imgElem ) ;
		if( imgElem != null )
		   imgElem.src = ROOTPREFIX + "/images/nav_main_" + this.id + ".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_main_" + this.id);
        if( imgElem != null )
           imgElem.src = ROOTPREFIX + "/images/nav_main_" + 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/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_main_" + this.id);
        if( imgElem != null )
        {
			//alert( "this.defaultStateIsOn: " + this.defaultStateIsOn );
           if( this.defaultStateIsOn )
               imgElem.src = ROOTPREFIX + "/images/nav_main_" + this.id + "_over.gif"; 
           else
               imgElem.src = ROOTPREFIX + "/images/nav_main_" + 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 visit menu
var visitLinks = new Array(4);
visitLinks[0] = new NavMenuItem("visit_plan", ROOTPREFIX + "/visit/plan_a_visit.html", "<img id=\"nav_visit_plan\" src=\"" + ROOTPREFIX + "/images/nav_visit_plan.gif\" alt=\"Plan a Visit\">");
visitLinks[1] = new NavMenuItem("visit_tour", ROOTPREFIX + "/visit/docent_tours.html", "<img id=\"nav_visit_tour\" src=\"" + ROOTPREFIX + "/images/nav_visit_tour.gif\" alt=\"Docent Tours\">");
visitLinks[2] = new NavMenuItem("visit_book", ROOTPREFIX + "/visit/bookshop.html", "<img id=\"nav_visit_book\" src=\"" + ROOTPREFIX + "/images/nav_visit_book.gif\" alt=\"Bookshop\">");
visitLinks[3] = new NavMenuItem("visit_cafe", ROOTPREFIX + "/visit/cafe.html", "<img id=\"nav_visit_cafe\" src=\"" + ROOTPREFIX + "/images/nav_visit_cafe.gif\" alt=\"The Caf&eacute;\">");
var menuVisit = new NavMenu("visit", 179, 65, visitLinks);

// Construct view menu
var viewLinks = new Array(4);
viewLinks[0] = new NavMenuItem("view_coll", ROOTPREFIX + "/view/collections.html", "<img id=\"nav_view_coll\" src=\"" + ROOTPREFIX + "/images/nav_view_coll.gif\" alt=\"Collections\">");
viewLinks[1] = new NavMenuItem("view_exh", ROOTPREFIX + "/view/exhibitions.html", "<img id=\"nav_view_exh\" src=\"" + ROOTPREFIX + "/images/nav_view_exh.gif\" alt=\"Exhibitions\">");
viewLinks[2] = new NavMenuItem("view_rodin", ROOTPREFIX + "/view/rodin.html", "<img id=\"nav_view_rodin\" src=\"" + ROOTPREFIX + "/images/nav_view_rodin.gif\" alt=\"Rodin Collection\">");
viewLinks[3] = new NavMenuItem("view_outd", ROOTPREFIX + "/view/outdoor_sculpture.html", "<img id=\"nav_view_outd\" src=\"" + ROOTPREFIX + "/images/nav_view_outd.gif\" alt=\"Outdoor Sculpture\">");
var menuView = new NavMenu("view", 179, 65, viewLinks);

// Construct participate menu
var partLinks = new Array(5);
partLinks[0] = new NavMenuItem("part_prog", ROOTPREFIX + "/participate/programs_events.html", "<img id=\"nav_part_prog\" src=\"" + ROOTPREFIX + "/images/nav_part_prog.gif\" alt=\"Programs and Events\">");
partLinks[1] = new NavMenuItem("part_host", ROOTPREFIX + "/participate/host_event.html", "<img id=\"nav_part_host\" src=\"" + ROOTPREFIX + "/images/nav_part_host.gif\" alt=\"Host an Event\">");
partLinks[2] = new NavMenuItem("part_guide", ROOTPREFIX + "/participate/guided_visits.html", "<img id=\"nav_part_guide\" src=\"" + ROOTPREFIX + "/images/nav_part_guide.gif\" alt=\"Guided Visits\">");
partLinks[3] = new NavMenuItem("part_class", ROOTPREFIX + "/participate/classes.html", "<img id=\"nav_part_class\" src=\"" + ROOTPREFIX + "/images/nav_part_class.gif\" alt=\"Classes\">");
partLinks[4] = new NavMenuItem("part_stan", ROOTPREFIX + "/participate/stanford_students_faculty.html", "<img id=\"nav_part_stan\" src=\"" + ROOTPREFIX + "/images/nav_part_stan.gif\" alt=\"Stanford Students and Faculty\">");
var menuPart = new NavMenu("part", 179, 65, partLinks);

// Construct explore menu
var expLinks = new Array(3);
expLinks[0] = new NavMenuItem("exp_muse", ROOTPREFIX + "/explore/museum_history.html", "<img id=\"nav_exp_muse\" src=\"" + ROOTPREFIX + "/images/nav_exp_muse.gif\" alt=\"Museum History\">");
expLinks[1] = new NavMenuItem("exp_quest", ROOTPREFIX + "/explore/questions_about_art.html", "<img id=\"nav_exp_quest\" src=\"" + ROOTPREFIX + "/images/nav_exp_quest.gif\" alt=\"Questions About Art\">");
expLinks[2] = new NavMenuItem("exp_cat", ROOTPREFIX + "/explore/catalogues_books.html", "<img id=\"nav_exp_cat\" src=\"" + ROOTPREFIX + "/images/nav_exp_cat.gif\" alt=\"Catalogues and Books\">");
var menuExp = new NavMenu("exp", 179, 65, expLinks);

// Construct join menu
var joinLinks = new Array(5);
joinLinks[0] = new NavMenuItem("join_about", ROOTPREFIX + "/join/about_membership.html", "<img id=\"nav_join_about\" src=\"" + ROOTPREFIX + "/images/nav_join_about.gif\" alt=\"About Membership\">");
joinLinks[1] = new NavMenuItem("join_join", ROOTPREFIX + "/join/join_now.html", "<img id=\"nav_join_join\" src=\"" + ROOTPREFIX + "/images/nav_join_join.gif\" alt=\"Join Now\">");
joinLinks[2] = new NavMenuItem("join_cat", ROOTPREFIX + "/join/categories_benefits.html", "<img id=\"nav_join_cat\" src=\"" + ROOTPREFIX + "/images/nav_join_cat.gif\" alt=\"Categories and Benefits\">");
joinLinks[3] = new NavMenuItem("join_mem", ROOTPREFIX + "/join/member_programs.html", "<img id=\"nav_join_mem\" src=\"" + ROOTPREFIX + "/images/nav_join_mem.gif\" alt=\"Member Programs and Events\">");
joinLinks[4] = new NavMenuItem("join_stan", ROOTPREFIX + "/join/student_membership.html", "<img id=\"nav_join_stan\" src=\"" + ROOTPREFIX + "/images/nav_join_stan.gif\" alt=\"Stanford Student Membership\">");
var menuJoin = new NavMenu("join", 179, 65, joinLinks);

// Construct support menu
var supLinks = new Array(3);
supLinks[0] = new NavMenuItem("sup_gift", ROOTPREFIX + "/support/gift_opportunities.html", "<img id=\"nav_sup_gift\" src=\"" + ROOTPREFIX + "/images/nav_sup_gift.gif\" alt=\"Gift Opportunities\">");
supLinks[1] = new NavMenuItem("sup_corp", ROOTPREFIX + "/support/corporate_sponsorship.html", "<img id=\"nav_sup_corp\" src=\"" + ROOTPREFIX + "/images/nav_sup_corp.gif\" alt=\"Corporate Sponsorship\">");
supLinks[2] = new NavMenuItem("sup_vol", ROOTPREFIX + "/support/volunteering.html", "<img id=\"nav_sup_vol\" src=\"" + ROOTPREFIX + "/images/nav_sup_vol.gif\" alt=\"Volunteer\">");
var menuSup = new NavMenu("sup", 179, 65, supLinks);

// Construct array of menus
var navMenus = new Array(6);
navMenus[0] = menuVisit;
navMenus[1] = menuView;
navMenus[2] = menuPart;
navMenus[3] = menuExp;
navMenus[4] = menuJoin;
navMenus[5] = menuSup;
