addLoadListener(activatePopups);
addLoadListener(activateClosures);

var keepOpen = 0;
var openWindow = '';

function activatePopups() {
	var popupLinks = getElementsByClassName(document, 'a', 'popup_link');
	for (i=0; i < popupLinks.length; i++) {
		popupLinks[i].onclick = function() {
			
			if (openWindow != '') {
				document.getElementById(openWindow).style.display = 'none';	
			}
			keepOpen = 1;
			popupID = this.id + '_popup';
			openWindow = popupID;
			var thisPopup = document.getElementById(popupID);
			var thisLocationLeft = this.parentNode.offsetLeft;
			var popupLocationLeft = this.parentNode.offsetWidth;
			var thisLocationBottom = this.parentNode.parentNode.offsetTop;
			//alert(popupLocationLeft);
			//alert(thisLocationLeft);
			//alert(thisLocationBottom);
			thisPopup.style.display = 'block';
			
			
			
			var bodyTop = window.pageYOffset;
			var popTop = thisLocationBottom - 180;
			
			
			if ((bodyTop - popTop) > 0) {
				thisPopup.style.top = (thisLocationBottom + 210) + 'px';
			} else {
				thisPopup.style.top = popTop + 'px';
			}
			
			
			if ((thisLocationLeft + popupLocationLeft + 40) > 440) {
				thisPopup.style.left = ((thisLocationLeft + popupLocationLeft + 30) - 480) + 'px';
			} else {
				thisPopup.style.left = (thisLocationLeft + popupLocationLeft + 40) + 'px';
			}
			return false;
		}
		
		popupLinks[i].onmouseover = function() {
			if (keepOpen == 0) {
				popupID = this.id + '_popup';
				var thisPopup = document.getElementById(popupID);
				var thisLocationLeft = this.parentNode.offsetLeft;
				var popupLocationLeft = this.parentNode.offsetWidth;
				var thisLocationBottom = this.parentNode.parentNode.offsetTop;
				thisPopup.style.display = 'block';
				
				
				
				var bodyTop = window.pageYOffset;
				var popTop = thisLocationBottom - 180;
				
				
				if ((bodyTop - popTop) > 0) {
					thisPopup.style.top = (thisLocationBottom + 210) + 'px';
				} else {
					thisPopup.style.top = popTop + 'px';
				}
				
				
				
				
				if ((thisLocationLeft + popupLocationLeft + 40) > 440) {
					thisPopup.style.left = ((thisLocationLeft + popupLocationLeft + 30) - 480) + 'px';
				} else {
					thisPopup.style.left = (thisLocationLeft + popupLocationLeft + 40) + 'px';
				}
			}
		};
		
		popupLinks[i].onmouseout = function() {
			if (keepOpen == 0) {
				popupID = this.id + '_popup';
				var thisPopup = document.getElementById(popupID);
				thisPopup.style.display = 'none';
			}
		};
	}
}

function activateClosures() {
	var closeLinks = getElementsByClassName(document, 'a', 'popup_close');
		for (i=0; i < closeLinks.length; i++) {
			closeLinks[i].onclick = function() {
				this.parentNode.parentNode.style.display = 'none';
				keepOpen = 0;
				return false;
			};
		}
}