var timeout	= 500;
var closetimer = 0;
var lastMenuItem = null;
var subMenuTopOffset = 110;

function doMenu() {
	var p = window.location.href;
	var menu = $('mainMenu');
	var links = menu.getElementsByTagName("a");
	for(var i = 0; i < links.length; i++) {
		if(links[i].href.toLowerCase() == p.toLowerCase())
			$(links[i]).className = 'menuselected';
	}
}

function showSubMenu(menuId, parentId) {

    cancelCloseTimer();

    if (lastMenuItem)
        lastMenuItem.style.display = 'none';

    // set the submenu's position relative to the position of the FLOORPLANS element
    if ($(menuId)) {
        Element.extend($('mainMenu'));
        var parentLeft = $('mainMenu').cumulativeOffset()[0];
        var left = $(parentId).cumulativeOffset()[0] - parentLeft;
        lastMenuItem = $(menuId);
        lastMenuItem.style.top = subMenuTopOffset + 'px';
        lastMenuItem.style.left = left + 'px';
        lastMenuItem.style.display = '';
    }
    return false;
}

function hideSubMenu() {
    if (lastMenuItem)
        lastMenuItem.style.display = 'none';
}

function closeTimer() {
    closetimer = window.setTimeout(hideSubMenu, timeout);
}

function cancelCloseTimer() {
    if (closetimer) {
        window.clearTimeout(closetimer);
        closetimer = null;
    }
}

// trims spaces from start and end of a string
function trimString(sVal) 
{
	while(sVal.charAt(sVal.length-1)==' ')
		sVal=sVal.substring(0,sVal.length-1);

	while(sVal.charAt(0)==' ')
		sVal=sVal.substr(1,sVal.length);

	return sVal;
}

// email checker
function isEmailFormatCorrect(strAddr) 
{ 
	var ptn = /^[\w\d._%-]+@[\w\d._%-]+\.[\w\d._%-]{2,4}$/;
	return ptn.test(strAddr);
}

// left in here for compatibility with some of the old DW stuff
function MM_jumpMenu(targ, selObj, restore) { //v3.0
    eval(targ + ".location='" + selObj.options[selObj.selectedIndex].value + "'");
    if (restore) selObj.selectedIndex = 0;
}

Event.observe(window, 'load', doMenu);
