/* ihwg.js */


// detect the current topnav tab and make its background effect
addEvent(window,"load",checkActiveTopNav);

// scan the page to see if any "order now" buttons are there (class="order_now")
addEvent(window,"load",checkOrderNowButtons);

// check which branch we are in, and make that topnav effect
// background-color:#009cd7
function checkActiveTopNav(){
  // here are the patterns that should trigger certain topnav elements
  // add patterns that match any given topnav element (left to right, 1 to 6) by
  // adding more URL patterns to the array on the right hand side. 
  var topnavs = Array();
  topnavs['topnav_1'] = ['home'];
  topnavs['topnav_2'] = ['hla/'];
  topnavs['topnav_3'] = ['reference/'];
  topnavs['topnav_4'] = ['order/'];
  topnavs['topnav_5'] = ['resources/'];
  topnavs['topnav_6'] = ['about/'];
  
  // scroll through the options and detect the active topnav
  var el, i, navfound;
  // nodepath is either the <meta name="node-path" content="..."> tag, or the document location (see master page)
  var nodepath = document.getElementById('node-path') ? document.getElementById('node-path').getAttribute('content') : window.location.href; //alert(nodepath);
  for (el in topnavs) { 
    if(!navfound){
      for (i=0; i<topnavs[el].length; i++){ //alert(el+' '+topnavs[el][i]);
        if(nodepath.indexOf(topnavs[el][i])>-1){
          if(document.getElementById(el)){
             document.getElementById(el).style.backgroundColor="#009cd7";
             navfound = true;
          }
        }
      }
    }
  }
  // the site home page (/index.xml or /index.xml) is special
  //$cmignore
  if(nodepath=='/IHWG/'||nodepath=='/'||window.location.pathname=='index.html'){
    if(document.getElementById('topnav_1')){
       document.getElementById('topnav_1').style.backgroundColor='#009cd7';
    }
  }
  //$/cmignore
}

// scan the page html and seek out any IMG elements with a certain
// src value. those are "order buttons" that need to be dynamic rollovers.
function checkOrderNowButtons(){
  //alert('checking for order now buttons...');
  var nds = document.getElementsByTagName('img');
  for( var i=0; i<nds.length; i++ ){
    if( nds.item(i).getAttribute('src').indexOf('order_button_stat.gif')>-1 ){
      addEvent(nds.item(i),"mouseover",handleOrderNowButtonHover);
      addEvent(nds.item(i),"mouseout",handleOrderNowButtonStat);
    }
  }
}

function handleOrderNowButtonHover(e){
  var el;
  if(e.target) el=e.target;
  else{
    if(e.srcElement) el=e.srcElement;
  } //alert(el.src);
  if(!el) return;
  el.src = "../img/order_button_over.gif"; // this needs to be absolute in production
  //alert(e.src);
}
function handleOrderNowButtonStat(e){
  var el;
  if(e.target) el=e.target;
  else{
    if(e.srcElement) el=e.srcElement;
  } //alert(el.src);
  if(!el) return;
  el.src = "../img/order_button_stat.gif"; // this needs to be absolute in production
  //alert(e.src);
}


// general-use function to add handlers.
// use like this:
//    if(document.getElementById('ehs.form')){
//      addEvent(document.getElementById('ehs.form'), 'click', handleRadioClick);
//    }
// JS Bible 6th ed.
function addEvent(elem,evtType,func){
  if(elem.addEventListener){ elem.addEventListener(evtType,func,false); }
  else if(elem.attachEvent){ elem.attachEvent("on"+evtType, func); }
  else { elem["on"+evtType] = func; }
}

// this omits the slider menu while in the CMS
function omitSlideoutMenu(){
  if(document.getElementById('thessm')){
    document.getElementById('thessm').style.display='none';
  }
}
addEvent(window,"load",omitSlideoutMenu);


