	
	var popupVisible = false;
	var closingPopup = false;
	
	document.onclick = function () {
		if (popupVisible == true && closingPopup == false) {
			closePopup();
		}
	}

	function setOpacity (elem, opacVal)
	{
	    elem.style.opacity = opacVal/100.0;
	    elem.style.MozOpacity = opacVal/100.0;
	    elem.style.KhtmlOpacity = opacVal/100.0;
	    elem.style.filter = "alpha(opacity=" + opacVal + ")"; 
	}
	
	function getClientOffsetFromTop (node)
	{
		var off = 0;
		
		while (node.offsetParent) {
			off += node.offsetTop;
			node = node.offsetParent;
		}
		
		return off;
	}
	
	function getClientOffsetFromLeft (node)
	{
		var off = 0;
		
		while (node.offsetParent) {
			off += node.offsetLeft;
			node = node.offsetParent;
		}
		
		return off;
	}
	
	function getScrollTop ()
	{
  		var scrOfY = 0;
  		if( typeof( window.pageYOffset ) == 'number' ) {
    		//Netscape compliant
    		scrOfY = window.pageYOffset;
  		} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    		//DOM compliant
    		scrOfY = document.body.scrollTop;
  		} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    		//IE6 standards compliant mode
    		scrOfY = document.documentElement.scrollTop;
  		}
  		return scrOfY;
	}

	
	function showPopup (imgurl, title, target)
	{
		if (popupVisible == false) {
			closingPopup = true;
			var popup = document.getElementById('popup');

			if (popup) {

				// Calculate where to place the popup
				var tx = getClientOffsetFromLeft(target);
				var ty = getClientOffsetFromTop(target);
				var tty = getScrollTop();
				
				if ( ((ty + popup.offsetHeight) > document.body.offsetHeight) && (ty - popup.offsetHeight + target.offsetHeight > tty) ) {
					ty = ty - popup.offsetHeight + target.offsetHeight;
				}
				
				
				if ((tx - popup.offsetWidth) > 200) {
					tx = (tx - popup.offsetWidth) + (target.offsetWidth);
				}
				
				
				
				
				popup.style.top = ty + 'px';
				popup.style.left = tx + 'px';
			
				setOpacity(popup, 0.0);
				popupVisible = true; // well, truish
				popup.style.visibility = 'visible';
				
				var img = document.createElement('img');
				img.alt = title;
				img.src = imgurl;
				img.id = 'popupimg';
				img.onclick = closePopup;
				
				var h2 = document.getElementById('popuptitle');
				h2.innerHTML = title;
				
				popup.appendChild(img);

				var effect = new Effect.Opacity('popup',
					{
						from: 0.0,
						to: 1.0,
						duration: 0.75,
						transition: Effect.Transitions.sinoidal,
						afterFinish: afterPopupOpen
					});
				
				var selects = document.getElementsByTagName('select');
				
				tx2 = tx + popup.offsetWidth;
				ty2 = ty + popup.offsetHeight;
				for (var i = 0; i != selects.length; i++) {
					var sx = getClientOffsetFromLeft(selects[i]);
					var sy = getClientOffsetFromTop(selects[i]);
					var sx2 = sx + selects[i].offsetWidth;
					var sy2 = sy + selects[i].offsetHeight;
					
					if (
							(sx >= tx && sy >= ty && sx <= tx2 && sy <= ty2)
						||	(sx2 >= tx && sy >= ty && sx2 <= tx2 && sy <= ty2)
						||	(sx >= tx && sy2 >= ty && sx <= tx2 && sy2 <= ty2)
						||	(sx2 >= tx && sy2 >= ty && sx2 <= tx2 && sy2 <= ty2)
						) {
						selects[i].style.visibility = 'hidden';
					}
				}
			}
		}
	}
	
	function afterPopupOpen ()
	{
		closingPopup = false;
	}
	
	function closePopup ()
	{
		if (popupVisible == true) {
			closingPopup = true;
			var effect = new Effect.Opacity('popup',
				{
					from: 1.0,
					to: 0.0,
					duration: 0.3,
					transition: Effect.Transitions.sinoidal,
					afterFinish: afterPopupClosed
				});
		}
	}
	
	function afterPopupClosed ()
	{
		var popup = document.getElementById('popup');
		var img = document.getElementById('popupimg');
		/*var shroud = document.getElementById('shroud');
		
		shroud.style.visibility = 'hidden';
		*/
		if (img && popup) {
			popup.removeChild(img);
			popup.style.visibility = 'hidden';
		}
		
		var selects = document.getElementsByTagName('select');
				
		for (var i = 0; i != selects.length; i++) {
			selects[i].style.visibility = 'visible';
		}

		popupVisible = false;
		closingPopup = false;
	}