﻿var jmenu = {
    effect: 'fade', 
    duration: 400,  
    set: function (settings) {
        try {
            if (settings.animation == 'show') { this.effect = 'show'; }
            if (settings.animation == 'slide') { this.effect = 'slide'; }
            if (settings.animation == 'fade') { this.effect = 'fade'; }
        } catch (e) { }

        try {
            this.duration = settings.duration;
        } catch (e) { }
    },
    fix_pos: function (elem) {
        if ($(elem).parent('ul').parent('li').length) {
            $(elem).children('ul').eq(0).css({ marginTop: -$(elem).height(), marginLeft: $(elem).width() });
        } else {
            $(elem).children('ul').eq(0).css({ 'top': $(elem).offset().top + $(elem).height() - 2, 'left': $(elem).offset().left });
        }
    },
    show: function (elem) {
        if (this.effect == 'fade') { $(elem).children('ul').eq(0).stop(1, 1).fadeIn(this.duration); }
        else if (this.effect == 'slide') { $(elem).children('ul').eq(0).stop(1, 1).slideDown(this.duration); }
        else if (this.effect == 'show') { $(elem).children('ul').eq(0).stop(1, 1).show(this.duration); }
    },
    hide: function (elem) {
        //$(elem).children('ul').eq(0).stop(1, 1).fadeOut(100);
        if (this.effect == 'fade') { $(elem).children('ul').eq(0).stop(1, 1).fadeOut(this.duration); }
        else if (this.effect == 'slide') { $(elem).children('ul').eq(0).stop(1, 1).slideUp(this.duration); }
        else if (this.effect == 'show') { $(elem).children('ul').eq(0).stop(1, 1).hide(this.duration); }
    }
}

jQuery.fn.jmenu = function (settings) {
    jmenu.set(settings);

    $(this).find('li').each(function () {
        $(this).hover(
                function () {
                    jmenu.fix_pos(this);
                    jmenu.show(this);
                },
                function () {
                    jmenu.hide(this);
                }
            );
    });
}
