/*
	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 p;
var allI;
var curI;

function showGallery(path,allImg,curImg) {
	p = path;
	allI = allImg;
	curI = curImg;
	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';
	theBody.insertBefore(theShadow, theBody.firstChild);

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

	var imgPreload = new Image();
	imgPreload.onload = function() {
		var theBox = document.createElement('div');
		theBox.id = 'litebox';
		theBox.style.width = imgPreload.width + 10 + 'px';
		theBox.style.marginTop = parseInt(pageCoords[2] + (pageCoords[0] - imgPreload.height - 50) / 2) + 'px';

		var theImage = document.createElement('img');
		theImage.src = path + curImg + '.jpg';
		theImage.alt = 'Картинка ' + curImg + ' из ' + allImg;
		theImage.width = imgPreload.width;
		theImage.onclick = function() { closeBox(); }
		theImage.title = "Щелкните на картинке или нажмите ESC, чтобы закрыть";

		var theCaption = document.createElement('p');
		theCaption.innerHTML = (theImage.alt) ? theImage.alt : '';
		theCaption.innerHTML += "<em id='em'>Щелкните на картинке или нажмите ESC, чтобы закрыть</em><br/>";
		theCaption.innerHTML += "<iframe src='/comment.php?path=" + path + "&line=" + curImg + "' id='comment' width='" + imgPreload.width + "' frameborder='0'></iframe>";
		//theCaption.innerHTML += "<iframe src='/comment.php?path=" + path + "' id='comment' width='" + imgPreload.width + "' border='0'></iframe>";

		if (curImg>1) {
			var thePrevLink = document.createElement('a');
			thePrevLink.className = 'prev';
			thePrevLink.href = 'javascript: showGallery("' + path + '",' + allImg + ',' + curImg-1 + ');';
			thePrevLink.onclick = function() { closeBox(); showGallery(path,allImg,curImg-1); return false; }
			theCaption.insertBefore(thePrevLink, theCaption.firstChild);
		}
		if (curImg<allImg) {
			var theNextLink = document.createElement('a');
			theNextLink.className = 'next';
			theNextLink.href = 'javascript: showGallery("' + path + '",' + allImg + ',' + curImg+1 + ');';
			theNextLink.onclick = function() { closeBox(); showGallery(path,allImg,curImg+1); return false; }
			theCaption.insertBefore(theNextLink, theCaption.firstChild);
		}
		
		theShadow.removeChild(theLoading);
		theBox.appendChild(theImage);
		theBox.appendChild(theCaption);
		theShadow.appendChild(theBox);

		document.onkeypress = getKey;
		return false;
	}
	imgPreload.src = path + curImg + '.jpg';
}

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() {
	var theBody = document.getElementsByTagName('body')[0];
	var theBox = document.getElementById('litebox');
	if (theBox) theBox.style.display = 'none';
	var theShadow = document.getElementById('shadow');
	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 = '';
	return false;
}

function getKey(e) {
	var arrowImg;

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

	switch (keycode) {
  	case 27: // esc
		case 32: // spacebar
			closeBox();
			break;
		case 37: // <-
			if (curI-1>0) {curI = curI - 1; doEv = true};
			break;
		case 39: // ->
			if (curI+1<allI+1) {curI = curI + 1; doEv = true };
	}
	if (doEv == true) { closeBox(); showGallery(p,allI,curI);}
	return false;
}


