// javascript library

// some general settings, browser detection, etc
var MSIE = (document.all) ? 1 : 0;
var HASDOM = (document.all || document.getElementById);
var CURRINFO = 'none';

// some shortcuts
function getElement(id) {
  return document.getElementById(id);
}
function stringtrim(str) {
  return str.replace(/^\s+/,'').replace(/\s+$/,'');
}

// the following functions create, show, and hide balloon tip windows
function makeTip(id, width, code) {
  var str = "<style type='text/css'>";
  str += "  #" + id + "{width: " + width + "px;}";
  str += "</style>";
  str += "<div class='tooltip' id='" + id + "'>" + code + "</div>";
  document.write(str);
}
function getMouseX(event) {
  if (MSIE)
    return event.clientX+document.body.scrollLeft-document.body.clientLeft;
  else return event.pageX;
}
function getMouseY(event) {
  if (MSIE)
    return event.clientY+document.body.scrollTop-document.body.clientTop;
  else return event.pageY;
}
function showTip(id, event) {
  var fwin = getElement(id);
  var wwidth = fwin.clientWidth;
  var wheight = fwin.clientHeight;
  var tip = (MSIE) ? document.all[id].style
                   : document.getElementById(id).style;
  var tleft = getMouseX(event) + document.body.scrollLeft;
  // check if the tooltip would go out of the right window boundary
  if ( (tleft+wwidth) > document.body.clientWidth ) {
    tip.left = document.body.clientWidth - wwidth - 20;
  } else {
    tip.left = tleft;
  }
  var ttop = getMouseY(event) + 20 + document.body.scrollTop;
  if ( (ttop+wheight) > document.body.clientHeight ) {
      tip.top = document.body.clientHeight - wheight - 20;
  } else {
      tip.top = ttop;
  }
  tip.visibility = "visible";
}
function hideTip(id) {
  var tip = (MSIE) ? document.all[id].style
                   : document.getElementById(id).style;
  tip.visibility = "hidden";
}
function makeMenu(page) {
  makeTip('t_home', 200, "Homepage");
  makeTip('t_people', 200, "Faculty, Staff, and Students");
  makeTip('t_projects', 200, "Current Research Projects");
  makeTip('t_software', 200, "Our Software");
  makeTip('t_papers', 200, "Our Publications");
  makeTip('t_internal', 200, "Hatch Member Area");
  if (page=="home") {
    document.write("<div class=atab");
  } else {
    document.write("<div class=tab")
  }
  document.write(" onMouseOver=\"showTip('t_home', event)\"\
    onMouseOut=\"hideTip('t_home')\"><a href=\"index.html\">Home</a></div>");
  if (page=="people") {
    document.write("<div class=atab");
  } else {
    document.write("<div class=tab")
  }
  document.write(" onMouseOver=\"showTip('t_people', event)\"\
    onMouseOut=\"hideTip('t_people')\"><a href=\"people.html\">People</a></div>");
  if (page=="projects") {
    document.write("<div class=atab");
  } else {
    document.write("<div class=tab")
  }
  document.write(" onMouseOver=\"showTip('t_projects', event)\"\
    onMouseOut=\"hideTip('t_projects')\"><a href=\"projects.html\">Projects</a></div>");
  if (page=="software") {
    document.write("<div class=atab");
  } else {
    document.write("<div class=tab")
  }
  document.write(" onMouseOver=\"showTip('t_software', event)\"\
    onMouseOut=\"hideTip('t_software')\"><a href=\"software.html\">Software</a></div>");
  if (page=="papers") {
    document.write("<div class=atab");
  } else {
    document.write("<div class=tab")
  }
  document.write(" onMouseOver=\"showTip('t_papers', event)\"\
    onMouseOut=\"hideTip('t_papers')\"><a href=\"papers.html\">Publications</a></div>");
  if (page=="internal") {
    document.write("<div class=atab");
  } else {
    document.write("<div class=tab")
  }
  document.write(" onMouseOver=\"showTip('t_internal', event)\"\
    onMouseOut=\"hideTip('t_internal')\"><a href=\"internal.html\">Internal</a></div>");
}

