var xmlHttp = undefined;
var latest  = "initial";
var timer   = null;

function update(s) {
    if(s != "") {
        window.location.href="#"+s;
    }
    document.getElementById('content').style.visibility="hidden";
    timer=setTimeout('bytext(\'<img src="img/loading.png" alt="Please wait..." />\'',1000);
    document.title = "DunderHumor - Laddar";
    
    if(xmlHttp !== undefined) {
        xmlHttp.abort();
    }
    
    xmlHttp = ajax();
    
    xmlHttp.onreadystatechange=function() {
        if(xmlHttp.readyState==4) {
            bytext(xmlHttp.responseText);
        }
    };
    
    xmlHttp.open("GET","?ajax&"+s,true);
    xmlHttp.send(null);
    
    setTimeout('latest=location.hash;',1);
    return true;
}

function bytext(t) {
    clear_timer();
    a=document.getElementById('content');
    if(a !== null) {
        a.innerHTML=t;
        b=a.getElementsByTagName("script");
        for(c=0;c<b.length;c+=1) {
            eval(b[c].text);
        }
    }
    setTimeout('a.style.visibility="visible"',1);
}

function clear_timer() {
    if(timer != null) {
        clearTimeout(timer);
        timer = null;
    }
}

function ajax() {
    var xmlHttp;
    try {
      xmlHttp=new XMLHttpRequest();
    } catch (e1) { try {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e2) { try {
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e3) {
                return false;
            }
        }
    }
    return xmlHttp;
}

function set_hash(value) {
    if(location.hash == "") {
        location.hash=value;
        latest=location.hash;
    }
}

function check_hash() {
    if(location.hash != latest) {
        if(location.hash == "" || location.hash == "#") {
            //update("");
        } else {
            update(location.hash.slice(1));
        }
    }
    latest=location.hash;
}