/*
   Simple JQuery Accordion menu.
   HTML structure to use:

Copyright 2007 by Marco van Hylckama Vlieg

web: http://www.i-marco.nl/weblog/
email: marco@i-marco.nl
*/

// Heavily Modified by Keith Thompson 6-8-2011


$(document).ready(function() {initMenu();});
function initMenu() {

	if($('li').hasClass('menu-item-parent')) {
  var children, newHeight;
  var defaultHeight = $('.menu-item').css('height');
  var childHeight = $('.menu-item-group a').css('height');
  var childPaddingTop = $('.menu-item-group a').css('padding-top');
  var childPaddingBottom = $('.menu-item-group a').css('padding-bottom');

  // The variables start out as strings and we need to remove the px and
  // parse them to ints before doing the math later on
  defaultHeight = parseInt(defaultHeight.slice(0, -2));
  childHeight = parseInt(childHeight.slice(0, -2));
  childPaddingTop = parseInt(childPaddingTop.slice(0, -2));
  childPaddingBottom = parseInt(childPaddingBottom.slice(0, -2));

  $('#menu ul').hide();

  //This conditional makes sure that the dropdown is already open if you are on
  //a page linked by a child anchor of a dropdown
  if($('#menu ul a').is('.selected')) {
    var parent = $('#menu ul a.selected').parent().parent();
    $(parent).addClass('selected');
    children = $(parent).children().length;
    newHeight = defaultHeight + ((childPaddingTop + childPaddingBottom + childHeight) * children );
    $('#menu ul.selected').siblings('a').addClass('active-dropdown').parent().addClass('dropdown-root');
    $('.dropdown-root').css('padding-top','2px').css('padding-bottom','2px');
    $('#menu ul.selected').parent().addClass('active-dropdown-parent');
    $('.active-dropdown-parent').css('height', newHeight + 'px');
    $('#menu ul.selected').show();
  }

  //This adds the dropdown functionality to the menu bar
  $('#menu li a').click(function() {
    var checkElement = $(this).next();

    //If the clicked element is already activated it will be deactivated and
    //also slide back up
    if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
      checkElement.slideToggle();
      $('li.menu-item-parent').animate({ height: defaultHeight + 'px' });
      $('.dropdown-root').css('padding-top','0').css('padding-bottom','0');
      $('.dropdown-root').removeClass('dropdown-root');
      $('.active-dropdown').removeClass('active-dropdown');
      $('.active-dropdown-parent').removeClass('active-dropdown-parent');
    }

    //If the clicked element isn't already activated, any activated dropdown
    //will slide up and the clicked item will activate and slide down
    if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {

      // Here we establish variables for getting the right height on the dropdown
      children = $(checkElement).children().length;
      newHeight = defaultHeight + ((childPaddingTop + childPaddingBottom + childHeight) * children );

      // This is where we hide what was open (if there was one) and open our
      // new drop down panel
      $('#menu ul:visible').hide();
      $('li.menu-item-parent').css('height', defaultHeight + 'px').css('padding-top', '0').css('padding-bottom', '0');
      $('.dropdown-root').removeClass('dropdown-root');
      $('.active-dropdown').removeClass('active-dropdown');
      $('.active-dropdown-parent').removeClass('active-dropdown-parent');

      // Here is where the new classes are added to the drop down that's being
      // opened, and we also adjust the height of the li containers of
      // drop downs other than the lowest one so the on hover still works
      $(this).addClass('active-dropdown');
      $(this).parent().addClass('dropdown-root');
      $(this).parent().addClass('active-dropdown-parent');
      checkElement.slideToggle();
      $('.dropdown-root').css('padding-top', '2px').css('padding-bottom','2px');
      $('.active-dropdown-parent').animate({
        height: newHeight + 'px'
      });
    }
  });
  }
}

