function getParentOffset(el){
	var pOffset = 0;
	while(el != this.topParent.parentNode){
		el = el.parentNode;
		if((el.tagName == 'LI') && (el.offsetTop)){
			pOffset += parseInt(el.offsetTop);
		}
		if((el.tagName == 'UL')&&(el.style.top)){
			topint = parseInt(el.style.top);
			pOffset += topint;
		}
	}
	return pOffset ;
}
function selectBoxes(inid,visibility){
	var containerObj = document.getElementById(inid);
	var selects = containerObj.getElementsByTagName("SELECT");
	for(s=0; s<selects.length; s++){
		selects[s].style.visibility = visibility;

	}
}
function getFlyoutOffset(flyoutObj){
	var vscroll = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
	var flyoutHeight = flyoutObj.offsetHeight;
	var flyoutTop = getParentOffset(flyoutObj) - vscroll;
	var flyoutBottom = flyoutTop + flyoutHeight;
	/*
	window.innerHeight
		Firefox
		Safari
	document.documentElement.clientHeight
		Firefox
		IE6(PC)
	document.body.clientHeight
		IE5.1(PC)
		IE5.5(PC)
		IE5.2(Mac)
	*/
	var windowHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
	if (navigator.userAgent.indexOf("Mac") != -1 && typeof document.body.topMargin != "undefined") {
		var windowHeight = windowHeight - document.body.topMargin;
    }
	if (navigator.userAgent.indexOf("Safari") != -1) {
		// this has to change once i figure out how to get the body margin in safari with js
		var windowHeight = windowHeight - 15;
    }
	var diffOffset = flyoutBottom > windowHeight ? (flyoutBottom - windowHeight) - ((flyoutBottom - windowHeight)*2) : 0;

	if (navigator.userAgent.indexOf("Mac") > -1 && !window.getComputedStyle && flyoutObj.parentNode.id == ""){
		// this annoying if statement is here because there seems to be no way to get the computed style of the parent node and retrieve it's border-width
		diffOffset -= 1;
	}
	
	if(window.getComputedStyle){
		borderOffset = window.getComputedStyle(flyoutObj.parentNode,null).getPropertyValue("border-top-width");
		borderOffset = borderOffset.substring(0,borderOffset.indexOf("px"));
		if(borderOffset > 0){
			diffOffset -= borderOffset;
		}
	}

	return diffOffset + 'px';
}
function showFlyout(flyoutObj){
	flyoutObj.style.display = 'block';
	flyoutObj.style.top = getFlyoutOffset(flyoutObj);
	flyoutObj.style.visibility = 'visible';
	
	if(is_ie6down && is_win){
		var ar = flyoutObj.getElementsByTagName('li');
		for(var i=0;i<ar.length;i++){
			ar[i].style.height = '1px';
			if(ar[i].className.indexOf('navarrow') != -1){
				ar[i].style.lineHeight = '11px';
			}			
		}
	}		
}
function updateClassName(obj,str){
	var rx = new RegExp('( ?)' + str + '( ?)');
	var rxex = rx.exec(obj.className);
	// incase the string to be removed is in between two other classes we'll need to replace the removed string with a space
	var replace = !rxex ? '' : rxex[1]&&rxex[2] == " " ? ' ' : '';
	if(rxex == null){
		obj.className = !obj.className ? str : obj.className+' '+str;
	} else {
		 obj.className = obj.className.replace(rx, replace)
	}
}
function hasNested(liObj){
	// check for nested UL and apply mouseover and mouseout functions if exists
	var hasul = liObj.getElementsByTagName("UL");
	var nestedObj = hasul[0];

	if(hasul.length > 0){
		var theLI = hasul[0];
		liObj.onmouseover = function(){ showFlyout(theLI); updateClassName(liObj,'over'); selectBoxes('content','hidden'); }
		liObj.onmouseout = function(){ theLI.style.display = 'block'; theLI.style.visibility = 'hidden'; updateClassName(liObj,'over'); selectBoxes('content','visible'); }
		var a    = liObj.getElementsByTagName('A')[0];
		updateClassName(a,'navarrow');
		
		if(a.href.charAt(a.href.length-1) == "#"){
			a.style.cursor = "default";
			a.onclick      = function nn() { return false };
		}
		
		theLI.style.display = 'none';
		theLI.style.visibility = 'hidden';

		return true;
	} else {
		return false;
	}
}
function initFlyouts(el){
	this.topParent = el;
	el.innerHTML.replace(/\n/g,'');
	
	if(hasNested(el)){
		updateClassName(el,'flyout');
		listitems = el.getElementsByTagName('LI');
		for(var k=0;k<listitems.length;k++){
			hasNested(listitems[k]);
		}
	}
	
	if (is_ie5 && !is_mac){
		document.write('<style type="text/css">.flyout li,#lnav .flyout a.navarrow{width:184px!important;}</style>');
	}
	
	return null;
}
function Flyout(id){initFlyouts(document.getElementById(id));}

/* UIL release 1.3.1 */
