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

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

var imgNext;
var imgPrev;

function showBox(theTarget) {
	var theBody = document.getElementsByTagName('body')[0];
	
	var pageCoords = getPageSize();
	var pageScroll = getPageScroll();
	
	var theShadow = document.createElement('div');
	theShadow.id = 'shadow';
	
	theShadow.onclick = function() {
	 	closeBox();
	 };

	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[3] - 55) / 2) + pageScroll[1]) + 'px';
	theLoading.onclick = function() { closeBox(); }
	
	theShadow.appendChild(theLoading);

	var imgPreload = new Image();
	
	imgPreload.onload = function() {

		var theImage = document.createElement('img');
		theImage.src = theTarget.href;
		theImage.alt = theTarget.title;
		
		var itemSize = getMaxSize(pageCoords[2] - 100, pageCoords[3] - 100, imgPreload.width, imgPreload.height);
		
		theImage.width	= itemSize[0];
		theImage.height = itemSize[1];
		
		theImage.onclick = function() { closeBox(); }
		theImage.title = (typeof LITEBOX_CLOSE === 'undefined' ? 'Lai aizvērtu logu, spiediet uz attēla vai ESC' : LITEBOX_CLOSE);

		var theBox = document.createElement('div');
		theBox.id = 'litebox';
		
		theBox.style.width		= itemSize[0] + 'px'; //theImage.width + 10 + 'px'; //imgPreload.width + 10 + 'px';
		theBox.style.marginTop	= parseInt(((pageCoords[3] - theImage.height) / 2) + pageScroll[1] - 15) + 'px';

		var theCaption = document.createElement('p');
		theCaption.innerHTML = (theImage.alt) ? theImage.alt : '';
		
		//theCaption.innerHTML += "<em>" + (typeof LITEBOX_CLOSE === 'undefined' ? 'Lai aizvērtu logu, spiediet uz attēla vai ESC' : LITEBOX_CLOSE) + "</em>";

		var allThumbs = new Array();
		var allLinks = document.getElementsByTagName('a');
		var linkLen = allLinks.length;
		
		for (i=0,j=0; i<linkLen; i++) {
			if (allLinks[i].rel == theTarget.rel) {
				allThumbs[j++] = allLinks[i];
			}
		}

		linkLen = allThumbs.length;
		
		for (i=0; i<linkLen; i++) {
			if (allThumbs[i].href == theTarget.href) {
				if (allThumbs[i-1]) {
					var thePrevLink = document.createElement('a');
					thePrevLink.className = 'prev';
					thePrevLink.title = allThumbs[i-1].title;
					thePrevLink.href = allThumbs[i-1].href;
					thePrevLink.rel = theTarget.rel;
					thePrevLink.onclick = function(e) { if (!e) var e = window.event; e.cancelBubble = true; if (e.stopPropagation) e.stopPropagation(); closeBox(); showBox(this); return false; }
					theCaption.insertBefore(thePrevLink, theCaption.firstChild);
					imgPrev = allThumbs[i-1];
				}
				if (allThumbs[i+1]) {
					var theNextLink = document.createElement('a');
					theNextLink.className = 'next';
					theNextLink.rel = theTarget.rel;
					theNextLink.title = allThumbs[i+1].title;
					theNextLink.href = allThumbs[i+1].href;
					theNextLink.onclick = function(e) { if (!e) var e = window.event; e.cancelBubble = true; if (e.stopPropagation) e.stopPropagation(); closeBox();  showBox(this); return false; }
					theCaption.insertBefore(theNextLink, theCaption.firstChild);
					imgNext = allThumbs[i+1];
				}
			}
		}

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

		document.onkeypress = getKey;
		return false;
	}
	

	imgPreload.src = theTarget.href;
	
	function cent() {
		var theBox = document.getElementById('litebox');
		if (theBox) {
			var pageCoords = getPageSize();
			var pageScroll = getPageScroll();
			theBox.style.marginTop = parseInt( ( (pageCoords[3] - $(theBox).outerHeight()) * 0.5) + pageScroll[1], 10) + 'px';
		}
	}
	
	window.onscroll=cent;
	window.onresize=cent;
}

function getMaxSize(maxWidth, maxHeight, imageWidth, imageHeight) {
	var r = Math.min(Math.min(maxWidth, imageWidth) / imageWidth, Math.min(maxHeight, imageHeight) / imageHeight);
	
	return [Math.round(r * imageWidth), Math.round(r * imageHeight)];
};

function getPageScroll() {
	return [$(window).scrollLeft(), $(window).scrollTop()];
}

function getPageSize(){
	return [$(document).width(), $(document).height(), $(window).width(), $(window).height()];
}

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;

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

function initLitebox() {
	if (!document.getElementsByTagName) { return; }

	var anchors = document.getElementsByTagName('a');

	for (i=0,len=anchors.length; i<len; i++){
		var anchor = anchors[i];
		var rel 	= anchor.getAttribute('rel') == undefined ? '' : anchor.getAttribute('rel');
		
		if (anchor.getAttribute('href') && (rel == 'lightbox' || rel.substring(0, 8) == 'lightbox')) {
			anchor.onclick = function() { closeBox(); showBox(this); return false; }
		}
	}
	anchor = null;
}


$(document).ready(function() {
	initLitebox();
});

