/**!meta-info
 * @name           harlequin menu
 *
 * @description
 *
 * @requires       Trapeze jQuery 1.4.1-1.0.0
 * @requires       Trapeze Core Plugins 0.3.0
 * @requires       MegaMenu-2.0.0
 *
 * @group-with     mega-menu/mega-menu.js
 *
 */


$.namespace("trapeze.HarlequinMenu");

trapeze.HarlequinMenu = trapeze.MegaMenu.extend({
        
        setToggle : function(i, obj) {
            var $panel = $(obj).find('.'+this.conf.menu_block);
            if ($panel.size <= 0) return false;
             // initially hide the subitem block
            $panel.before(trapeze.render_template(this.toggle_template, {
                    'class'     : this.conf.toggle.wrapper_class,
                    'img_path'  : this.conf.toggle.img_path,
                    'img_label' : this.conf.toggle.img_label
                }));
             
             $panel.prev('.'+this.conf.toggle.wrapper_class).click($.proxy(this.openMenu, this));
             $panel.parent("li.mega-menu-item").hover($.proxy(this.openMenu, this), $.proxy(this.closeMenu, this));
        },
        
        openMenu : function(evt) {
        
        if ($(evt.currentTarget).hasClass(this.conf.toggle.wrapper_class)){
            var $obj = $(evt.currentTarget).parent().find('.'+this.conf.menu_block);
        }
        else {
            var $obj = $(evt.currentTarget).find('.'+this.conf.menu_block);
        }
        
        if (this.active_item && this.active_item.get(0) == $obj.get(0)) {    
            this.closeMenu();
        } else if (!this.active_item || this.active_item.get(0) != $obj.get(0)) {
            this.active_item = $obj.slideDown(this.conf.open_speed);
            this.active_item.parent("li").addClass("active-item");
            this.conf.onOpen({item: this.active_item});
        }
        
    },
        closeMenu : function(evt) {
        // if there is an active item and whatever was clicked was outside of the mega menu
        if (this.active_item && (evt == null || $(evt.target).parents(this.conf.selector).size() == 0 || evt.type == "mouseleave")) {
            this.active_item.stop(true, true).slideUp(this.conf.close_speed);
            this.conf.onClose({panel: this.active_item});
            this.active_item = null;
        }
    }      
});


/*
        setToggle : function(i, obj) {
            var $panel = $(obj).find('.'+this.conf.menu_block);
            if ($panel.size <= 0) return false;
             // initially hide the subitem block
            $panel.before(trapeze.render_template(this.toggle_template, {
                    'class'     : this.conf.toggle.wrapper_class,
                    'img_path'  : this.conf.toggle.img_path,
                    'img_label' : this.conf.toggle.img_label
                }));
             
             $panel.prev('.'+this.conf.toggle.wrapper_class).click($.proxy(this.openMenu, this));
             $panel.parent("li.mega-menu-item").hover($.proxy(this.openMenu, this), $.proxy(this.closeMenu({ data : {close_speed: null}}), this));
        },
        
        openMenu : function(evt) {
        
        if ($(evt.currentTarget).hasClass(this.conf.toggle.wrapper_class)){
            var $obj = $(evt.currentTarget).parent().find('.'+this.conf.menu_block);
        }
        else {
            var $obj = $(evt.currentTarget).find('.'+this.conf.menu_block);
        }
        
        if (this.active_item && this.active_item.get(0) == $obj.get(0)) {    
            this.closeMenu({ data : {close_speed:0}});

        } else if (!this.active_item || this.active_item.get(0) != $obj.get(0)) {
            if (this.active_item) {
                this.closeMenu({ data : {close_speed:0}});
            }
            this.active_item = $obj.slideDown(this.conf.open_speed);
            this.active_item.parent("li").addClass("active-item");
            this.conf.onOpen({item: this.active_item});
        }
    },
        closeMenu : function(evt) {
        // if there is an active item and whatever was clicked was outside of the mega menu
        if (this.active_item && (evt == null || $(evt.target).parents(this.conf.selector).size() == 0 || evt.type == "mouseleave")) {
            var close_speed  = (evt.data.close_speed > 0) ? evt.data.close_speed : this.conf.close_speed;  
            this.active_item.stop(true, true).slideUp(close_speed);
            this.conf.onClose({panel: this.active_item});
            this.active_item = null;
        }
    },
*/
