var onload_events = new Array();

function add_to_onload(event)
{
	onload_events.push(event);
	window.onload = function() { 
		for (var x = 0; x < onload_events.length; x ++) {
			eval(onload_events[x]);
		}
	}
}

function add_popups()
{
	var lnks = document.getElementsByTagName("A");
	for (var x = 0; x < lnks.length; x ++) {
		var rel = lnks[x].getAttribute("rel");
		if (rel && rel.search(/^popup/) != -1) {
			lnks[x].onclick = function() {
				var pstr = this.getAttribute("rel").replace(/^popup_?/, "");
				window.open(this.href, "", pstr);
				return false;
			}
		}
	}
}

add_to_onload("add_popups()");



siteBaseUrl = '/reosac/';


function closePopup() {
	e = document.getElementById('imgPopup');
	if (e){
		e.parentNode.removeChild(e);
	} 
}

function popupKeyListener() {document.onkeypress = processKeyPress; }

function processKeyPress(e){
	var e = window.event  || e;
	try {
		var key = e.keyCode || e.which;
	} catch (e){
		var key = 0;
	}
	//Esc or x (lowercase)
	if (key == 27 || key == 120) closePopup();
}

function attachOnClickImage(id, imgPath , altText, imgWidth, imgHeight){
	element = document.getElementById(id);
	element.style.cursor = 'pointer';
	var imageText = altText || '';		
	var imgWidth = Number(imgWidth);
	var imgHeight = Number(imgHeight);		
	element.removeAttribute("href");
	element.onclick = function (){
		//check if this element exists.
		e = document.getElementById('imgPopup');
		if (!e){
			popupKeyListener();
			var myDiv = document.createElement("DIV");
			myDiv.id = "imgPopup";
			myIframe = document.createElement('iframe');
			myIframe.src = siteBaseUrl+'blank.php?image='+encodeURIComponent(imgPath)+'&altText='+encodeURIComponent(imageText);			
			myIframe.scrolling = 'no';
			myIframe.width = imgWidth;
			myIframe.height = imgHeight+45;					
			myIframe.frameBorder = "0";
			//Append
			myDiv.appendChild(myIframe);
			myDimensions = getWindowDimensions();
			myWidth = myDimensions[0];
			myDiv.style.width = imgWidth;
			myDiv.style.left = (myWidth / 2) - (imgWidth / 2) + "px";
			document.body.appendChild(myDiv);
		}
		return false;
	}
}
function getWindowDimensions(){
	
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return [myWidth, myHeight];
}

