/*
*   Dropdown Navigation
*   Generates a drop-down navigation
*
*   Requires: Trapeze jQuery distribution
*
*   Marcos Abreu - April 18, 2009
*/

$.namespace("trapeze.DropdownNavigation");

trapeze.DropdownNavigation = $.Class.extend({
    jNavigation : null,
    settings  : null,
    dd_active   : null,

    toggle_template : '<a class="toggle-dropdown" href="javascript:void(0);"><img class="%(image_class)" src="%(image_path)" alt="%(image_label)" /></a>',

    build_from_list : function(i, obj) {
        var current_dd = $(obj).find('.'+this.settings.subnav_class);
        if (current_dd.size() > 0) {
            current_dd.before(trapeze.render_template(this.toggle_template, {
                "image_class" : this.settings.toggle_img.extra_class, 
                "image_path"  : this.settings.toggle_img.path, 
                "image_label" : this.settings.toggle_img.label
            }));
        }

        $(obj).children(".toggle-dropdown").click(
            this.open_dropdown.bind(this)
        ).hover(
            this.over_dropdown.bind(this),
            this.out_dropdown.bind(this)
        );

        $(obj).bind('click', this.settings.on_click);

        current_dd.find('li').hover(
            this.over_dropdown_item.bind(this),
            this.out_dropdown_item.bind(this)
        );
    },

    over_dropdown : function(evt) {
        $(evt.currentTarget).parent().addClass('dropdown-hover').find('.'+this.settings.subnav_class).addClass('dropdown-item-hover');
    },

    out_dropdown : function(evt) {
        $(evt.currentTarget).parent().removeClass('dropdown-hover').find('.'+this.settings.subnav_class).removeClass('dropdown-item-hover');
    },

    over_dropdown_item : function(evt) {
        $(evt.currentTarget).addClass('dropdown-item-hover');
    },

    out_dropdown_item : function(evt) {
        $(evt.currentTarget).removeClass('dropdown-item-hover');
    },

    open_dropdown : function(evt) {
        var obj = $(evt.currentTarget).parent().find('.'+this.settings.subnav_class).get(0);
        
        if (this.dd_active && this.dd_active != obj) {
            $(this.dd_active).slideUp(this.settings.close_speed);
        }
        if (this.dd_active == obj) {
            $(obj).slideUp(this.settings.close_speed);
            this.dd_active = null;
        } else {
            $(obj).slideDown(this.settings.open_speed);
            this.dd_active = obj;
        }
    },

    close_active_dropdown : function(evt) {
        if (this.dd_active && $(evt.target).parents(this.jNavigation.selector).size() == 0) {
            $(this.dd_active).slideUp(this.settings.close_speed);
            this.dd_active = null;
        }
    },

    init : function(params) {
        var default_options = { 
            subnav_class : 'dropdown-subnav',
            open_speed   : 'normal',
            close_speed  : 'fast',
            on_click     : function(){},
            toggle_img   : { 
                path  : (trapeze.media_path + "images/btn-toggle-dropdown.png"), 
                label : "toggle sub-items",
                extra_class : ""
            },
            selector     : null
        };
        this.settings = $.extend({}, default_options, params);
        this.jNavigation = $(this.settings.selector);

        $(this.jNavigation).children("li").each(this.build_from_list.bind(this));

        $("html").click(this.close_active_dropdown.bind(this));
    }
});

