/*
 * L'Menu 0.0.7
 * OmegaScorp.net
 */
(function($){
    $.lmenu_options=[];
    $.lmenu_tree_options=[];
    $.fn.lmenu=function(opt){
        var index=$.lmenu_options.length;
        
        if(opt!=undefined){
            switch(opt.type){
                case 'none': opt.type=0; break;
                case 'fade': opt.type=1; break;
                case 'slide': opt.type=2; break;
                case 'size': opt.type=3; break;
                default: opt.type=0;
            }
        }
        
        $.lmenu_options[index]=$.extend({
            timeout: 500,
            closetimer: 0,
            type: 0,
            speed: 500,
            opened: 0,
            menuitems: []
        },opt);
        $(this).eq(0).attr('lmenu', index).addClass('lmenu');
        var options=$.lmenu_options[index];
        
        $(this).eq(0).lmenu_level();
        
        $(this).eq(0).find('li').mouseover(function(){
            $(this).lmenu_open();
        });
        $(this).eq(0).find('li').mouseout(function(){
            if(!$(this).children('ul').length){
                $(this).removeClass('opened');
                $(this).children('a').removeClass('opened');
            }
            var menu=$(this).closest('.lmenu');
            menu.lmenu_timer();
        });
        $(document).click(function(){
            $.lmenu_close();
        });
    },
    $.fn.lmenu_open=function(){
        var menu=$(this).closest('.lmenu');
        var index=menu.attr('lmenu');
        var options=$.lmenu_options[index];
        menu.lmenu_canceltimer();
        
        var level=$(this).parent().attr('lmenu_level');
        if(!$(this).hasClass('opened')&&level<options.menuitems.length){
            $.lmenu_close();
        }
        if(!$(this).hasClass('opened')&&$(this).children('ul').length&&$(this).children('ul').queue().length<=1){
            switch(options.type){
                case 0: options.menuitems.push($(this).children('ul').css({'display':'block'})); break;
                case 1: options.menuitems.push($(this).children('ul').fadeIn(options.speed)); break;
                case 2: options.menuitems.push($(this).children('ul').slideDown(options.speed)); break;
                case 3: options.menuitems.push($(this).children('ul').show(options.speed)); break;
            }
            $(this).addClass('opened');
            $(this).children('a').addClass('opened');
        }
        if(!$(this).children('ul').length){
            $(this).addClass('opened');
            $(this).children('a').addClass('opened');
        }
    }
    $.lmenu_close=function(){
        $.each($.lmenu_options, function(){
            if(this.menuitems.length){
                menuitem=this.menuitems.pop();
                menuitem.parent().removeClass('opened');
                menuitem.parent().children('a').removeClass('opened');
                switch(this.type){
                    case 0: menuitem.css({'display':'none'}); break;
                    case 1: menuitem.fadeOut(this.speed); break;
                    case 2: menuitem.slideUp(this.speed); break;
                    case 3: menuitem.hide(this.speed); break;
                }
            }
        });
    }
    $.fn.lmenu_timer=function(){
        var index=$(this).attr('lmenu');
        var options=$.lmenu_options[index];
        options.closetimer = setTimeout(function(){
            if(options.closetimer){
                $.lmenu_close();
            }
        }, options.timeout);
        $.lmenu_options[index]=options;
    }
    $.fn.lmenu_canceltimer=function(){
        var index=$(this).attr('lmenu');
        var options=$.lmenu_options[index];
        if(options.closetimer){
            clearTimeout(options.closetimer);
            options.closetimer = null;
        }
        $.lmenu_options[index]=options;
    }
    $.fn.lmenu_tree=function(opt){
        var index=$.lmenu_tree_options.length;
        if(opt!=undefined){
            switch(opt.type){
                case 'none': opt.type=0; break;
                case 'fade': opt.type=1; break;
                case 'slide': opt.type=2; break;
                case 'size': opt.type=3; break;
                default: opt.type=0;
            }
        }
        $.lmenu_tree_options[index]=$.extend({
            type: 0,
            speed: 500,
            hide: true
        },opt);
        $(this).eq(0).attr('lmenu', index).addClass('lmenu_tree');
        var options=$.lmenu_tree_options[index];
        
        $(this).lmenu_level();
        
        $(this).find('li.active.have-sub').addClass('opened').children('a').addClass('opened');
        
        $(this).find('a').click(function(){
            if($(this).parent().find('ul').html()==null) return true;
            if($(this).parent().find('ul').css('display')!='none'){
                $(this).parent().removeClass('opened').children('a').removeClass('opened');
                switch(options.type){
                    case 0: $(this).parent().find('ul').css({'display':'none'}); break;
                    case 1: $(this).parent().find('ul').fadeOut(options.speed); break;
                    case 2: $(this).parent().find('ul').slideUp(options.speed); break;
                    case 3: $(this).parent().find('ul').hide(options.speed); break;
                }
            }
            else{
                switch(options.type){
                    case 0:
                        if(options.hide){
                            $(this).parent().parent().find('ul').css({'display':'none'});
                            $(this).parent().parent().find('li, li>a').removeClass('opened');
                        }
                        $(this).parent().find('ul:first').css({'display':'block'});
                        break;
                    case 1:
                        if(options.hide){
                            $(this).parent().parent().find('ul').fadeOut(options.speed);
                            $(this).parent().parent().find('li, li>a').removeClass('opened');
                        }
                        $(this).parent().find('ul:first').fadeIn(options.speed);
                        break;
                    case 2:
                        if(options.hide){
                            $(this).parent().parent().find('ul').slideUp(options.speed);
                            $(this).parent().parent().find('li, li>a').removeClass('opened');
                        }
                        $(this).parent().find('ul:first').slideDown(options.speed);
                        break;
                    case 3:
                        if(options.hide){
                            $(this).parent().parent().find('ul').hide(options.speed);
                            $(this).parent().parent().find('li, li>a').removeClass('opened');
                        }
                        $(this).parent().find('ul:first').show(options.speed);
                        break;
                }
                $(this).parent().addClass('opened').children('a').addClass('opened');
            }
            return $(this).parent().parent().attr('lmenu_level')=='0'&&$(this).parent().hasClass('opened')?true:false;
        });
    }
    $.fn.lmenu_level=function(level){
        if(level==undefined) level=0;
        $(this).attr('lmenu_level', level);
        $(this).children('li').each(function(){
            if($(this).find('ul').length){
                $(this).addClass('have-sub').children('a').addClass('have-sub');
            }
        });
        $(this).children('li').children('ul').each(function(){
            $(this).lmenu_level(level+1);
        });
    };
})(jQuery);
