(function($){
 $.fn.megaMenu = function(menu,options) {
 		var anchor = this;
		var toggled = false;

	  var defaults = {
	   action: 'click', // valid actions are mouseover & click
     closeClass: 'mega-menu-close', // these elements will fire the close menu event when clicked
	   showAnimationMethod: 'fadeIn',
	   hideAnimationMethod: 'fadeOut'
	  };
	  
	  var options = $.extend(defaults, options);

    // register any/all close links
    $('.'+options.closeClass).bind('click', {anchor: anchor}, function(event) {
      event.data.anchor.click();
    });

    return this.each(function() {
    	if (null == menu) return;

			if (options.action == 'click') {
				setMegaMenuClickEvent(this, menu, options);
			} else if (options.action == 'mouseover') {
				setMegaMenuMouseoverEvent(this, menu, options);
			}
    });
    
    function setMegaMenuMouseoverEvent(anchor, menu, options) {
    };
    
    function setMegaMenuClickEvent(anchor, menu, options) {
      $(anchor).bind(options.action, doAnimation);
    };
    
    function doAnimation() {
    	if (toggled) {
    		$(this).removeClass('active');
    		eval('$("' + menu + '").' + getHideAnimationMethod(options) + '()');
    	} else {
    		$(this).addClass('active');
    		eval('$("' + menu + '").' + getShowAnimationMethod(options) + '()');
    	}
    	toggled = (toggled) ? false : true;
    }
    
    function getShowAnimationMethod(options) {
			return showAnimationMethod = (eval('typeof($.fn.' + options.showAnimationMethod + ')') == 'function')
				? options.showAnimationMethod
				: 'toggle';
    };
    
    function getHideAnimationMethod(options) {
			return hideAnimationMethod = (eval('typeof($.fn.' + options.hideAnimationMethod + ')') == 'function')
				? options.hideAnimationMethod
				: 'toggle';
    };
 };
})(jQuery);

