function ahah(url, target, delay, prependHTML) {
//  document.getElementById(target).innerHTML = 'Laden...';
  if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (req != undefined) {
    req.onreadystatechange = function() {ahahDone(url, target, delay, prependHTML);};
    req.open("GET", url, true);
    req.send("");
  }
}  

function ahahDone(url, target, delay, prependHTML) {
  if (req.readyState == 4) { // only if req is "loaded"
    if(prependHTML == undefined)
      prependHTML = '';
    if (req.status == 200) { // only if "OK"
      document.getElementById(target).innerHTML = prependHTML+req.responseText;
      activateAJAXLinks();
      fixPosBottom('linksunten', 0);
      fixPosBottom('copyright', 2);
      Title = req.responseText.match(/<h.>[A-Za-z]+ [0-9]+/);
      if(Title && Title[0])
      {
        document.title = 'The Power Dinosaurs // '+Title[0].slice(4);
      }
      else
        document.title = 'The Power Dinosaurs';
    } else {
      if(req.status)
        document.getElementById(target).innerHTML="Fehler! Code "+req.status+"<br>\nMeldung vom Server: "+req.statusText;
      else //Probably strange implementation of XMLHTTPRequest. Try HTML version.
        window.location.href='comic,'+window.location.hash.slice(1);
    }
    if (delay != undefined) {
       setTimeout("ahah(url,target,delay)", delay); // resubmit after delay
	    //server should ALSO delay before responding
    }
  }
}

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

function removeElement(id)
{
    obj = document.getElementById(id);
    obj.parentNode.removeChild(obj);
}

function fixPosBottom(nodeID, bottomVal)
{
    //Opera and Firefox 2 don't need this fix
    if(navigator.appName.match(/Opera/) || navigator.userAgent.match(/Firefox\/[^1]/)) return true;
    node = document.getElementById(nodeID);
    if(node.nodeType != 1) return false;
    node.style.bottom = 'auto';
    return node.style.top = (node.parentNode.clientHeight-node.clientHeight-bottomVal)+'px';
}

var WatchTimer;

function checkChanged(currentID)
{
    comicNum = parseInt(window.location.hash.slice(1));

    if(currentID>0 && comicNum!=currentID)
    {
        window.clearInterval(WatchTimer);
        ahah("comicDATA,"+comicNum, "comic");
        WatchTimer = window.setInterval('checkChanged('+comicNum+')', 400);
    }
}

function activateAJAXLinks()
{
    if (document.getElementsByTagName)
    {
        var links = document.getElementById("comic").getElementsByTagName("a");
        for (var i=0; i < links.length; i++)
        {
            //.replace() is a quick fix for crappy Microsoft 'browsers' that lie about the
            //contents of the href attribute (and implicitly convert it to an absolute URL).
            links[i].onclick = function() {
                //Avoid stuff like 'comic,42#15'
                var comicNum = parseInt(this.getAttribute("href").replace(/^http:.*\//, "").replace(/^.*,/, ""));
                if(window.location.pathname.match(/comic,/))
                {
                    window.location.href='comic#'+comicNum;
                }
                else
                {
                    window.clearInterval(WatchTimer);
                    ahah("comicDATA,"+comicNum, "comic");
                    window.location.hash = comicNum;
                    WatchTimer = window.setInterval('checkChanged('+comicNum+')', 400);
                }
                return false;
            }
        }
    }
}

function loadComic()
{
    if(window.location.hash)
    {
        document.getElementById('comic').innerHTML = 'Comic wird geladen...';
        comicNum = parseInt(window.location.hash.replace(/#/, ''));
        ahah("comicDATA,"+comicNum, 'comic');
        WatchTimer = window.setInterval('checkChanged('+comicNum+')', 400);
    }
    else
    {
        activateAJAXLinks()
    }
}

if(window.location.pathname.match(/comic/))
    addLoadEvent(loadComic);
