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) {
		if(section == 0){
			return;
		}
    	openPopupImagePreview(section);
    }

    function initializeNavigationBar() {
    	
    	containerIdArray = Array;
    	containerIdArray[0] = 'imageHolder';
    	containerIdArray[1] = 'nav';
    	
    	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('site', el.get('href')) || '1';
	    				if (!Y.History.navigate('site', section)) {
	    					// Fallback...
	    					loadSection(section);
	    				}
	    				break;
	    			} else {
	    				el = el.get('parentNode');
	    			}
	    		}
	    	}, '#'+containerIdArray[i]);
    	}

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

    bookmarkedSection = Y.History.getBookmarkedState('site');
    querySection = Y.History.getQueryStringParameter('site');
    initSection = bookmarkedSection || querySection || '0';
    

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

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

