/*jslint browser: true, unparam: true, sloppy: true, maxlen: 80, indent: 2 */

/*globals Shadowbox, YAHOO */

Shadowbox.init();

// custom navigation based on old site
YAHOO.util.Event.onDOMReady(function () {
  (function alignTabs() {
    var tab, targetx, as, a, alen, diff, targets, target, t, tlen;
    targets = document.getElementById('overbar').getElementsByTagName('a');
    for (t = 0, tlen = targets.length; t < tlen; t += 1) {
      target = targets[t];
      if (target) {
        tab = target.id + '_sub';
        tab = document.getElementById(tab);
        if (tab) {
          targetx = YAHOO.util.Dom.getX(target);
          as = tab.getElementsByTagName('a');
          for (a = 0, alen = as.length; a < alen; a += 1) {
            diff = YAHOO.util.Dom.getX(as[a]) - targetx;
            if (diff > 0) {
              tab.style.right = diff.toString() + 'px';
              break;
            }
          }
        }
      }
    }
  }());

  function showTab(e) {
    var target = YAHOO.util.Event.getTarget(e),
      tabs = YAHOO.util.Dom.getElementsByClassName(
        'tab',
        'div',
        'underbar'
      ),
      t,
      tlen,
      tab;
    if (target.nodeName.toLowerCase() !== 'a') {
      target = YAHOO.util.Dom.getAncestorByTagName(target, 'a');
    }
    if (target && target.id) {
      tab = target.id + '_sub';
      for (t = 0, tlen = tabs.length; t < tlen; t += 1) {
        if (tabs[t].id === tab) {
          tabs[t].style.visibility = 'visible';
        } else {
          tabs[t].style.visibility = 'hidden';
        }
      }
    } else {
      for (t = 0, tlen = tabs.length; t < tlen; t += 1) {
        if (tabs[t].style.visibility !== 'hidden') {
          tabs[t].style.visibility = 'hidden';
          break;
        }
      }
    }
  }


  function hideTabs(e) {
    var tabs = YAHOO.util.Dom.getElementsByClassName('tab', 'div', 'underbar'),
      t,
      tlen,
      xy = YAHOO.util.Event.getXY(e),
      region = YAHOO.util.Dom.getRegion('nav'),
      point = new YAHOO.util.Point(xy[0], xy[1]);
    if (!region.contains(point)) { // elvis has left the building
      for (t = 0, tlen = tabs.length; t < tlen; t += 1) {
        tabs[t].style.visibility = 'hidden';
      }
    }
  }
  YAHOO.util.Event.on('overbar', 'mouseover', showTab);
  YAHOO.util.Event.on('nav', 'mouseout', hideTabs);
});


// #search - Search Widget
YAHOO.util.Event.onDOMReady(function () {
  var searchmessage = 'Site Search',
    searchcolor = '#828282',
    textbox = document.getElementById('search');
  if (YAHOO.util.Lang.isNull(textbox)) {
    return;
  }
  textbox = YAHOO.util.Dom.getElementBy(function (el) {
    return el.className === 'text';
  }, 'input', textbox);
  if (YAHOO.util.Lang.isArray(textbox)) {
    if (textbox.length) {
      textbox = textbox[0];
    } else {
      textbox = null;
      return;
    }
  }
  textbox.value = searchmessage;
  textbox.style.color = searchcolor;
  YAHOO.util.Event.on(textbox, 'click', function (e) {
    if (this.value === searchmessage) {
      this.value = '';
      this.style.color = '';
    }
  });
  YAHOO.util.Event.on(textbox, 'blur', function (e) {
    if (this.value === '') {
      this.value = searchmessage;
      this.style.color = searchcolor;
    }
  });
  textbox = null;
});

