/**
 * Default Drop Menu.
 * These functions can be used to create a (dropdown) menu from a list of items.
  * An UL element that is inside an LI-element will be shown on mouse over of the LI element,
 * the UL element is hidden on mouse out of the parent LI element.
 * Usage: Give the root of a menu the className 'dropMenu':
 * <ul className="dropMenu">
 *   <li><a>item 1</a></li>
 *   <li><a>item 2</a>
 *       <ul>
 *         <li><a>sub item 1</a></li>
 *       </ul>
 *   </li>
 * </ul>
 */

/**
 mnuShowHide()
 * Shows/hides the childList in an LI element.
 */
function mnuShow(elem){
    obj = getSubItem(elem);
    obj.style.display = "block";
    obj.parentNode.firstChild.className = 'itemOn';
}

function mnuHide(elem){
    obj = getSubItem(elem);
    obj.style.display = "none";
    obj.parentNode.firstChild.className = '';
}

function getSubItem(li){
    var liChilds = li.childNodes.length;
    for (j = 0; j < liChilds; j++) {
        if (li.childNodes[j].nodeType == 1 && li.childNodes[j].tagName.toLowerCase() == "ul") {
            return (li.childNodes[j]);
        }
    }
}

function getObj(name){
    if (document.getElementById) {
        return (document.getElementById(name));
    }
    else
        if (document.all) {
            return (document.all[name]);
        }
        else
            if (document.layers) {
                return (document.layers[name]);
            }
}

function prepare(ul){
    var length = ul.childNodes.length;
    var i, j, liChilds, sub;
    for (i = 0; i < length; i++) {
        if (ul.childNodes[i].nodeType != 1) {
            continue;
        }
        liChilds = ul.childNodes[i].childNodes.length;
        if (liChilds <= 1) {
            continue;
        }
        sub = null;
        for (j = 0; j < liChilds; j++) {
            if (ul.childNodes[i].childNodes[j].nodeType == 1 && ul.childNodes[i].childNodes[j].tagName.toLowerCase() == "ul") {
                ul.childNodes[i].onmouseover = function(){
                    mnuShow(this);
                };
                ul.childNodes[i].onmouseout = function(){
                    mnuHide(this);
                };
                prepare(ul.childNodes[i].childNodes[j]);
                break;
            }
        }
    }
}

function prepareMenu(menuId){
    var menu = getObj(menuId);
    prepare(menu);
}

function mnuShowHide(e, showHide) {
    if (! e) { var e = window.event; }
    var elem = e.target ? e.target : e.srcElement;
    var i;

    while (! pDomApi.hasClassName(elem, mnuClassName)) {

        if (elem.tagName.toLowerCase() == 'li') {

            for (i=0; i < elem.childNodes.length; i++) {
                if (elem.childNodes[i].nodeType == 1 && elem.childNodes[i].tagName.toLowerCase() == 'ul') {
                    pDomApi.setClassName(elem.childNodes[i], showHide, showHide == 'show' ? 'hide' : 'show');
                    break;
                }
            }
        }
        elem = elem.parentNode;
    }
}

if (typeof actionAtacher == 'undefined') {
    var actionAttacher = new PrezentDomApi.ActionAttacher();
    pDomApi.addEvent(window, 'domload', function() { actionAttacher.attach(); });
}

var mnuClassName = 'mainMenu';

actionAttacher.addTagEvent(new PrezentDomApi.TagEvent('ul', mnuClassName, 'mouseover', function (e) { mnuShowHide(e, 'show'); }));
actionAttacher.addTagEvent(new PrezentDomApi.TagEvent('ul', mnuClassName, 'mouseout', function (e) { mnuShowHide(e, 'hide'); }));

