$(document).ready(function() {
  $('#searchsubmit').hide();
  var s = $('#searchterm')
            .bind('focus', function() { this.className = 'active' })
            .bind('blur change', function() { this.className = ($.trim(this.value) != '' ? 'active' : '') })
            .get(0);
  if (s == document.activeElement || $.trim(s.value) != '') { s.className = 'active' }
  animateMenu.build('home_menu',false,179,10,150,1000);
  animateMenu.build('main_menu',true,179,10,150,1000,openMenuItem);
});

// Accordion adapted from Michael Leigeber
// http://www.scriptiny.com/2008/05/horizontal-javascript-accordion-menu/

// Image Crossfade inspired by Remy Sharp
// http://jqueryfordesigners.com/image-cross-fade-transition/

// Merged and refined by Cameron Clark

// menuID     : id of the unordered list to bind the accordion to.
// mSlide     : whether to do the sliding effect
// aHeight    : height of the open accordion selection (closed height is automatically calculated)
// mSpeed     : time (milliseconds) for the animation
// mSteps     : number of steps in the animation (higher number = smoother, 1 = no animation)
// mDelay     : time (milliseconds) to delay before reverting to opening section specified by openIndex
// openIndex  : (optional) integer (zero-based) of the section you would like to be expanded when the accordion is loaded

var animateMenu = function() {
  var menuUL, menuSlide, activeHeight, menuSpeed, menuSteps, menuLIs, liCount, menuHeight, defaultHeight, inactiveHeight, menuInterval, interval, timeout, imgs = $();
  return {
    build: function(menuID, mSlide, aHeight, mSteps, mSpeed, mDelay, openIndex) {
      menuUL  = $('#'+menuID);
      if (menuUL.length == 0) return;
      menuSlide = mSlide; activeHeight = aHeight; menuSteps = mSteps; menuSpeed = mSpeed;  // store variables
      menuInterval = parseInt(menuSpeed/menuSteps,10);
      menuLIs = $('li',menuUL);
      if (menuSlide) {
        liCount = menuLIs.length;
        menuHeight = menuUL.height();
        defaultHeight = menuHeight/liCount;
        inactiveHeight = Math.floor((menuHeight-activeHeight)/(liCount-1));
        // set initial height
        menuLIs.height(defaultHeight);
      }
      menuLIs.each(function(index) {
        // find & cache existing menu elements and create new image
        var li = $(this), a = $('a',li), img1 = $('img',a), img2 = $('<img>');
        // parse image path to derive alternate image
        var img2path = img1.attr('src').replace(/_off\./, "_on.");
        // remove img1 from anchor, then re-insert behind it
        img1.remove().css({'z-index':'1'}).insertBefore(a);
        // create new image and preload, insert new img behind anchor
        img2.attr('src', img2path).css({'z-index':'2', 'display':'none'}).insertBefore(a);
        // give anchor higher z-index
        a.css({'z-index':'3'});
        imgs = imgs.add(img2); // store image
        // on mouseover, start sliding
        li.mouseover(function(){
          animateMenu.start(index);
        });
        // if openIndex, set mouseout action on all other LIs that waits and then turns on openIndex
        if (openIndex != null && openIndex != index) {
          li.mouseout(function(){
            animateMenu.timeout = setTimeout(function(){
              animateMenu.start(openIndex);
            },mDelay)
          });
        }
      });
      // if openIndex, turn it on instantly
      if (openIndex != null) {
        this.debut(openIndex);
      }
    },
    debut: function(openIndex){
      imgs.eq(openIndex).fadeTo(0,1);
      if (menuSlide) {
        for (var index=0; index<liCount; index++) {
          if (openIndex != index) {
            menuLIs.eq(index).height(inactiveHeight);
          }
        }
        menuLIs.eq(openIndex).height(activeHeight);
      }
    },
    start: function(activeIndex){
      clearTimeout(animateMenu.timeout);
      animateMenu.crossfade(activeIndex);
      if (menuSlide) {
        clearInterval(animateMenu.interval);
        animateMenu.interval = setInterval(function(){
          animateMenu.slide(activeIndex);
        },menuInterval)
      }
    },
    crossfade: function(activeIndex) {
      imgs.each(function(index) {
        if (index == activeIndex) {
          // if activeLI, stop any active animations and fade img1 to opaque
          $(this).stop().fadeTo(400, 1);
        } else {
          // for all other lis, stop any active animations and fade img1 to transparent
          $(this).stop().fadeTo(400, 0);
        }
      });
    },
    slide: function(activeIndex){
      // get height of active LI
      var currentHeight = menuLIs.eq(activeIndex).height();
      if (currentHeight < activeHeight) {
        var inactiveHeightTotal = 0;
        for (var index=0; index<liCount; index++) {
          if (index != activeIndex) {
            var increment = 0;
            var liHeight = menuLIs.eq(index).height();
            if (liHeight > inactiveHeight) {
              increment = Math.floor((liHeight-inactiveHeight)/menuSteps);
              increment = (increment>0) ? increment : 1;
              menuLIs.eq(index).height(liHeight-increment);
            }
            inactiveHeightTotal = inactiveHeightTotal+(liHeight-increment);
          }
        }
        menuLIs.eq(activeIndex).height(menuHeight-inactiveHeightTotal);
      } else {
        clearInterval(animateMenu.interval);
      }
    }
  };
}();
