/**
 * Drop Menu with delay.
 * 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>
 */

var mnuTimeout = [];
var mnuTimeoutTime = 800;	// set timeout time for hiding submenu's
var mnuClassName = 'dropMenu';	// Classname, not an id!!

/**
 * mnuShowHide()
 * Shows/hides the childList in an LI element.
 */
function mnuShowHide(e, showHide, root) {
    var elem = e.target ? e.target : e.srcElement;
    var i, done = false;
    var hideStack = [];
    var key = '' + root.offsetTop + '_' +root.offsetLeft;		// we need an unique key for each menu

    while (! pDomApi.hasClassName(elem, mnuClassName) && ! done) {

    	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') {

                	if (showHide == 'show') {
                		window.clearTimeout(mnuTimeout[key]); // otherwise this item could be hidden again after we show it
                		mnuInit(elem.parentNode, true);	// hide other submenu's for this menu
                		pDomApi.setClassName(elem.childNodes[i], showHide, 'hide');
                		return;
                	} else {
                		hideStack.push(getShowHideFunction(elem.childNodes[i], showHide));
                	}
                    break;
                }
            }
        }
        elem = elem.parentNode;
    }

    if (hideStack.length > 0) {
    	mnuTimeout[key] = window.setTimeout(hideStack.pop(), mnuTimeoutTime);
    }

}

// Returns the function that can be used to show and hide menu's
function getShowHideFunction(elem, showHide) {
	return function() {
		pDomApi.setClassName(elem, showHide, showHide == 'show' ? 'hide' : 'show');
	};
}

function mnuInit(elem, skipInitial) {
    var i, items;

    if (! skipInitial) {

    	var a, span;

	items = elem.getElementsByTagName('li');

	for (i = items.length - 1; i >= 0; i--) {
		pDomApi.setClassName(items[i], 'item');
	}
    }
    items = elem.getElementsByTagName('ul');

    for (i = items.length - 1; i >= 0; i--) {
    	pDomApi.setClassName(items[i], 'hide');

    	if (! skipInitial) {
    		pDomApi.setClassName(items[i].parentNode, 'folder', 'item');
    	}
    }
}

var actionAttacher = pDomApi.getActionAttacher();

actionAttacher.addTagEvent(new PrezentDomApi.TagEvent('ul', mnuClassName, 'mouseout', function (e) { mnuShowHide(e, 'hide', this); }));
actionAttacher.addTagEvent(new PrezentDomApi.TagEvent('ul', mnuClassName, 'mouseover', function (e) { mnuShowHide(e, 'show', this); }));
actionAttacher.addTagEvent(new PrezentDomApi.TagEvent('ul', mnuClassName, 'attach', mnuInit));