var _menu = {
    open_menu : null,
    class_prefix : "",
    onmenuopen : function(){},
    onmenuclose : function(){},
    onpreparemenuclose : function(){},
    oncancelclose : function(){},
    onmarkitem : function(){},
    
    open : function( oCE, prefix ) { /* CE = container element */
        this.onmenuopen();
        
        // Close open menu
        this.prepare_close(1);
        
        if( oCE !== null && oCE !== this.open_menu ) {
			this.close();
            oCE["class_prefix"] = ( prefix )? prefix : " open"
            oCE.className += oCE["class_prefix"];
            oCE.onmouseout = this.prepare_close;
			oCE.onmouseover = this.cancel_close;
            
            var oChilds = getChildElements( oCE, null, 2 );
            for( var i=0; i < oChilds.length; i++ ) {
                oChilds[i].onmouseover = this.cancel_close;
			}
        } else
            oCE = null;
        
        this.open_menu_waiter = oCE;        
        setTimeout("_menu.setOpenMenu()", 100);
    },
    
    close : function() {
        this.onmenuclose();
        
        // close and remove last opened li/menu
        if( this.open_menu ) {
            this.open_menu.className = String(this.open_menu.className).replace( this.open_menu["class_prefix"], "" );
            
            // Unset the reference to the open menu
            this.open_menu = null;
        }
    },
    
    prepare_close : function() {
        _menu.onpreparemenuclose();
        
        time = ( arguments[0] )? parseInt(arguments[0]) : 333;
        
		_menu.cancel_close(); // clear old close timeouts
		_menu.timer = setTimeout( "_menu.close();", time );
    },
    
    cancel_close : function() {
        _menu.oncancelclose();
        
        if( _menu.timer )
            clearTimeout( _menu.timer );
    },
    
    setOpenMenu : function() {
        this.open_menu = this.open_menu_waiter;
        this.open_menu_waiter = null;
    },
    
    markItem : function( oInput ) {
        var return_obj = null;
        
        if( oInput ) {
            var oParent = oInput.parentNode;
            var input_type = String(oInput.type).toLowerCase();
            
            if( oParent && input_type == "checkbox" || input_type == "radio" ) {
                if( oInput.checked ) {
                    oParent.className += " marked";
                    return_obj = oParent;
                } else {
                    oParent.className = String(oParent.className).replace(/\s?marked/g, "");
                }
            }
        }
        
        this.onmarkitem();
        
        return return_obj;
    }
}
