﻿(function ($) {
            $.fn.nmcDropDown = function (options) {

                // build main options before element iteration
                var opts = $.extend({}, $.fn.nmcDropDown.defaults, options);

                // iterate each matched element
                return this.each(function () {
                    var menu = $(this);
                    submenus = menu.children('li:has(' + opts.submenu_selector + ')');

                    if (opts.fix_IE) {
                        // Fix IE 6+7 z-index bug
                        menu.css('z-index', 51)
                    .parents().each(function (i) {
                        if ($(this).css('position') == 'relative') {
                            $(this).css('z-index', (i + 52));
                        }
                    });
                        submenus.children(opts.submenu_selector).css('z-index', 50);
                    }

                    // Function that is called to show the submenu
                    over = function () {
                        $(this).addClass(opts.active_class)
                       .children(opts.submenu_selector).animate(opts.show, opts.show_speed);
                        return false;
                    }

                    // Function that is called to hide the submenu
                    out = function () {
                        $(this).removeClass(opts.active_class)
                       .children(opts.submenu_selector).animate(opts.hide, opts.hide_speed);
                        return false;
                    }

                    // Show and hide the sub-menus
                    if (opts.trigger == 'click') {
                        submenus
                    .toggle(over, out)
                    .children(opts.submenu_selector).hide();
                    } else if ($().hoverIntent) {
                        submenus
                    .hoverIntent({
                        interval: opts.show_delay,
                        over: over,
                        timeout: opts.hide_delay,
                        out: out
                    }).children(opts.submenu_selector).hide();
                    } else {
                        submenus
                    .hover(over, out)
                    .children(opts.submenu_selector).hide();
                    }
                });
            };

            // Default options
            $j.fn.nmcDropDown.defaults = {trigger: 'hover', active_class: 'open', submenu_selector: 'ul', show: { opacity: '0.9' }, show_speed: 300, show_delay: 50, hide: { opacity: '0' }, hide_speed: 0, hide_delay: 0, fix_IE: true };
        })(jQuery);
