/*
	Litebox JS by Alexander Shabuniewicz - http://eye.loveline.ru
	2006-04-11

	inspired by Lightbox JS - Lokesh Dhakar - http://www.huddletogether.com
	and portfolio script of Cameron Adams - http://portfolio.themaninblue.com
*/

var imgNext;
var imgPrev;

function showBox(theTarget) {
	var theBody = document.getElementsByTagName('body')[0];
	var pageCoords = getPageCoords();

	var theShadow = document.createElement('div');
	theShadow.id = 'shadow';

	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 = 'loading';
	theLoading.style.top = parseInt(pageCoords[2] + (pageCoords[0] - 22) / 2) + 'px';
	theLoading.onclick = function() { closeBox(); }
//	theShadow.appendChild(theLoading);

popWidth = 400;
popHeight = 280;



var dataSourceObj = document.getElementById(theTarget.id + '_data');

		var theBox = document.createElement('div');
		theBox.id = 'litebox';
		theBox.style.width = popWidth + 10 + 'px';
		theBox.style.height = popHeight + 10 + 'px';
		theBox.style.marginTop = parseInt(pageCoords[2] + (pageCoords[0] - popHeight - 50) / 2) + 'px';

                theBox.innerHTML = dataSourceObj.innerHTML;

		theShadow.appendChild(theBox);
//		document.onkeypress = getKey;




}

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 closeBox() {
	document.getElementById('shadow').style.display = 'none';
	document.getElementById('litebox').style.display = 'none';

	selects = document.getElementsByTagName('select');
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = 'visible';
	}
	document.onkeypress = '';
	imgPrev = imgNext = '';
	return false;
}

function getKey(e) {

	if (!e) var e = window.event;
	var keycode = e.keyCode ? e.keyCode : e.which;
/*
	switch (keycode) {
		case 32: // spacebar
			closeBox();
			break;
	}
	return false;
*/
}

document.onclick = function(e) {
	var target = e ? e.target : window.event.srcElement;

	while (target && !/^(a|body)$/i.test(target.nodeName)) {
		target = target.parentNode;
	}
	if (target && target.getAttribute('href') && (target.getAttribute('rel') == 'lightbox')) {
		showBox(target);
		return false;
	}
}
