var current_id = "";

window.onload = function() {
  // change all of the links to be to the current host
  var as = document.getElementsByTagName('a');
  for (var i = 0; i < as.length; i++) {
    if (!/^\/\b/.test(as[i].href) && as[i].href.match(new RegExp(document.location.host))) {
      as[i].href = as[i].href.replace(/^http:\/\/[^\/]+?\/\b/, 'http://' + location.host + '/');
    }
  }
};


/*
  given a token, return the sunav ul elementId, or '' if none is found
*/
function find_block_id(text) {
  var nav = document.getElementById('nav');


  var as  = nav.getElementsByTagName('a');
  var ai  = null;
  for (var i = 0; i < as.length; i++) {
    var re = new RegExp(text + '\/$');
    if (re.test(as[i].href)) {
      ai = as[i];
      break;
    }
  }

  if (ai === null) {
    return '';
  }

  while (ai = ai.nextSibling) {
    if (ai.tagName && ai.tagName.toLowerCase() === 'ul') {
      return ai;
    }
  }

  return '';

/*
  var all_tags = document.getElementById("nav_parent_0").getElementsByTagName("LI");
  for(var i=0;i<all_tags.length;i++)
  {
    if(all_tags[i].className=="parent" && all_tags[i].innerHTML.search(text)!=-1)
    {
      var target_id=all_tags[i].id.replace("item","parent");
      return target_id;
    }
  }
  return '';
*/
}

/*
  given a token and an idx, install that subnav in div#sub_menu and show it
*/
function menu_on_hover(menu_name,i)
{
  document.getElementById("sub_menu").innerHTML = "";
  document.getElementById("sub_menu").style.display = "none";
  if(menu_name == "") {return;}
  var ul = find_block_id(menu_name);
  if (ul === '') {return;}

  document.getElementById("sub_menu").innerHTML = '<ul>' + ul.innerHTML + '</ul>';

  offset = 0;
  switch(i)
  {
      case 1: offset = 126;break;
      case 2: offset = 253;break;
      case 3: offset = 380;break;
      case 4: offset = 506;break;
      case 5: offset = 633;break;
//      case 6: offset = 660;break;
  }

  document.getElementById("sub_menu").style.left = offset+"px";
  document.getElementById("sub_menu").style.display = "block";

  document.getElementById("main_body").onmouseover=function(){document.getElementById("sub_menu").innerHTML = "";  document.getElementById("sub_menu").style.display = "none";};
  document.getElementById("sub_banner").onmouseover=function(){document.getElementById("sub_menu").innerHTML = "";  document.getElementById("sub_menu").style.display = "none";};
}

function menu_off_hover()
{

}

function replace(str,target,replacement)
{
  if(str.indexOf(target)==-1)
  {
    target=target.toLowerCase();
    replacement=replacement.toUpperCase();
  }
  while(str.indexOf(target)!=-1)
  {str=str.replace(target,replacement);}
  return str;

}

/*
  set the active state for the current menu item
*/
function set_top_menu()
{
  var target=null;
  if(location.href.search("about")!=-1){target = document.getElementById("about_link");}
  if(location.href.search("pipeline")!=-1){target = document.getElementById("pipeline_link");}
  if(location.href.search("technology")!=-1){target = document.getElementById("technology_link");}
  if(location.href.search("press_releases")!=-1||location.href.search("news")!=-1){target = document.getElementById("news_link");}
  if(location.href.search("career")!=-1){target = document.getElementById("career_link");}
  if(location.href.search("contact")!=-1){target = document.getElementById("contact_link");}
  if(target == null){return;}
  target.firstChild.src = target.firstChild.src.replace(".jpg","_on.jpg");
  //alert(target.firstChild.onmouseout);
  target.firstChild.onmouseout = "";
  target.firstChild.onmouseout = null;
  //target.firstChild.onmouseout = target.firstChild.onmouseout.replace(".jpg","_on.jpg");
  //target.innerHTML = target.innerHTML.replace(".jpg","_on.jpg");
  //alert(target.innerHTML);
}

