YAHOO.util.Event.onDOMReady( function() { 
 	var html, bookmarkedSection, querySection, initSection;

 	/* hack posizione centrata in verticale */
 	if (YAHOO.util.Dom.getY('madre') < 0)
	 	YAHOO.util.Dom.setY('madre', 10);
	 
	 YAHOO.util.Dom.setStyle('madre', 'visibility', 'visible');
	
 	window.onresize = function() {
	 	if (YAHOO.util.Dom.getX('madre') < 0) {
 	 		YAHOO.util.Dom.setX('madre', 10);
 		}
 		
 		if (YAHOO.util.Dom.getY('madre') < 0) {
 	 		YAHOO.util.Dom.setY('madre', 10);
 		}
 		
 		if ( window.innerWidth > 1008 ) {
	 		YAHOO.util.Dom.setStyle('madre', 'left', '50%');
 	 		YAHOO.util.Dom.setStyle('madre', 'marginLeft', '-500px');
 		}
	 	
	 	if ( window.innerHeight > 654 ) {
	 		YAHOO.util.Dom.setStyle('madre', 'top', '50%');
 	 		YAHOO.util.Dom.setStyle('madre', 'marginTop', '-333px');
 		}
  	};
 	

    // This function does an XHR call to load and
    // display the specified section in the page.
    function loadSection(section) {
        var url = section;
		
        if ( strpos(url, '?', 0) !== false)
			url = url + '&';
		else
			url = url + '?';
		url += 'jt_nolayout';
		

        function successHandler(obj) {
            // Use the response...
            YAHOO.util.Dom.get('content').innerHTML = obj.responseText;
            processLinks();
            
            if ( YAHOO.util.Dom.get('login') ) {
		 		new YAHOO.util.KeyListener('login', {keys: 13}, function() {
			 		makeRequest();
		 		}).enable();
	 		}
	 		
// 	 		if ( YAHOO.util.Dom.get('recover-password') ) {
// 		 		new YAHOO.util.KeyListener('gui-form-input-email', {keys: 13}, function() {
// 			 		makeRequest();
// 		 		}).enable();
// 	 		}
            
		 	if ( YAHOO.util.Dom.get('registrazione') ) {
		 		var key = new YAHOO.util.KeyListener('registrazione', {keys: 13}, function() {
			 		makeRequest();
		 		});
		 		
		 		key.enable();
		 		YAHOO.util.Event.addListener('gui-form-input-comments', 'focus', function() {
			 		key.disable();
		 		});
		 		
		 		YAHOO.util.Event.addListener('gui-form-input-comments', 'blur', function() {
			 		key.enable();
		 		});
	 		}
        }

        function failureHandler(obj) {
            // Fallback...
             location.href = url;
        }

        YAHOO.util.Connect.asyncRequest("GET", url,
            {
                success: successHandler,
                failure: failureHandler
            }
        );
    }
    
    function processLinks() {	    
	    var anchors, i, len, anchor, href, section, currentSection;
        anchors = document.getElementsByTagName('area');//YAHOO.util.Dom.get('content').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 = href || "splash.htm";
                // 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);
            });
        }
        
        anchors = document.getElementsByTagName('a');//YAHOO.util.Dom.get('content').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 = href || "splash.htm";
                // 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);
            });
        }
    }

    function initializeNavigationBar() {
        // Process links
        processLinks();
		
        // 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 || "";
    
//     alert(initSection );

    // 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);
    }
    
    function strpos(haystack, needle, offset) {
    	var i = (haystack+'').indexOf( needle, offset ); 
    	return i===-1 ? false : i;
	}
});


function splash(lan) {
	section = 'home-' + lan + '.htm';
    try {
        YAHOO.util.History.navigate("navbar", section);
    } catch (e) {
        loadSection(section);
    }
//     YAHOO.util.Event.preventDefault();
}

function startSound() {
	if ( navigator.appName.indexOf('Microsoft') != false )
	 	document['swf-sound'].callStartSound();
	else
	 	window['swf-sound'].callStartSound();
}

function skipIntroHide() {
	YAHOO.util.Dom.setStyle('skip-btn', 'visibility', 'hidden');
}
