// JavaScript Document
(function () {

    var html, bookmarkedSection, querySection, initSection;
	 var home = "2";
    // This function does an XHR call to load and
    // display the specified section in the page.
    function loadSection(section) {
       
		var url = "pagina.php?id_pagina=" + section ;
	    document.getElementById("contenuto_principale").innerHTML = '<div align="center"><img src="images/loading.gif"></div>';
        function successHandler(obj) {
            // Use the response...
            YAHOO.util.Dom.get("contenuto_principale").innerHTML = obj.responseText;
			EvalJS("contenuto_principale");
        }

        function failureHandler(obj) {
            // Fallback...
            location.href = "?section=" + section;
        }

        YAHOO.util.Connect.asyncRequest("GET", url,
            {
                success: successHandler,
                failure: failureHandler
            }
        );
    }

    function initializeNavigationBar() {
        // Process links
        var anchors, i, len, anchor, href, section, currentSection;
        anchors = YAHOO.util.Dom.get("voci_menu").getElementsByTagName("a");
        for (i = 0, len = anchors.length; i < len; i++) {
            anchor = anchors[i];
		
            YAHOO.util.Event.addListener(anchor, "click", function (evt) {
                href = this.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);
    }

})();
