/**
 * @author Falko Zander, www.falko-zander.de
 */

/**
 * @param id String
 */
function cMenu(id) {
    cEvent.call(this);
    
    this.DOM_id = id;
    this.DOM_elem = $(id);
    if (this.DOM_elem == null) return null;

    this.mainUL = this.DOM_elem.getElementsByTagName('ul')[0];
    this.mainLIs = {};
    this.isOpen = false;
    this.closeTimeout = null;

    for (var i = j = 0; i < this.mainUL.childNodes.length; i++) {
        if (this.mainUL.childNodes[i].nodeName.toLowerCase() == 'li') {
            var li = this.mainUL.childNodes[i];
            li.id = 'js_SubMenu_' + (++ j);

            var subMenu = li.getElementsByTagName('ul');
            var hasSubMenu = (subMenu.length > 0);
            this.mainLIs[li.id] = {
                hasSubMenu: hasSubMenu,
                subMenuContent: (hasSubMenu == true) ? subMenu[0].getElementsByTagName('li') : null
            };

            var THIS = this;
            if (hasSubMenu == true) {
                this.add_event(li, 'onmouseover', function(elem) { THIS.open(elem); });
                this.add_event(li, 'onmouseout', function() { THIS.delayedClose(); });
            } else {
                this.add_event(li, 'onmouseover', function() { THIS.close(); });
            }

            li.style.cursor = "pointer";
        }
    }
}
cMenu.prototype = new cEvent();
cMenu.superClass = cEvent.prototype;

/**
 * @param DOM_li Element
 */
cMenu.prototype.open = function(DOM_li) {
    if (this.isOpen) {
        this.close();
    }

    if (this.mainLIs[DOM_li.id].hasSubMenu == true) {
        var posLI = getAbsoluteDimensions(DOM_li);
    
        var html = '<table>';
        for (var i = 0; i < this.mainLIs[DOM_li.id].subMenuContent.length; i++) {
            html += "<tr><td style='padding:2px'>"      //$NON-NLS-1$ 
                 + this.mainLIs[DOM_li.id].subMenuContent[i].innerHTML 
                 + '</td></tr>';
        }
        html += '</table>';
        
        var div = document.createElement('div');
        div.id = 'js_openSubMenu';
        div.style.left = posLI.offsetLeft + 'px';
        div.style.top = posLI.offsetTop + 20 + 'px';
        document.body.appendChild(div);

        div.innerHTML = html;
        var THIS = this;
        this.add_event(div, 'onmouseover', function() { THIS.stopClosing(); });
        this.add_event(div, 'onmouseout', function() { THIS.delayedClose(); });
    }

    this.isOpen = true;
}

cMenu.prototype.close = function() {
    if (this.isOpen == true) {
        document.body.removeChild($('js_openSubMenu'));
        this.stopClosing();
        this.isOpen = false;
    }
}

cMenu.prototype.delayedClose = function() {
    if (this.closeTimeout == null) {
        var THIS = this;
        var f = function() { THIS.close(); };
        this.closeTimeout = window.setTimeout(f, 100);
    }
}

cMenu.prototype.stopClosing = function() {
    if (this.closeTimeout != null) {
        window.clearTimeout(this.closeTimeout);
        this.closeTimeout = null;
    }
}
