/*
Click Menu v0.2
2007 - Magic Wand
*/

(function($) {
    $.fn.clickmenu = function(settings) {
        var ClickMenu = function(elem, settings) {
            this._elem = elem;
            this._settings = settings ? settings : [];
            this._hideAllTimer = null;
            this._mouseInMenu = false;
        }
        
        ClickMenu.prototype = {
            create: function() {
                this.hideAll();
                var oThis = this;
                $(document).click(function() { if(!oThis.getMouseInMenu()) oThis.hideAll(); });
                $('.expander').mouseover(function() { oThis.onMouseOver(this); return false; }).next().hover(
                    function() {
                        oThis.endHideAll();
                        oThis.setMouseInMenu(true);
                    },
                    function() {
                        oThis.beginHideAll();
                        oThis.setMouseInMenu(false);
                    }
                );
            },
            
            setMouseInMenu: function(status) {
                this._mouseInMenu = status;
            },
            
            getMouseInMenu: function() {
                return this._mouseInMenu;
            },
            
            beginHideAll: function() {
                this.startHideAllTimer();
            },
            
            endHideAll: function() {
                this.stopHideAllTimer();
            },
            
            startHideAllTimer: function() {
                this.stopHideAllTimer();
                var oThis = this;
                this._hideAllTimer = setTimeout(function() {oThis.hideAll()}, 2000);
            },
            
            stopHideAllTimer: function() {
                if(!this._hideAllTimer) return;
                clearTimeout(this._hideAllTimer);
                this._hideAllTimer = null;
            },
            
            hideAll: function() {
                $('.expander', this._elem).next().hide();
                if(this._settings.normal) $('.expander', this._elem).attr('src', this._settings.normal);
                this.endHideAll();
            },
            
            onClick: function(expander) {
                var hidden = $(expander).next().is(':hidden');
                this.hideAll();
                if(!hidden) return;
                this.showByExpander(expander);
            },
            
            onMouseOver: function(expander) {
                if($(expander).next().is(':visible')) return;
                this.hideAll();
                this.showByExpander(expander);
            },
            
            showByExpander: function(expander) {
                var coords = $(expander).coords();
                $(expander).next().css('top', coords.y + $(expander).height() + 3).css('left', coords.x - 3).show();
                if(this._settings.expanded) $(expander).attr('src', this._settings.expanded);
            }
            
        }
        
        return this.each(function() {
            var menu = new ClickMenu(this, settings);
            menu.create();
        });
    }
})(jQuery);
