/**
 *
 * by danny, 1stOmni
 * based on www/modules/cms/dynamicmenu.js
 *
 * MENU ITEM PROPERTIES:
 *
 * menuVisible:bool
 *              - current state of the menu
 * menuVisibleSchedule:bool
 *							- next visibility (highlight) state (usually there is some delayed function)
 * menuSwitchTimeout:timeout
 *							- scheduled item.fceSwitch
 * menuInitialized:bool
 *							- was the menu initialized?
 * fceUnhighlight:function
 *							- unhighlight the menu 
 * fceSwitch:function
 *							- switch the menu according to current menuVisibleSchedule property
 * submenu:HTMLListElement
 *							- The UL element with the submenu
 *							
 */
function switchSubPullDown(item, show, event, position) {
    if (item.menuVisibleSchedule == show) return; // Already scheduled (bubbling optimization)
    item.menuVisibleSchedule=show;

    initPullDownItem(item, position);
		
    if (show) {
	hideSelectBoxes();
	switchMenuClassName(item, 'mouseOutChild', 'mouseOverChild');
    } else {
	switchMenuClassName(item, 'mouseOverChild', 'mouseOutChild');
	showSelectBoxes();				
    }

    // Delay the actual submenu showing/hidding because of the MOUSEOVER/MOUSEOUT problem when leaving DOM elements...
    item.menuSwitchTimeout=setTimeout(item.fceSwitch, show ? 250 : 150);		
}

function initPullDownItem(item, position) {
    if (item.menuSwitchTimeout) {
	clearTimeout(item.menuSwitchTimeout); // If there is alrady something schedule cancel it
	item.menuSwitchTimeout=0;
    }

    // One time initialization
    if (item.menuInitialized) return;
    item.menuInitialized=true;

    item.fceSwitch=function() {
	item.menuSwitchTimeout=0;
	if (!item.submenu || item.menuVisibleSchedule == item.menuVisible) return; // already visible/hidden
	item.menuVisible=item.menuVisibleSchedule;
				
	if (item.menuVisibleSchedule) {
	    switchMenuClassName(item.submenu, 'mouseOut', 'mouseOver');		
	} else {
	    switchMenuClassName(item.submenu, 'mouseOver', 'mouseOut');
	}

	var offsetWidth, offsetHeight;
	if (position) {
	    switch (position) {
	    case 'tl':
		offsetWidth=0;
		offsetHeight=0 - item.submenu.clientHeight;
		break;
	    case 'tr':
		offsetWidth=item.offsetWidth - item.submenu.clientWidth;
		offsetHeight=0 - item.submenu.clientHeight;
		break;
	    case 'bl':
		offsetWidth=0;
		offsetHeight=item.offsetHeight;
		break;
	    case 'br':
		offsetWidth=item.offsetWidth - item.submenu.clientWidth;
		offsetHeight=item.offsetHeight;
		break;
	    }
	} else if (item.tagName.toLowerCase() == 'span') {
	    offsetWidth=0;
	    offsetHeight=item.offsetHeight;
	} else {
	    offsetWidth=item.clientWidth;
	    offsetHeight=0;
	}
	alignBlock(item.submenu, item, offsetWidth, offsetHeight);	
	// debug('POSITION: '+getPosition(item).x+' x '+getPosition(item).y+', OFFSET: '+item.offsetWidth+' x '+item.offsetHeight);
    }

    item.submenu=findPullDown(item);
}

function findPullDown(item) {
    var localName, submenu;
    for(var i=item.childNodes.length - 1; i >= 0; i--) {
	if (item.childNodes[i].nodeType != 1) continue;
	localName=(item.childNodes[i].localName && item.childNodes[i].localName.toLowerCase()) || (item.childNodes[i].tagName && item.childNodes[i].tagName.toLowerCase());

	if (localName.match(/ul|table|iframe/)) { // We got it!
	    return item.childNodes[i];
	}
    }
    return false;
}

function switchMenuClassName(node, oldValue, newValue) {
    //window.console.log([node, oldValue, newValue]);				
    if (!node) return; // MSIE
    if (!node['styleCache'+newValue]) { // Calculate the new className using RegExp and store it for later use...
	var oldClass=(node.getAttribute('class') || node.className);
	node['styleCache'+newValue]=oldClass.replace(new RegExp(oldValue+"|"+newValue, 'g'), ''); // Remove all - we use RegExp because there can be unknown class names added in XSL...
	node['styleCache'+newValue]+=' '+newValue;
    }
    node.setAttribute('class', node['styleCache'+newValue]);
    node.className=node['styleCache'+newValue]; // MSIE
}		

