/*
sets the selected tab in the horizontal menu
also prepares hover mechanism for tabs to show drop down menu
*/
(function(){
  try
  {
    /*
    * List of tabs/tab-ids in the horizontal navigation menu. The index is the id of the LIs under the "nav" UL
    * The tabs are iterated, for each tab the onmouse(over|out) is set, and/or the "on" class is set if a match is found
    *
    * Each tab has an array of matching url parts that when found will "set" the tab to "on" 
    * an item can be a string or regex
    * if item is a string and starts with '/' it is assumed to be at the doc root
    * use all lower case, even if the actual page on the server isn't all lower case
    */
    var tabs = {
        'cs-tab': ['/cs/'],
        'opin-tab': ['/opin/', '/googlesearch_opinions.shtml'],
        'open-tab': ['/open/', '/googlesearch_openrecords.shtml'],
        'victims-tab': ['/victims/'],
        'criminal-tab': ['/criminal/', '/forms/mfcu/residential.shtml', /\/alerts\/alerts_view.*?type=3/],
        'elder-tab': ['/elder/', /\/alerts\/alerts_view.*?type=2/],
        'consumer-tab': ['/consumer/', '/iia/', '/border/colonias.shtml', /\/alerts\/alerts_view.*?type=1/],
        'home-tab': ['/']
    };

    var renderMenu = function() {
      var pathName = window.location.pathname.toLowerCase();//use for strings
      var href = window.location.href.toLowerCase();//use for regex
      //console.log('pathName is %s', pathName);

      //var rgx_type = isIE ? 'object' : 'function';//uh, isIE comes from AC_RunActiveContent.js
      var rgx_type = 'object';
      
      var sfhover_rgx = new RegExp(" sfhover\\b");
      var found_it = false;
      var dom_obj;
      var t;
      var s;
      for(var tab in tabs)
      {
        //if the LI (tab) doesn't exist in the HTML, no need to look for matches or set up drop-down menu
        dom_obj = document.getElementById(tab);
        if(!dom_obj) continue;

        //setup the hover drop-down menu
        dom_obj.onmouseover = function() {
          this.className+=" sfhover";
        }
        dom_obj.onmouseout = function() {
          this.className = this.className.replace(sfhover_rgx, "");
        }

        //the correct tab has already been found and set to "on", no need to keep searching
        if(found_it) continue;

        for(var i = 0, len = tabs[tab].length; i < len; ++i)
        {
          t = typeof tabs[tab][i];
          s = tabs[tab][i];
                   
          //console.log('typeof tabs[%s][%d] is %s', tab, i, t);
          //console.log('item searched is %s', s);
          if(t == 'string')
          {
            //we assume this is at the beginning of the path, otherwise they could find '/admin/' in '/someotherdir/admin/
            if(s.charAt(0) == '/') 
			      {
			        if(pathName.substr(0, s.length) == s) found_it = true;
            }
            else 
            {
              if(pathName.indexOf(s) > -1) found_it = true;
            }
          }
          else if(t == rgx_type && s.test(href))
          {
            found_it = true;
          }
          if(found_it)
          {
            //console.log('item found is %s', s);
            dom_obj.className = "on";
          }
        }

      }
    };

    if (window.addEventListener) //run onload in DOM2 browsers
    {
      window.addEventListener("load", renderMenu, false)
    }
    else if (window.attachEvent) //run onload in IE5.5+
    {
      window.attachEvent("onload", renderMenu);
    }
    else if (document.getElementById) //if legacy DOM browsers, try after 1 sec
    {
      setTimeout(renderMenu, 1000);
    }
  }
  catch(e){}
})();