/**
 * @PROJECT:   mygosuMenu 1.0.1
 * @COPYRIGHT: (c) 2003 Cezary Tomczak
 * @LINK:      http://gosu.pl/software/mygosumenu.html
 * @LICENSE:   BSD
 */

var menuTimeout = 400;

var menuDebug     = false
var menuDebugTest = false
var menuDebugErrorCount = 0
var menuDebugErrorLimit = 5

var menuSections = new Array()
var menuCountHide = new Array();

if (!menuDebug) {
    window.onerror = function() { return true }
}

function menuRaiseError(msg, e) {
    if (menuDebug && menuDebugErrorCount <= menuDebugErrorLimit) {
        menuDebugErrorCount++
        if (!e) {
            alert('menuRaiseError() failed, argument 2 is not an object')
            return
        }
        var s = ''
        s += 'Error: ' + e.name + '\n'
        s += 'Message: ' + msg + '\n'
        s += 'More Info: ' + (e.message ? e.message : e.description) + '\n'
        if (e.fileName && e.lineNumber) {
            s += 'File: ' + e.fileName + '\n'
            s += 'Line: ' + e.lineNumber + '\n'
        }
        alert(s)
    }
}

function menuShow(section, elements) {
    // hide other sections
    for (var i = 0; i < menuSections.length; i++) {
        if (menuSections[i] != section) {
            menuHide(menuSections[i], menuCountNodes(menuSections[i]))
        }
    }
    // show current section
    for (var i = 1; i <= elements; i++) {
        try {
            document.getElementById(section + '-' + i).style.visibility = 'visible'
        } catch (e) {
            menuRaiseError('menuShow() failed, visibility, element = ' + section + '-' + i, e)
        }
    }
}

function menuHide(section, elements) {
    for (var i = 1; i <= elements; i++) {
        try {
            document.getElementById(section + '-' + i).style.visibility = 'hidden'
        } catch (e) {
            menuRaiseError('menuHide() failed, visibility, element = ' + section + '-' + i, e)
        }
    }
}

function menuTryHide(section, elements, countHide) {
    if (countHide != menuCountHide[section]) {
        return
    }
    menuHide(section, elements)
}

function menuCountNodes(element) {
    if (document.getElementById(element)) {
        ret = 0
        nodes = document.getElementById(element).childNodes.length
        for (var i = 0; i < nodes; i++) {
            if (document.getElementById(element).childNodes[i].nodeType == 1) {
                ret++
            }
        }
        return ret
    } else {
        throw new Error('menuCountNodes() failed, element not found, element = ' + element)
    }
}

function menuInitSection(section) {
    try {
        var elements = menuCountNodes(section)
    } catch (e) {
        menuRaiseError('menuInitSection() failed, could not count nodes, element = ' + section, e)
    }
    for (var i = 0; i <= elements; i++) {
        if (i == 0) {
            var s = section + '-top';
        } else {
            var s = section + '-' + i
        }
        try {
            document.getElementById(s).onmouseover = function() {
                menuShow(section, elements)
                menuCountHide[section]++
            }
        } catch (e) {
            menuRaiseError('menuInitSection() failed, onmouseover, element = ' + s, e)
        }
        try {
            document.getElementById(s).onmouseout = function() {
                setTimeout("menuTryHide('" + section + "', " + elements + ", " + menuCountHide[section] + ")", menuTimeout)
            }
        } catch (e) {
            menuRaiseError('menuInitSection() failed, onmouseout, element = ' + s, e)
        }
    }
}

var menuSectionCnt = 0
var menuBoxCnt = 0

function menuMakeId(nodes) {
    for (var i = 0; i < nodes.length; i++) {
        switch (nodes[i].className) {
            case 'top':
                menuSectionCnt++
                menuBoxCnt = 0
                nodes[i].id = 'menu-' + menuSectionCnt + '-top'
                break;
            case 'section':
                nodes[i].id = 'menu-' + menuSectionCnt
                menuSections[menuSections.length] = nodes[i].id
                break;
            case 'box':
                menuBoxCnt++
                nodes[i].id = 'menu-' + menuSectionCnt + '-' + menuBoxCnt
                break;
        }
        if (nodes[i].childNodes) {
            menuMakeId(nodes[i].childNodes)
        }
    }
}

function menuInit() {
    if(document.getElementById('menu')) {
        try {
            menuMakeId(document.getElementById('menu').childNodes)
        } catch (e) {
            menuRaiseError('menuMakeId() failed', e)
        }
    } else {
        var error = new Error('menu element is missing')
        menuRaiseError('menuInit() failed', error)
    }
    for (var i = 0; i < menuSections.length; i++) {
        menuCountHide[menuSections[i]] = 0
    }
    for (var i = 0; i < menuSections.length; i++) {
        menuInitSection(menuSections[i])
    }
}