/* Adds an event listener to an element. */
function addEvent(elm, evType, fn, useCapture){
  if(elm.addEventListener){
    elm.addEventListener(evType, fn, useCapture);
  }
  else if(elm.attachEvent){
    var r = elm.attachEvent('on'+evType, fn);
    return r;
  }
  else{
    elm['on'+evType] = fn;
  }
}


/* Goes up the DOM tree and looks for an element of a given type. */
function ascendDOM(e, target){
  while(e.nodeName.toLowerCase()!=target && e.nodeName.toLowerCase()!='html'){
    e = e.parentNode;
  }
  
  return (e.nodeName.toLowerCase()=='html') ? null : e;
}



/* Returns the element that invoked the action. */
function getInvoker(e){
  var el;
  if(window.event && window.event.srcElement){
    el = window.event.srcElement;
  }
  if(e && e.target){
    el = e.target;
  }
  
  if(!el){
    return;
  }
  else{
    return el;
  }
} 


/* Cancels default action. */
function cancelDefault(e){
  if(window.event && window.event.returnValue){
    window.event.returnValue = false;
  }
  if(e && e.preventDefault){
    e.preventDefault();
  }
}


/* Cancels default action (Safari). */
function cancelDefaultSafari(e){
  return false;
}


/* Switches the image the color version. */
function showColorImg(e){
  var el = getInvoker(e);
  
  el = ascendDOM(el, "img");
  el.src = el.src.replace(/\b ?-bw\b/, '');
}


/* Switches the image the BW version. */
function showBWImg(e){
  var el = getInvoker(e);
  
  el = ascendDOM(el, "img");
  el.src = el.src.replace(/\b ?.png\b/, '.png');
}


/* Should be invoked by an TD or TH element to highlight the whole row. */
function highlightTr(e){
  var el = getInvoker(e);
  var tr = ascendDOM(el, "tr");
  
  if(tr.className.indexOf("highlighted")==-1){
    tr.className += " highlighted";
  }
}


/* Should be invoked by an TD or TH element to unhighlight the whole row. */
function unhighlightTr(e){
  var el = getInvoker(e);
  var tr = ascendDOM(el, "tr");
  
  tr.className = tr.className.replace(/\b ?highlighted\b/, '');
}


/* Blurs the invoker. */
function fireBlur(e){
  var el = getInvoker(e);
  el.blur();
}


/* Calls the targetBlank function on the invoker's href. */
function targetBlankComplete(e){
  var el = getInvoker(e);
  var el = ascendDOM(el, "a");
  targetBlank(el.href);
}


/* Adds listeners for a page. */
function addListeners(){
  if(!document.getElementById){
    return;
  }
  
  //for each image in divSlideShow add hover effect which switches the image to a color version
  var divSlideShow = document.getElementById("divSlideShow");
  
  if(divSlideShow){
    var imgs = divSlideShow.getElementsByTagName("img");
    
    for(var i=0; i<imgs.length; i++){
      window.addEvent(imgs[i], 'mouseover', showColorImg, false);
      window.addEvent(imgs[i], 'mouseout', showColorImg, false);
    }  
  }

  
  //for each link prevent focusing (ugly border in MSIE)
  //for each external link convert link to point to a blank window
  var links = document.getElementsByTagName("a");
  for(var i=0; i<links.length; i++){
    window.addEvent(links[i], 'focus', fireBlur, false);
    
    if(links[i].className.indexOf("external")!=-1){
      window.addEvent(links[i], 'click', targetBlankComplete, false);
      window.addEvent(links[i], 'click', cancelDefault, false);
      links[i].onclick = cancelDefaultSafari;
    }
  }
  
  
  //for each table column/header add row highlighting
  var tds = document.getElementsByTagName("td");
  for(var i=0; i<tds.length; i++){
    window.addEvent(tds[i], 'mouseover', highlightTr, false);
    window.addEvent(tds[i], 'mouseout', unhighlightTr, false);
  }

  var ths = document.getElementsByTagName("th");
  for(var i=0; i<ths.length; i++){
    window.addEvent(ths[i], 'mouseover', highlightTr, false);
    window.addEvent(ths[i], 'mouseout', unhighlightTr, false);
  }
   
}


/* Initializes round corners. */
function roundCorners(){
  rC = new DHTMLgoodies_roundedCorners();
  rC.addTarget('h1Title',30,20,'#FFA80D','#FFFFFF',15,45, 'top_left,top_right');
  rC.addTarget('h2Title',5,5,'#FEC869','#FFFFFF',20,0, 'bottom_left,bottom_right');
  rC.addTarget('divFooter',10,10,'#FEC869','#FFFFFF',20,0, 'bottom_left,bottom_right,top_left,top_right');
  rC.init();
}


//add listeners
window.addEvent(window, 'load', addListeners, false);







