// this script closes popups opened by popup.js and drop-down-list.js

// current position of mouse on the PAGE
var pageLeft = -1;
var pageTop = -1;

// current postion of mouse on the SCREEN
var screenLeft = -1;
var screenTop = -1;

document.onclick = checkClick;

// have we clicked on the glossary?
var glossClicked = 0;


	// catch where the user clicked on the screen
	function getClickPos(e){
        
        	if (!e) {
	  		e = window.event; // works on IE, but not NS (we rely on NS passing us the event)
	  	}
		
		if (e)
		{ 
		      screenLeft = e.clientX;
		      screenTop = e.clientY;

		      pageLeft = getBody().scrollLeft + screenLeft;
		      pageTop = getBody().scrollTop + screenTop;
	      
  		}	
        }
        
        
        // check if one of the popups is showing
        function checkClick(e){
        	getClickPos(e);
		var glossDiv = document.getElementById("glossaryDiv");
		// Ignore the first click on the glossary link...
		if(glossDiv && glossDiv.style.visibility == "visible"){
			if(glossClicked > 1){
				calcDivClick(glossDiv);
			}
			if(glossClicked < 3){
				glossClicked++;
			}
	  	}
	  	var dropmenuDiv = document.getElementById("dropmenudiv");
        	if (dropmenuDiv && dropmenuDiv.style.visibility=="visible"){
        		calcDivClick(dropmenuDiv);
        	}
	}
	
	
	// did user click outside our popup? If so, close it.
	function calcDivClick(div){
		var divWidth = div.offsetLeft + div.clientWidth;
		var divHeight = div.offsetTop + div.clientHeight;
		
		if(pageLeft > div.offsetLeft && pageTop > div.offsetTop){
			if(pageLeft > divWidth || pageTop > divHeight)
			{
				hidePopup(div);	
			}
			
		}
		else{
			hidePopup(div);
		}
		
	}

	function hidePopup(div){
    		div.style.visibility = "hidden";
    		glossClicked = 0;
    
	}