var bookmarkedSection, querySection, initSection;

 	function resetSections()
		{
		  var sections, i, len, section;
		  sections = YAHOO.util.Dom.getElementsByClassName("section", "div", "content");
			for (i = 0, len = sections.length; i<len; i++)
			{
			 		section = sections[i];
					YAHOO.util.Dom.setStyle(section, "display", "none");
			} 
		}

function loadSection(section) {
			 resetSections();
			 if (section)
			 {
			 		YAHOO.util.Dom.setStyle(section, "display", "block");
					if (section === "gallery")
					{
					 	 if (!galleryInitialized)
						 {
						    initializeGallery();
						 }
					}
					else if (section === "location"){
						 if (!mapInitialized)
						 {
						 		initializeMap();
						 }		
					} 
			 }
			 else
			 {
			 		YAHOO.util.Dom.setStyle("home", "display", "block");
			 }
    }

function addMouseHoverEffectToNav(element)
		{
		 		YAHOO.util.Event.addListener(element, "mouseover", function(event){YAHOO.util.Dom.addClass(element, "active");});
				YAHOO.util.Event.addListener(element, "mouseout", function(event){YAHOO.util.Dom.removeClass(element, "active");});
				YAHOO.util.Event.addListener(element, "click", function (evt) {
								  var srcElement = evt.srcElement ? evt.srcElement : evt.target;
					    		  var anchors = YAHOO.util.Dom.getElementsByClassName("navigationReference", "a", srcElement);
									if (anchors && anchors.length > 0){
									 	var anchorElement = anchors[0];
										var href = anchorElement.href;
                    var section = YAHOO.util.History.getQueryStringParameter("section", href) || "home";
     								try {
                        YAHOO.util.History.navigate("navbar", section);
                    } catch (e) {
                        loadSection(section);
                    }
                    YAHOO.util.Event.preventDefault(evt);
									}
            		});  
		}
		
function initializeNavigationBar() {
        // Process links
				
        var anchors, i, len, anchorElement, href, section, currentSection;				
        anchors = YAHOO.util.Dom.getElementsByClassName("navigationReference", "a", "page");
				for (i = 0, len = anchors.length; i < len; i++) {
            anchorElement = anchors[i];
						var parent = anchorElement.parentNode;
						if (parent.tagName === 'LI')
						{
						 	  addMouseHoverEffectToNav(parent);				
						}

						YAHOO.util.Event.addListener(parent, "click", function (evt) {
						    var href = this.children[0].getAttribute("href");
						    section = YAHOO.util.History.getQueryStringParameter("section", href) || "home";
						    // If the Browser History Manager was not successfuly initialized,
						    // the following call to YAHOO.util.History.navigate will throw an
						    // exception. We need to catch it and update the UI. The only
						    // problem is that this new state will not be added to the browser
						    // history.
						    //
						    // Another solution is to make sure this is an A-grade browser.
						    // In that case, under normal circumstances, no exception should
						    // be thrown here.
						    try {
						        YAHOO.util.History.navigate("navbar", section);
						    } catch (e) {
						        loadSection(section);
						    }
						    YAHOO.util.Event.preventDefault(evt);
						});
        }

        // This is the tricky part... The window's onload handler is called when the
        // user comes back to your page using the back button. In this case, the
        // actual section that needs to be loaded corresponds to the last section
        // visited before leaving the page, and not the initial section. This can
        // be retrieved using getCurrentState:
        currentSection = YAHOO.util.History.getCurrentState("navbar");
        loadSection(currentSection);
    }

    // The initial section will be chosen in the following order:
    //
    // URL fragment identifier (it will be there if the user previously
    // bookmarked the application in a specific state)
    //
    //         or
    //
    // "section" URL parameter (it will be there if the user accessed
    // the site from a search engine result, or did not have scripting
    // enabled when the application was bookmarked in a specific state)
    //
    //         or
    //
    // "home" (default)

    bookmarkedSection = YAHOO.util.History.getBookmarkedState("navbar");
    querySection = YAHOO.util.History.getQueryStringParameter("section");
    initSection = bookmarkedSection || querySection || "home";

    // Register our only module. Module registration MUST take place
    // BEFORE calling initializing the browser history management library!
    YAHOO.util.History.register("navbar", initSection, function (state) {
        // This is called after calling YAHOO.util.History.navigate,
        // or after the user has trigerred the back/forward button.
        // We cannot distinguish between these two situations.
        loadSection(state);
    });

    // Use the Browser History Manager onReady method to initialize the application.
    YAHOO.util.History.onReady(function () {
        initializeNavigationBar();
    });

    // Initialize the browser history management library.
    try {
        YAHOO.util.History.initialize("yui-history-field", "yui-history-iframe");
    } catch (e) {
        // The only exception that gets thrown here is when the browser is
        // not supported (Opera, or not A-grade) Degrade gracefully.
        // Note that we have two options here to degrade gracefully:
        //   1) Call initializeNavigationBar. The page will use Ajax/DHTML,
        //      but the back/forward buttons will not work.
        //   2) Initialize our module. The page will not use Ajax/DHTML,
        //      but the back/forward buttons will work. This is what we
        //      chose to do here:
        loadSection(initSection);
    }

