// Defines JavaScript helper functions.

// Invoked on each page. Replaces certain elements in the page with 
// links to appropriate things. The function has been adapted from:
// http://lojjic.net/blog/20030828-142754, which concerns email obfuscation.

function linkStuff() 
{
  if(!document.getElementsByTagName) return;
  var allElts = document.getElementsByTagName("*");
  if(allElts.length == 0 && document.all) 
    allElts = document.all; //hack for IE5
  for(var i=0; i<allElts.length; i++) {
    var elt = allElts[i];
    var className = elt.className || elt.getAttribute("class") 
      || elt.getAttribute("className");
    if(className && className.match(/\be\b/) // email
        && elt.firstChild.nodeType == 3) {
      var e = elt.firstChild.nodeValue;
      e = e.replace(/[ \[\{\(\|\/\\]at[ \]\}\)\|\/\\]/i, "@")
        .replace(/[ \[\{\(\|\/\\](dot|period)[ \]\}\)\|\/\\]/gi, ".");
      var lnk = document.createElement("a");
      lnk.setAttribute("href","mailto:"+e);
      var img = document.createElement("img");
      img.setAttribute("src", "/common/text_to_image/" + e + "/");
      img.setAttribute("border", "0");
      img.setAttribute("alt", "Email Address");
      lnk.appendChild(img);
      elt.replaceChild(lnk, elt.firstChild);
    }
    else if(className && className.match(/\bc\b/) // course
        && elt.firstChild.nodeType == 3) {
      var c = elt.firstChild.nodeValue;
      var lnk = document.createElement("a");
      lnk.setAttribute("href","/www/academic/courses/"+c+"/");
      lnk.appendChild(document.createTextNode(c));
      elt.replaceChild(lnk, elt.firstChild);
    }
    else if(className && className.match(/\bp\b/) // person
        && elt.firstChild.nodeType == 3) {
      var p = elt.firstChild.nodeValue;
      new_p = p.replace(/^([^ ]+) +([^ ]+)/, "$1_$2");
      var lnk = document.createElement("a");
      lnk.setAttribute("href","/www/people/"+new_p+"/");
      lnk.appendChild(document.createTextNode(p));
      elt.replaceChild(lnk, elt.firstChild);
    }
    else if(className && className.match(/\bo\b/) // third floor room
        && elt.firstChild.nodeType == 3) {
      var o = elt.firstChild.nodeValue;
      var lnk = document.createElement("a");
      lnk.setAttribute("href","/www/third_floor_map/"+o+"/");
      lnk.appendChild(document.createTextNode(o));
      elt.replaceChild(lnk, elt.firstChild);
    }
  }
}

// Handler for an option in the pull-down menu of academic terms. Takes the 
// user to the (relative) page specified by the value attribute of the option 
// element.

function jumpToTerm() 
{ 
    var destination = document.terms.term.value; 
    var version = navigator.appVersion; 
    if (version.indexOf("MSIE") >= -1) { 
        window.location.href = destination;
    }
    else {
        window.open(destination, target = "_self");
    }
}

// Handler for an option in the pull-down menu of academic years. Takes the 
// user to the (relative) page specified by the value attribute of the option 
// element.

function jumpToYear() 
{ 
    var destination = document.years.year.value; 
    var version = navigator.appVersion; 
    if (version.indexOf("MSIE") >= -1) { 
        window.location.href = destination;
    }
    else {
        window.open(destination, target = "_self");
    }
}

function showToolTipp(text, x, y)
{
  var obj = document.getElementById('bubble_tooltip');
  var obj2 = document.getElementById('bubble_tooltip_content');
  obj2.innerHTML = text;
  obj.style.display = 'block';
  var st = 
    Math.max(document.body.scrollTop,document.documentElement.scrollTop);
  if(navigator.userAgent.toLowerCase().indexOf('safari')>=0)st=0; 
  obj.style.left = x - 100 + 'px';
  obj.style.top = y - obj.offsetHeight + st + 10 + 'px';
}	


function showToolTip(e,text)
{
  if(document.all)e = event;
  var obj = document.getElementById('bubble_tooltip');
  var obj2 = document.getElementById('bubble_tooltip_content');
  obj2.innerHTML = text;
  obj.style.display = 'block';
  var st = 
    Math.max(document.body.scrollTop,document.documentElement.scrollTop);
  if(navigator.userAgent.toLowerCase().indexOf('safari')>=0)st=0; 
   var leftPos = e.clientX - 100;
  if(leftPos<0)leftPos = 0;
  obj.style.left = leftPos + 'px';
  obj.style.top = e.clientY - obj.offsetHeight - 1 + st + 'px';
}	

function hideToolTip()
{
  document.getElementById('bubble_tooltip').style.display = 'none';
}

function mailTo(car, cdr, subject)
{
  location.href = 'mailto' + ':' + car + "@" + cdr + '?subject=' + subject;
}