/**
 *  file: ARRCmap.js
 *  created: mvellines - 2007/06/07
 *  updated: 
 *  purpose: separate behavioral enhancements from structural markup
 *
 *  Adds event handler to anchor which points to detailed ARRC map image
 *  in order to display this map in a popup window without using HTML attributes.
 */


// Ensures all prior page-load handlers run plus the specifed one.
function addLoadEvent(fn) {
  var old = window.onload;
  if(typeof window.onload != 'function') {
    window.onload = fn;
  } else {
    window.onload = function() {
      old();
      fn();
    }
  }
}


// Add event handlers for the course description links.
function wireDescPopups(e) {
  var links = document.getElementsByTagName('a');
  for(var i = 0; i < links.length; i++) {
  
    // Skip other anchors.
    if(!links[i].href || links[i].href.lastIndexOf('cc_level2.gif') < 0) { continue; }
    
    // Add event handler and target property to this anchor.
    links[i].onclick = showBigMap;
    links[i].target = "ARRCmap";
  }
  return true;
}


// Retrieve the href value of the anchor element whence the event occurred.
// This function will recursively climb the element hierarchy until the anchor
// element's href value can be read or the document element is reached. It is
// needed because child elements can reside inside the anchor element which may
// be the source of the click event.
// WTF: Testing for elem.href in IE 6&7 incorrectly evals true but returns the
//      src value of the <img>.
function getHrefForIE(elem) {
// alert('NodeType='+elem.nodeType+"\nNodeName="+elem.nodeName+"\nhref="+elem.href+"\nsrc="+elem.src);
  if(elem.nodeType == 9) { return null; } // failed: reached top of document.
  else if(elem.nodeType == 1 && elem.nodeName == 'A') { return elem.href; } // Found it!
  else { return getHrefForIE(elem.parentNode); }
}


// Click-handler for map link.
function showBigMap(e) {
  if(!e) e = window.event; // IE only
  var atts = 'scrollbars=yes,menubar=yes,resizable=yes';
  var wWidth = 620;
  var wHeight = 320;
  var wX = 35; //default X-coord for new window
  var wY = 35; //default Y-coord for new window
  var url; // = '/maps/pics/cc_level2.gif';
  if (e.currentTarget) {
    // Safari and Konqueror sometimes reference text and cdata node instead of <a>.
    url = (e.currentTarget.nodeType == 3 || e.currentTarget.nodeType == 4)
        ? e.parentNode.currentTarget.href
        : e.currentTarget.href;

  // Naturally, IE must do it differently.
  } else if (e.srcElement) {
    url = getHrefForIE(e.srcElement);
  }
  
  // Calculate where the new window should be positioned.
  if (wWidth >= screen.width) {
    wX = 0;
  } else if ((wWidth + e.screenX) >= screen.width) {
    wX = screen.width - wWidth;
  } else {
    wX = e.screenX;
  }
  if (wHeight >= screen.height) {
    wY = 0;
  } else if ((wHeight + e.screenY) >= screen.height) {
    wY = screen.height - wHeight;
  } else {
    wY = e.screenY;
  }
  
  // Add coord & size to window atts.
  atts += ',width=' + wWidth + ',height=' + wHeight + ',top=' + wY + ',left=' + wX;

  // Open new window and load content in it.
  var mapWin = window.open(url, 'ARRCmap', atts);
  mapWin.focus();
  
  // Prevent main window from following link.
  if(e.preventDefault) {
    e.preventDefault();
  } else {
//     e.cancelBubble= true;
    e.returnValue = false;
  }
  return true;
}

// Hook the initializer function to the document load.
addLoadEvent(wireDescPopups);