
var cur_id = null;
var window_title = '';

/* STATE CHANGE HANDLER */

function updatePage(new_id) {
    // update navigation and content
    updateNavigation(new_id);
    updateContent(new_id);
    // update window title
    document.title = window_title + ((new_id == 's0p0') ? '' : ' :: ' + I(new_id).firstChild.nodeValue);
    // update current id variable
    cur_id = new_id;
}

/* NAVIGATION */

function init_navigation() {
    var nav_items = I('navroot').childNodes;
    for (var x = 0; x < nav_items.length; x++)
        if (nav_items[x].nodeName.toLowerCase() == 'li') {
        nav_items[x].onmouseover = function() { this.className = (this.className == '' ? 'hover' : this.className == 'open' ? 'openhover' : this.className == 'sub' ? 'subhover' : this.className); };
        nav_items[x].onmouseout = function() { this.className = (this.className == 'hover' ? '' : this.className == 'openhover' ? 'open' : this.className == 'subhover' ? 'sub' : this.className); };
        nav_items[x].onclick = function() { if (this.id != cur_id) YAHOO.util.History.navigate('page', this.id); };
    }
}
function updateNavigation(new_id) {
    var i = new_id.toString().indexOf('p', 1);
    var new_section = new_id.toString().substring(1, i);

    // go through all navigation items
    var nav_items = I('navroot').childNodes;
    for (var x = 0; x < nav_items.length; x++) {
        var ni = nav_items[x];
        if (ni.id.indexOf('s' + new_section + 'p') == 0) {    // section related pages
            ni.style.visibility = 'visible';
            ni.style.display = 'block';
            // set class name
            if (ni.id == 's' + new_section + 'p0')
                ni.className = (ni.id == new_id ? 'selected' : 'open');
            else
                ni.className = (ni.id == new_id ? 'subselected' : 'sub');
        } else {
            // hide all other sub pages
            if (ni.id.indexOf('p0') == -1) {
                ni.style.visibility = 'hidden';
                ni.style.display = 'none';
            }
            // reset class name
            ni.className = (ni.className == 'selected' || ni.className == 'open' ? '' : ni.className == 'subselected' ? 'sub' : ni.className);
        }
    }
}

/* CONTENT */

function init_content() {
    // remember base window title
    window_title = document.title;

    // initate YUI History
    var initial_state = YAHOO.util.History.getBookmarkedState('page') || 's0p0';
    YAHOO.util.History.register('page', initial_state, function(new_state) { updatePage(new_state); });
    YAHOO.util.History.onReady(function() { updatePage(YAHOO.util.History.getCurrentState('page')); });
    YAHOO.util.History.initialize('yui-history-field', 'yui-history-iframe');
}
function updateContent(new_id) {
    // hide current content
    if (cur_id) { I(cur_id + 'content').style.visibility = 'hidden'; I(cur_id + 'content').style.display = 'none'; }
    // show new content
    I(new_id + 'content').style.visibility = 'visible'; I(new_id + 'content').style.display = 'block';
}

