var es, gp, img, w, h, /*scrollx,*/ scrolly, windowx, windowy;

var Viewport = { 
	windowX: function() {
		return (document.documentElement && document.documentElement.clientWidth) || window.innerWidth || self.innerWidth || document.body.clientWidth;
	},
	windowY: function() {
		return (document.documentElement && document.documentElement.clientHeight) || window.innerHeight || self.innerHeight || document.body.clientHeight;
	},
	scrollX: function() {
		return (document.documentElement && document.documentElement.scrollLeft) || window.pageXOffset || self.pageXOffset || document.body.scrollLeft;
	},
	scrollY: function() {
		return (document.documentElement && document.documentElement.scrollTop) || window.pageYOffset || self.pageYOffset || document.body.scrollTop;
	},
	pageX: function() {
		return (document.documentElement && document.documentElement.scrollWidth) ? document.documentElement.scrollWidth : (document.body.scrollWidth > document.body.offsetWidth) ? document.body.scrollWidth : document.body.offsetWidth;
	},
	pageY: function() {
		return (document.documentElement && document.documentElement.scrollHeight) ? document.documentElement.scrollHeight : (document.body.scrollHeight > document.body.offsetHeight) ? document.body.scrollHeight : document.body.offsetHeight;
	}
};

function PopupOpen(imgSrc) {
	img = new Image();
	img.onload = ImageLoaded;
	img.src = imgSrc;
}

function ImageLoaded() {
	h = img.height;
	w = img.width;
	var offY = Math.round(Viewport.scrollY() + (Viewport.windowY() - h) / 2 - 20);
	var offX = Math.round(Viewport.scrollX() + (Viewport.windowX() - w) / 2);
	gp.innerHTML = '<div id="popup-overlay" style="height: ' + Viewport.pageY() + 'px;" onclick="javascript:PopupClose();"></div><div id="popup-window" style="margin-top: ' + offY + 'px; margin-left: ' + offX + 'px; width: ' + w + 'px; height: ' + h + 'px;"><a href="javascript:PopupClose();"><img src="' + img.src + '" alt="" /></a></div>';
}

function PopupClose() {
	gp.innerHTML = '';
}

function WindowDetect() {
	if (/*(scrollx != Viewport.scrollX()) ||*/ (scrolly != Viewport.scrollY()) || (windowx != Viewport.windowX()) || (windowy != Viewport.windowY())) {
		var offY = Math.round(Viewport.scrollY() + (Viewport.windowY() - h) / 2 - 20);
		var offX = Math.round(Viewport.scrollX() + (Viewport.windowX() - w) / 2);
		if (document.getElementById('popup-window')) {
			document.getElementById('popup-window').style.marginTop = offY + "px";
			document.getElementById('popup-window').style.marginLeft = offX + "px";
			/*scrollx = Viewport.scrollX();*/
			scrolly = Viewport.scrollY();
			windowx = Viewport.windowX();
			windowy = Viewport.windowY();
		}
	}
}

function Init(imgLoc) {
	gp = document.getElementById('popup');
	/*es = document.getElementById('links').getElementsByTagName('a');
	for (var i = 0; i < es.length; i++) {
		es[i].href = "javascript:PopupOpen('" + es[i].href + "');";
	}*/
	/*scrollx = Viewport.scrollX();*/
	scrolly = Viewport.scrollY();
	windowx = Viewport.windowX();
	windowy = Viewport.windowY();
	PopupOpen(imgLoc);
	/*setInterval("WindowDetect()",50);*/
}

/*window.onload = Init;*/



