var
    XHR_GET = 'GET',
    XHR_POST = 'POST';

function XHR(type, request, postdata, success_cb, failure_cb, finish_cb)
/*:
    Str
    *  XHR
    *  Str + Null
    *  (XHR -> Undef) + Undef
    *  (XHR -> Undef) + Undef
    *  (XHR -> Undef) + Undef
    -> Undef                                                                                                                                                                                                                                                      
*/
{

    var req = new XMLHttpRequest(),

	call_if_present = function(f)
	/*: (XHR -> Undef) + Undef -> (XHR -> Undef)*/
    {
	return function(req)
	/*: XHR -> Undef */
	{
	    if(typeof f === 'function') {
		f(req);
	    }
	};
    }

    success = call_if_present(success_cb),
	failure = call_if_present(failure_cb),
	finish = call_if_present(finish_cb);

    if(type !== XHR_GET && type !== XHR_POST) {
        throw new Error("Nonsensical XHR request type: " + type);
    }

    req.open(type, request, true);

    req.onreadystatechange = function (e) {
        if(req.readyState === 4) {
            if(req.status === 200) {
                success(req);
            }
            else {
                console.log(req.status + "was the status");
                failure(req);
            }
            finish(req);
        }
    }

    if(type === XHR_POST) {
        req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    }

    req.send(postdata);

}


function initialize() {
    
    instrument_tabs();

    if(!window.onhashchange) {
    	window.onhashchange = 
	    function() {
	    perform_switch(gup("t"));
	}
    }

    

    var tab = gup("t"),
	top_tab;

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

function instrument_tabs() {

    var els = get_tabs();

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

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

function update(id) {
    update_url(id);
    perform_switch(id);
}

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];
}
