/*
	Litebox JS by Alexander Shabuniewicz - http://aether.ru
	2006-08-18

	inspired by Lightbox JS - Lokesh Dhakar - http://www.huddletogether.com
	and portfolio script of Cameron Adams - http://portfolio.themaninblue.com
*/

var imgNext;
var imgPrev;
var nPopupActive = 0;

function showPopup( nWidth, yAxis ) {
	if ( nPopupActive == 1 ) {
		return;
	} else {
		nPopupActive = 1;
	}
	var theBody = document.getElementsByTagName('body')[0];
	var pageCoords = getPageCoords();

	var theShadow = document.createElement('div');
	theShadow.id = 'popshadow';
	theShadow.style.height = (pageCoords[1] + 'px');
	theShadow.className = 'on';
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
	theBody.insertBefore(theShadow, theBody.firstChild);

	var theLoading = document.createElement('div');
	theLoading.id = 'poploading';
	theLoading.style.top = parseInt(pageCoords[2] + (pageCoords[0] - 55) / 2) + 'px';
	theLoading.onclick = function() { closePopup(); }
	theShadow.appendChild(theLoading);

	var theBox = document.createElement('div');
	theBox.id = 'popbox';
//	theBox.className = 'popup-outer';
	theBox.style.width = nWidth + 'px';
//	theBox.style.marginTop = parseInt(pageCoords[2] + (pageCoords[0] -  500) / 2) + 'px';
	theBox.style.marginTop = yAxis + 'px';
//	theBox.innerHTML = '<div style="padding:10px 10px; text-align:center;"><img src="/i/loading.gif" width="400" height="400" border="0"></div>';


	theShadow.removeChild(theLoading);
	theShadow.appendChild(theBox);

	document.onkeypress = getPopKey;
	return false;
}

function getPageCoords() {
	var coords = [0, 0, 0]; // height of window, document, scroll pos
	// all except IE
	if (window.innerHeight) {
		coords[0] = window.innerHeight;
		coords[2] = window.pageYOffset;
	}
	// IE 6 Strict
	else if (document.documentElement && document.documentElement.clientHeight != 0) {
		coords[0] = document.documentElement.clientHeight;
		coords[2] = document.documentElement.scrollTop;
	}
	else if (document.body) {
		coords[0] = document.body.clientHeight;
		coords[2] = document.body.scrollTop;
	}

	var test1 = document.body.scrollHeight;
	var test2 = document.body.offsetHeight;
	if (test1 > test2) {
		coords[1] = document.body.scrollHeight;
	} else {
		coords[1] = document.body.offsetHeight;
	}
	if (coords[1] < coords[0]) coords[1] = coords[0];

	return coords;
}

function closePopup() {
	var theBody = document.getElementsByTagName('body')[0];
	var theBox = document.getElementById('popbox');
	if (theBox) theBox.style.display = 'none';
	var theShadow = document.getElementById('popshadow');
	if (theShadow) theShadow.style.display = 'none';
	theBody.removeChild(theShadow);

	selects = document.getElementsByTagName('select');
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = 'visible';
	}
	document.onkeypress = '';
	imgPrev = imgNext = '';
	nPopupActive = 0;
	return false;
}

function getPopKey(e) {
	var arrowImg;

	if (!e) var e = window.event;
	var keycode = e.keyCode ? e.keyCode : e.which;

	switch (keycode) {
//		case 32: // spacebar
		case 27: // esc
			closePopup();
			break;
//		case 37: // <-
//			arrowImg = imgPrev ? imgPrev : '';
//			break;
//		case 39: // ->
//			arrowImg = imgNext ? imgNext : '';
	}
//	if (arrowImg) { closePopup(); showPopup(arrowImg); }
}

function initPopbox() {
	if (!document.getElementsByTagName) { return; }
	var anchors = document.getElementsByTagName('a');

	for (i=0,len=anchors.length; i<len; i++){
		var anchor = anchors[i];
		if (anchor.getAttribute('href') && (anchor.getAttribute('rel') == 'lightbox')) {
			anchor.onclick = function() { showPopup(this); return false; }
		}
	}
	anchor = null;
}

