function initialize() {
    
    instrument_tabs();

    var tab = gup("t");

    if(document.getElementById(tab) && document.getElementById(tab).getAttribute("class") == "tab") { 
	perform_switch(tab);
    }
    else {
	update_url(get_tabs()[0].getAttribute("id"));
    }
	
}

function instrument_tabs() {

    var els = get_tabs();

    for(i=0;i<els.length;i++) {
	els[i].setAttribute("onclick", "update_url(this.id);");
    }

}

function swap(id) {
    var main = document.getElementById("main-content");
    var swap = document.getElementById(id);
    main.innerHTML = swap.innerHTML;
}

function update_url(name) {
    var window_base = get_window_base();
    window.location.href = window_base + "?t=" + name;
}

function get_window_base() {
    return window.location.href.substring(0,window.location.href.indexOf("?"));
}

function perform_switch(name) {
    deactivate_tabs();

    var el = document.getElementById(name);
    el.setAttribute("class", "active_tab");

    swap(el.getAttribute("target"));
}

function deactivate_tabs() {
    var els = get_active_tabs();
    if(els.length != 0)
	els[0].setAttribute("class", "tab");
}

function get_tabs() {
    return document.getElementsByClassName("tab");    
}

function get_active_tabs() {
    return document.getElementsByClassName("active_tab");
}

function gup( name )
{
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    if( results == null )
	return "";
    else
	return results[1];
}