﻿(function($) {
    var settings = {
        firstLevelTag: "div",
        firstLevelParentTag: "ul",
        parentTag: "ul",
        childTag: "li"
    };
    var $this;
    $.fn.jDropDownMenu = function(options) {
        if (options)
            $.extend(settings, options);

        $this = this;

        // Call default method
        methods.init();
    }

    var methods = {
        init: function() {
            $this.children(settings.FLTriggerTag)
                .mouseenter(function(event) {
                    methods.hideAll();
                    methods.displayChildren(this, settings.firstLevelParentTag);
                })
                .mouseleave(function(event) {
                    methods.hideChildren(this, settings.firstLevelParentTag);
                })
                .click(
                    function() {
                        methods.displayChildren(this, settings.firstLevelParentTag);
                    }
                );
        },
        hideAll: function() {
            $this.find(settings.parentTag).fadeOut();
        },
        displayChildren: function(element, parentTag) {

            var element = $(element);

            var siblings = element.siblings().find().hide();

            var children = element.children(parentTag)
                                  .show()
                                  .each(methods.observeChildren);
        },
        hideChildren: function(element, parentTag) {
            $(element).children(parentTag).hide();
        },
        observeChildren: function() {
            var lastParent = this;

            var children = $(lastParent).children(settings.childTag);
            children
                .mouseenter(function() { methods.displayChildren(this, settings.parentTag); })
                .mouseleave(function() { methods.hideChildren(this, settings.parentTag) });
        }
    }
})(jQuery);

