/*!

(c) Xenario GmbH - All rights reserved.

Xenario MultiLvlMenu 

@author Jean-François Müller (mueller at styled4u dot com)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

(function($){

    $.fn.xenmultilvlmenu = function(options){
        $(this).each(function(){
            menu.call(this, options);
        });

        return this;
    }

    /**
     * Multi lvl menu class
     */
    function menu(options){

        options = options ? options : new Object();

        var self = this;

        var _view_map = options.view_map ? options.view_map : new Array();

        function _construct(){
            $(self).find("ul").each(function(){
               $(this).hide();
            });

            $(self).find("li").hover(showSection, hideSection);
        }

        function showSection(){
            $(this).addClass("hover");

            var depth = getDepth(this);
            console.log("depth", depth);
            var num_sections = _view_map[depth] != null ? _view_map[depth] : 1;

            if(num_sections == 0){
                return;
            }
            
            console.log("show num sections", num_sections);

            _recShowSection.call(this, 1, num_sections);
        }

        function hideSection(){
            $(this).removeClass("hover");

            var depth = getDepth(this);
            var num_sections = _view_map[depth] != null ? _view_map[depth] : 1;
            
            if(num_sections == 0){
                return;
            }

            _recHideSection.call(this, 1, num_sections);
        }

        function _recShowSection(cur, until){
            var ulChilds = $(this).find(">ul");

            if(cur < until){
               _recShowSection.call($(ulChilds).find(">li"), cur+1, until);
            }

            $(ulChilds).show();
        }

        function _recHideSection(cur, until){
            var ulChilds = $(this).find(">ul");

            if(cur <= until){
               _recHideSection.call($(ulChilds).find(">li"), cur+1, until);
            }

            $(ulChilds).hide();
        }

        function getDepth(liElement){
            return $(liElement).parents("li").length+1;
        }

        _construct();
    }

})(jQuery)

