/*
 * jQuery DropMenu
 * ---
 * No-frills drop-downs.
 *
 * Tweaked for Park Place to accommodate a problem with PNG backgrounds
 * and jQuery's fadeIn()/fadeOut() (read: filters). Checks for true,
 * non-filter opacity support and fades only if proper opacity support
 * is available. Otherwise you'll see a nasty black background on the
 * drop-down's transparent parts (PNG) while IE switches from the
 * PNG "filter" for opacity to the element opacity filter and then back
 * again. Someday, IE. Someday...
 *
 * (C) 2011 MyNorth Media, hello@traversemagazine.com
 */

if (typeof(jQuery) != 'undefined') {
  (function($) {
    $.fn.dropMenu = function(settings) {
      var config = {
        'droppedClass': 'dropped',
        'droppedChild': '> ul',
        'fadeInSpeed': 200,
        'fadeOutSpeed': 400,
        'subTimeout': 800
      };

      if (settings) $.extend(config, settings);

      function hideDropDown(menuItem, useTimeout) {
        function gtfoDropdown() {
          $(menuItem).removeClass(config.droppedClass);
          if ($.support.opacity) {
            $(menuItem).find(config.droppedChild).stop(true, true).fadeOut(config.fadeOutSpeed);
          } else {
            $(menuItem).find(config.droppedChild).stop(true, true).hide();
          }
        };

        clearTimeout($(menuItem).data('fadeInt'));

        if (useTimeout) {
          $(menuItem).data('fadeInt', setTimeout(function() {
            gtfoDropdown();
          }, config.subTimeout));
        } else {
          gtfoDropdown();
        }

      };

      function hideAllOtherDropDowns(menuItem) {
        $(menuItem).parent().find('.' + config.droppedClass).each(function() {
          if (this != menuItem) {
            hideDropDown(this, false);
          }
        });
      };

      function showDropDown(menuItem) {
        hideAllOtherDropDowns(menuItem);
        clearTimeout($(menuItem).data('fadeInt'));

        $(menuItem).addClass(config.droppedClass);
        if ($.support.opacity) {
          $(menuItem).find(config.droppedChild).stop(true, true).hide().fadeIn(config.fadeInSpeed);
        } else {
          $(menuItem).find(config.droppedChild).stop(true, true).show();
        }
      };

      this.each(function() {
        $(this).find('> li').each(function() {
          if ($(this).find(config.droppedChild).length) {
            $(this).hover(function() {
              showDropDown(this);
            }, function() {
              hideDropDown(this, true);
            });
          } else {
            $(this).mouseover(function() {
              $(this).parent().find('.' + config.droppedClass).each(function() {
                hideDropDown(this, false);
              });
            });
          }
        });

      });

      return this;

    };
  })(jQuery);
}

