﻿//Megamenu - Stuart Johnston 03/10/11 - It's a hot sunny day in October
(function ($) {

    $.fn.megamenu = function (o) {
        //Reset all menus
        resetMenu();

        //If a trigger gets focus it has been tabbed, touched or clicked - show the menu
        $(o.trigger).bind("focus click", function (e) {
            resetMenu();

            $(this).addClass("current").next(o.menu).show();
            //make equal height
            $(this).next(".menu").find(".line.equalHeight .unit").equalHeight();
            //If the event is a click stop the return
            if (!e) var e = window.event;
            if (e.type == "click") {
                return false;
            }
            
        });

        //If something outside the mega menu gets *focus* or a *click* reset the Mega Menu
        //only for non-mega menu links or non mega teaser links
        $("*:not(#megaMenu a, #megaTeaserController .trigger)").bind("click focus", function () {
            resetMenu();
        });

        //If *Mouseout* of the mega menu hide content and blur any focused links
        $(this).hover(
              function () {
                  //do nothing
              },
              function () {
                  $(this).find("a").blur();
                  resetMenu();
              }
            );

        //reset all menus
        function resetMenu() {
            $(o.trigger).removeClass("current");
            $(o.menu).hide();
        }

        return this;
    };


})(jQuery);
