function findElPos(el){
	var x=0,y=0;
	var temp = el;
	while (temp.offsetParent){ //Looping parent elements to get the offset of them as well
		temp=temp.offsetParent; 
		x+=temp.offsetLeft
		y+=temp.offsetTop;
	}
	x+=el.offsetLeft;
	y+=el.offsetTop;
	return [x,y,x+el.offsetWidth,y+el.offsetHeight];
}

function MainMenuHide(id) {
    var el=document.getElementById(id);
    var elDrop = document.getElementById(el.id + 'Drop');
    if (elDrop && (el.className.indexOf(' MainMenuOver')<0)) { 
        setSelVis(findElPos(elDrop),-1);
    }
    el.className=el.className.replace(/ MainMenuOver/,"");
}

function MainMenuMouseOver(el) {
    var elDrop;
    if (el.className.indexOf(' MainMenuOver')<0)
        // don't process submenus if they are already visible.
        elDrop=document.getElementById(el.id + 'Drop');

    if (elDrop) elDrop.style.visibilty='hidden';
    
    el.className+=' MainMenuOver';
    if (elDrop) {
        var elDropBG = document.getElementById(el.id + 'DropBG');
        elDrop.style.height='';
        var iHeight = elDrop.offsetHeight;
        elDrop.style.height= '0px';
        elDropBG.style.height= '0px';
        elDrop.style.visibility='';

        elDrop.style.top = findElPos(el)[3] + 'px';
        elDropBG.style.top = findElPos(el)[3] + 'px';

        if (!elDrop.style.width) {
            elDrop.style.width = (elDrop.offsetWidth-20) + 'px';
            elDropBG.style.width = elDrop.style.width;
        }
        
        var dropPos = findElPos(elDrop);
        setSelVis(dropPos,1);
        
        var sDrop = 'ExpandMenu("' + el.id + '",' + iHeight + ');';
        setTimeout(sDrop,100);
    }
}

function ExpandMenu(id,iHeight) {
    var el=document.getElementById(id);
    if (el.className.indexOf(' MainMenuOver')>=0) {
        var elDrop = document.getElementById(id + 'Drop');
        if (elDrop) {
            var ulDrop = elDrop.firstChild;
            var elDropBG = document.getElementById(id + 'DropBG');
            var iNewHeight = parseInt(elDrop.style.height.replace(/px/,'')) + 30;
            if (iNewHeight>=iHeight) {
                iNewHeight = iHeight;
                ulDrop.style.top='';
            } else {
                ulDrop.style.top = (iNewHeight - iHeight) + 'px';
            }
            
            elDropBG.style.height = iNewHeight + 'px';
            elDrop.style.height = iNewHeight + 'px';

            if (iNewHeight<iHeight) {
                var sDrop = 'ExpandMenu("' + id + '",' + iHeight + ');';
                setTimeout(sDrop,25);            
            }
        }
    }
}

function MainMenuMouseOut(el) {
    var sMouseOut = 'MainMenuHide("' + el.id + '");';
    // the delay is to give the user time to move their mouse from the active item to the sub menu. 
    setTimeout(sMouseOut,10);
}

var aSelVis=new Array();
var iSelVisElLength = 0;
function setSelVis(aMenuPos,iVis) {
	// Hide any select boxes, OBJECTs, or EMBEDs covered up by this menu.
	var x,y;
	if (aSelVis.length==0) loadaSelVis();
	
	for (x=0;x<aSelVis.length;x++){
		if (
			(aSelVis[x][1][0]<=aMenuPos[2]) && 
			(aSelVis[x][1][2]>=aMenuPos[0]) && 
			((aSelVis[x][1][1]<=aMenuPos[3]) || (aMenuPos[3]==0) || (iVis<0)) && 
			(aSelVis[x][1][3]>=aMenuPos[1])
			) {
			
			// track the number of menus covering up this object.
			if (aSelVis[x][2]<0) 
				aSelVis[x][2] = 0;
			aSelVis[x][2] += iVis;
			
			if (aSelVis[x][2]>0) {
				aSelVis[x][0].style.visibility="hidden";
			} else {
				aSelVis[x][0].style.visibility="visible";
			}
		} 
	}
}

function loadaSelVis() {
	aSelVis = new Array();
    
    // We only need to do this in ie 6 or lower.
    var agent = navigator.userAgent.toLowerCase();
    if (agent.indexOf("msie 6")<0 && agent.indexOf("msie 5")<0) return;

	var el;
	var aTags = ['SELECT','OBJECT','EMBED'];
	var x,y;
	var aEls;
	for (x=0;x<aTags.length;x++) {
		aEls=document.getElementsByTagName(aTags[x]);
		for (y=0;y<aEls.length;y++) {
			aSelVis[aSelVis.length] = [aEls[y],findElPos(aEls[y]),0];
		}
	}
}

