// creates an XMLHttpRequest for use in AJAX calls.
function createRequest () {
  // try to instantiate and return a new XHR.
  try { return new XMLHttpRequest (); } catch(e) {}
  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) {}

  // no such support, return null.
  return null;
}

function handleResponse (request, suffix) {
  if (request.readyState == 4 && request.status == 200) {
    var resp = document.getElementById ("tipText:" + suffix);
    resp.innerHTML = request.responseText;
  }
}
 
function sendRequest (project, suffix) {
  var request = createRequest ();
  if (request) {
    request.open ("GET", "../proj/tip.php?p=" + project + "&f=" + suffix, true);
    request.onreadystatechange = function () { handleResponse (request, suffix); };
    request.send (null);
  }
}

function showBubble (project, suffix) {
  var box = document.getElementById ("tipBox:" + suffix);
  box.style.display = "";
  sendRequest (project, suffix);
}

function hideBubble (project, suffix) {
  var box = document.getElementById ("tipBox:" + suffix);
  box.style.display = "none";
}

// links up all javascript functionality to the DOM.
window.onload = function () {
  var divs = new Array ();
  divs = document.getElementsByTagName ("div");
  for (var i = 0; i < divs.length; i++) {
    var divid = divs[i].id;
    var divids = divid.split (":");
    if (divids[0] == "columnBox" || divids[0] == "menuBox") {
      divs[i].onmouseover = function () {
        this.style.background = "#e5ecf9";
      };
      divs[i].onmouseout = function () {
        this.style.background = "#ffffff";
      };
    }
  }
  for (var i = 0; i < document.anchors.length; i++) {
    var aname = document.anchors[i].name;
    var anames = aname.split (":");
    if (anames[0] == "link") {
      document.anchors[i].onmouseover = function (project, suffix) {
        return function () { showBubble (project, suffix); }
      }(anames[1], anames[2]);
      document.anchors[i].onmouseout = function (project, suffix) {
        return function () { hideBubble (project, suffix); }
      }(anames[1], anames[2]);
    }
  }
};

