var sec;
var url;
var newbody;
var xmlhttp;
var timerId = 0;
var counter = 0;

// Provide the XMLHttpRequest class for IE 5.x-6.x:
if( typeof(XMLHttpRequest) == "undefined" )
  XMLHttpRequest = function()
  {
    try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch(e){}
    try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch(e){}
    try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e){}
    try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e){}
    throw new Error( "This browser does not support XMLHttpRequest." );
  };

function blend() {
  with (document.getElementById("old_body")) {
    /* for browsers that support opacity
    * (a.k.a. everything except IE) */
    style.opacity -= .1;
    style.filter = "alpha(opacity="+ style.opacity*100+ ")";
    if (style.opacity > 0.1) {
      timerId = window.setTimeout("blend();", 200);
    }
    else {
      /* clean up */
      window.clearTimeout(timerId);
      style.visibility = "hidden";
      document.body.removeChild(document.getElementById("old_body"));
      window.setTimeout("getPage();", sec * 1000);
    }
  }
}

function trans(){
  document.body.innerHTML =
    '<div id="old_body" style="position: absolute; opacity: 1;">'
    + document.body.innerHTML + '</div>';

  newbody = document.body.appendChild(document.createElement("div"));
  newbody.innerHTML = xmlhttp.responseText.replace(/.*<body[^>]*>(.*)<\/body>.*/, "$1")
  /* getting meta refresh data from RE is a worse idea than from DOM */
  /* it's the only way works because the DOM way doesn't work for IE */
  var a = xmlhttp.responseText.match(
      /<meta.*http-equiv="refresh" content="([0-9]+);url=([^"]*)"/i);
  sec = a[1];
  url = a[2];

  blend();
}

function xmlhttpChange()
{
  // if xmlhttp shows "loaded"
  if (xmlhttp.readyState == 4){
    // if "OK"
    if (xmlhttp.status == 200){
      if ( !timerId ) window.clearTimeout(timerId);
      timerId = window.setTimeout("trans();", 100);
    }
    else alert("Error:\t" + xmlhttp.status + "\nURI:\t" + url);
  }
}

function getPage()
{
  document.title = ++counter;

  if (XMLHttpRequest){
    xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = xmlhttpChange;
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
  }
}

window.onload = function()
{
  url = "?" + window.location.search.substring(1);
  getPage();
};
