YUI({combine: true, timeout: 10000}).use('io', 'history', function(Y) {

    var html, elem, bookmarkedSection, querySection, initSection, navbar;

    // This function does an XHR call to load and display the specified section.
    function loadSection(section) {
		goTour(section);
    }

    function initializeNavigationBar() {
    	containerIdArray = Array;
    	containerIdArray[0] = 'nav';
    	containerIdArray[1] = 'navc';
    	
    	for(var i=0; i<=containerIdArray.length; i++)
    	{
	    	Y.on('click', function (evt) {
	            var el = evt.target;
	            while (el.get('id') !== containerIdArray[i]) {
	                if (el.get('nodeName').toUpperCase() === 'A') {
	                    evt.preventDefault();
	                    section = Y.History.getQueryStringParameter('tour', el.get('href')) || '1';
	                    if (!Y.History.navigate('navbar', section)) {
	                        // Fallback...
	                        loadSection(section);
	                    }
	                    break;
	                } else {
	                    el = el.get('parentNode');
	                }
	            }
	        }, '#'+containerIdArray[i]);
    	}

        currentSection = Y.History.getCurrentState('navbar');
        loadSection(currentSection);
    }

    bookmarkedSection = Y.History.getBookmarkedState('navbar');
    querySection = Y.History.getQueryStringParameter('tour');
    initSection = bookmarkedSection || querySection || '1';

    Y.History.register('navbar', initSection).subscribe('history:moduleStateChange', loadSection);
    Y.History.subscribe('history:ready', initializeNavigationBar);

    if (!Y.History.initialize('#yui-history-field', '#yui-history-iframe')) {
        // Fallback...
        loadSection(initSection);
    }
    
});
