var ua = navigator.userAgent.toLowerCase();
var av = navigator.appVersion.toLowerCase();
var mac = ( av.indexOf( 'mac' ) != -1 );
var saf = ( ua.indexOf( 'safari' ) != -1 );
var ie5x = ( document.all && document.getElementById );
var ie5mac = ( mac && ie5x );
var defaultEdgeMargin = 20;
var LastListNum = "";
var COLOR = '';

function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}
function hideall(ListNum) {
	
	divItem = document.getElementById('Lst'+ListNum);
	divItem.style.display="none";
}
function showList(ListNum,  color) {
	
	if(LastListNum == ListNum)
   		 closeDropDown();
  	else{
		closeDropDown();
		COLOR = color;
    	LastListNum = ListNum;
   		ListId = "Lst" + LastListNum;
    	if(document.getElementById(ListId)){
	    	document.getElementById(ListId).style.display = "block";	
			if(document.getElementById('dropPlus'+LastListNum)){
	    		document.getElementById('dropPlus'+ListNum).className = "dropPlusOver_" + color; 
			}
			checkHeightNew(ListNum); 
    }
  }
}	
function closeDropDown(){
 if(LastListNum!=""){
	    ListId = "Lst" + LastListNum;
    	if(document.getElementById(ListId)){
      		document.getElementById(ListId).style.display = "none";
			if(document.getElementById('dropPlus'+LastListNum)){
      			document.getElementById('dropPlus'+LastListNum).className = "dropPlus_" + COLOR;
			}
    }
    	LastListNum = ""; // set the menu ID to null 
  }
}
/* css drop downs - modified js - use this function to avoid setting height on the page level */
function checkHeightNew(ListNum){
		var list = document.getElementById('Lst' + ListNum);	
		var winHeight = calcHeight();
		var scrollOffset = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
		if(!ie5mac) {
			list.style.overflow = "auto";// overflow auto for none ie5 on MAC -- to avoid horizontal scroll bar
			list.style.height = "";
			var drop = document.getElementById('Drp' + ListNum);
			var dropPos = findPosY(drop);
			var dropHeight = drop.offsetHeight;
			var spaceBelow = winHeight - (dropPos + dropHeight - scrollOffset) - 30;
			var spaceAbove = dropPos - scrollOffset - 30;
			var lstHeight = (spaceAbove > spaceBelow)? spaceAbove: spaceBelow;
			
			lstHeight = (list.offsetHeight > lstHeight)? lstHeight: list.offsetHeight;
			list.style.height = lstHeight + "px"; // adjust the menu height according to the space available
			
			var lstTop = dropPos + dropHeight - 1;  
			list.style.top = lstTop + "px";
		
				if( spaceBelow < lstHeight){ // Display above
				list.style.top = lstTop - dropHeight - lstHeight + 2 + "px";
				//  list.style.top = dropPos - dropHeight - lstHeight + 15;			
				}
			
 	}
  
  	var listTable = document.getElementById('LstTable' + ListNum);
		if(ie5mac){
			var drop = document.getElementById('Drp' + ListNum);
			var dropPos = findPosY(drop);
			var dropHeight = drop.offsetHeight;
			var spaceBelow = winHeight - (dropPos + dropHeight - scrollOffset) - 30;
			var spaceAbove = dropPos - scrollOffset - 30;
			var lstHeight = (spaceAbove > spaceBelow)? spaceAbove: spaceBelow;
			
				lstHeight = (list.offsetHeight > lstHeight)? lstHeight: list.offsetHeight;
				list.style.height = lstHeight + "px";
		}
		
		if(saf) {
			if(listTable.offsetHeight + 3 > list.offsetHeight && list.offsetWidth <= listTable.offsetWidth + 3)
			list.style.width = list.offsetWidth + 15 + "px";
		}
		
		if(ie5mac) {
			if(listTable.offsetHeight + 2 > list.offsetHeight && list.offsetWidth <= listTable.offsetWidth + 2){
				list.style.width = list.offsetWidth + 15 + "px";
				list.style.overflow = "auto"; 
				list.style.height = "";
			}
		}    
}
function calcHeight() {
	var myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		/*Non-IE*/
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		/*IE 6+ in 'standards compliant mode'*/
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		/*IE 4 compatible*/
		myHeight = document.body.clientHeight;
	}
	return myHeight;
}