// the following function deals with image mouseovers
function hlimg(id) {
  var img = getElement("i_"+id);
  var desc = getElement("d_"+id);
  img.className = "photo1";
  img.style.cursor = "hand";
  desc.className = "desc1";
  window.status = "click to show personal information";
}
function unhlimg(id) {
  var img = getElement("i_"+id);
  var desc = getElement("d_"+id);
  img.className = "photo";
  img.style.cursor = "default";
  desc.className = "desc";
  window.status = "";
}

// the following functions deal with personal information
function makeInfo(id, width, height, code) {
  var str = "<style type='text/css'>";
  str += "  #" + id + "{width: " + width + "px; height: " + height + "px; }\n";
  str += "  #s" + id + "{width: " + width + "px; height: " + height + "px; }";
  str += "</style>";
  str += "<div class='info' onClick='hideInfo(this)' id='" + id + "'>";
  str += "(click on the photo again or this window to close it)<br><br>";
  str += code + "</div>";
  str += "<div class='shadow' id='s" + id + "'></div>";
  document.write(str);
}
function showInfo(id, event) {

  //if there is an info already showing, hide it first
  if(CURRINFO != 'none' && CURRINFO != id){
      hideInfo(getElement(CURRINFO));
  }
  CURRINFO = id;

  var t = (MSIE) ? document.all[id]
                   : document.getElementById(id);
  var twidth = t.clientWidth;
  var theight = t.clientHeight;
  var tip = t.style;
  var sid = "s" + id;
  var sha = (MSIE) ? document.all[sid].style
                   : document.getElementById(sid).style;
  if (tip.visibility=="visible") {
    tip.visibility = "hidden";
    sha.visibility = "hidden";
    return;
  }
  var tleft = getMouseX(event) - document.body.scrollLeft;
  // check if the tooltip would go out of the right window boundary
  if ( (tleft+twidth) > document.body.clientWidth ) {
    tip.left = document.body.clientWidth - twidth - 20 + document.body.scrollLeft;
    sha.left = document.body.clientWidth - twidth - 15 + document.body.scrollLeft;
  } else {
    tip.left = tleft + document.body.scrollLeft;
    sha.left = tleft+5+ document.body.scrollLeft;
  }
  // check if the tooltip would go out of the bottom window boundary
  var ttop = getMouseY(event) - document.body.scrollTop;
  if ( (ttop+theight) > document.body.clientHeight ) {
      tip.top = document.body.clientHeight - theight - 20 + document.body.scrollTop;
      sha.top = document.body.clientHeight - theight - 15 + document.body.scrollTop;
  } else {
      tip.top = ttop + document.body.scrollTop;
      sha.top = ttop + document.body.scrollTop + 5;
  }
  tip.visibility = "visible";
  sha.visibility = "visible";
}
function hideInfo(e) {
  e.style.visibility = "hidden";
  var sid = "s" + e.id;
  var sha = (MSIE) ? document.all[sid].style
                   : document.getElementById(sid).style;
  sha.visibility = "hidden";
}

// goto URL
function Goto(id) {
  var s = getElement(id);
  var opts = s.selectedIndex;
  var sel = s.options[opts].value;
  if (sel=="") return;
  var url = sel;
  window.open(url, 'hatch_internal');
  //location.replace(url);
}

function printBanner() {
    document.write("\
<div class=banner>Hatch MR Research Center</div>\
Department of Radiology, Columbia University<br><br>\
710 West 168th street, New York, NY 10032");
}

function CheckForEmpty(id, msg) {
    var e = document.getElementById(id);
    if (e.value=="") {
        window.alert("please input " + msg);
        e.focus();
        return true;
    }
    return false;
}

function CheckInput() {
    if (CheckForEmpty("i_username", "your name")) return false;
    if (CheckForEmpty("i_title", "your title")) return false;
    if (CheckForEmpty("i_org", "your affiliation")) return false;
    var e = document.getElementById('i_email');
    if (e.value.indexOf("@")==-1) {
      window.alert("please input a valid email address");
      e.focus();
      return false;
     }
     return true;
}