/* $Id: lightbox.js,v 1.3 2009/04/20 19:18:52 petr Exp $ */

// ----- Configuration -----
/*
var lightboxConfig = {
	loadingImage: '../lightbox/loading.gif', // preloader
	closeKey: 'x', // 
	closeText: 'kliknutím okno zavřete (stiskněte X)',
	closeShow: true, // close button in details
	closeEndShow: true, // close click to image is not prev/next button
	prevKey: 'p',
	prevText: 'předchozí (klikněte nebo stiskněte P)',
	nextKey: 'n',
	nextText: 'následující (klikněte nebo stiskněte N)',
	imagesText: 'obrázek ', // note 'image 5/14'
	navigationShow: true, // show navigation 'image 5/14'
	dimensionShow: false, // show dimension after navigation
	detailsShow: true // show note image and navigation
};
*/
var lightboxConfig = {
	loadingImage: '../lightbox/loading.gif', // preloader
	closeKey: 'x', // 
	closeText: 'kliknutím okno zatvorite (stlačtě X)',
	closeShow: true, // close button in details
	closeEndShow: true, // close click to image is not prev/next button
	prevKey: 'p',
	prevText: 'predchozí (kliknitě alebo stlačtě P)',
	nextKey: 'n',
	nextText: 'následujúci (klikněte alebo stlačtě N)',
	imagesText: 'obrázok ', // note 'image 5/14'
	navigationShow: true, // show navigation 'image 5/14'
	dimensionShow: false, // show dimension after navigation
	detailsShow: true // show note image and navigation
};

var lightboxAnchors = new Array(); // array of links
var lightboxCurrent = 0; // current item


// ----- Get page scroll - returns array with x,y page scroll values -----
function getPageScroll(){
	var yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {
		yScroll = document.body.scrollTop;
	}
	return new Array('',yScroll);
}


// ----- Get page size - returns array with page width, height and window width, height -----
function getPageSize(){
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight) {
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else {
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) {
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	var pageHeight = yScroll < windowHeight ? windowHeight : yScroll;
	var pageWidth = xScroll<windowWidth ? windowWidth : xScroll;
	return new Array(pageWidth, pageHeight, windowWidth, windowHeight);
}


// ----- Pauses code execution for specified time -----
function pause(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}


// ----- Gets keycode -----
function getKey(e){
	var keycode;
	if (e == null) {
		keycode = event.keyCode;
	} else {
		keycode = e.which;
	}
	var key = String.fromCharCode(keycode).toLowerCase();
	if(key == lightboxConfig.closeKey){ hideLightbox(); }
	if(key == lightboxConfig.prevKey){ prevLightbox(); }
	if(key == lightboxConfig.nextKey){ nextLightbox(); }
}


// ----- Listen key -----
function listenKey () {	document.onkeypress = getKey; }


// ----- Show lightbox centers displays, preloads images -----
function showLightbox(objLink) {
	// prep objects
	var objOverlay = document.getElementById('overlay');
	var objLightbox = document.getElementById('lightbox');
	var objCaption = document.getElementById('lightboxCaption');
	var objImage = document.getElementById('lightboxImage');
	var objLoadingImage = document.getElementById('loadingImage');
	var objLightboxDetails = document.getElementById('lightboxDetails');
	var objPrev = document.getElementById('prevLink');
	var objNext = document.getElementById('nextLink');
	var objClose = document.getElementById('closeLightbox');

	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	lightboxCurrent = parseInt(objLink.getAttribute('name'));

	objPrev.style.display = lightboxCurrent<=0 ? 'none' : 'block';
	objNext.style.display = lightboxAnchors.length-lightboxCurrent-1<=0 ? 'none' : 'block';

	if (objLoadingImage) {
		objLoadingImage.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - objLoadingImage.height) / 2) + 'px');
		objLoadingImage.style.left = (((arrayPageSize[0] - 20 - objLoadingImage.width) / 2) + 'px');
		objLoadingImage.style.display = 'block';
	}

	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';

	var imgPreload = new Image();
	imgPreload.onload = function() {
		objImage.src = objLink.href;
		var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - imgPreload.height) / 2);
		var lightboxLeft = ((arrayPageSize[0] - 20 - imgPreload.width) / 2);
		objLightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
		objLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";
		objPrev.style.width = imgPreload.width / 2;
		objPrev.style.height = imgPreload.height;
		objNext.style.width = imgPreload.width / 2;
		objNext.style.height = imgPreload.height;
		objClose.style.width = imgPreload.width;
		objClose.style.height = imgPreload.height;
		objLightboxDetails.style.width = imgPreload.width + 'px';
		objCaption.style.display = 'block';
		var text = '';
		if(objLink.getAttribute('title')){
			text = '<b>'+objLink.getAttribute('title')+'</b>';
		}
		if(lightboxConfig.navigationShow) {
			text = text + '<br />'+lightboxConfig.imagesText+(lightboxCurrent+1)+'/'+lightboxAnchors.length;
			if(lightboxConfig.dimensionShow) {
				text = text + ' ['+imgPreload.width+'x'+imgPreload.height+' px]';
			}
		}
		objCaption.innerHTML = text;
		if (navigator.appVersion.indexOf("MSIE")!=-1){
			pause(250);
		}
		if (objLoadingImage) {	objLoadingImage.style.display = 'none'; }
		objLightbox.style.display = 'block';
		arrayPageSize = getPageSize();
		objOverlay.style.height = (arrayPageSize[1] + 'px');
		listenKey();
		return false;
	}

	objLightbox.style.display = 'none';
	imgPreload.src = objLink.href;

}


// ----- Hide lightbox -----
function hideLightbox() {
	var objOverlay = document.getElementById('overlay');
	var objLightbox = document.getElementById('lightbox');
	objOverlay.style.display = 'none';
	objLightbox.style.display = 'none';
	document.onkeypress = '';
}


// ----- Next image lightbox -----
function nextLightbox() {
	if(lightboxAnchors.length-lightboxCurrent-1>=1) {
		showLightbox(lightboxAnchors[lightboxCurrent+1]);
	}
}


// ----- Previous image lightbox -----
function prevLightbox() {
	if(lightboxCurrent-1>=0) {
		showLightbox(lightboxAnchors[lightboxCurrent-1]);
	}
}


// ----- Initialize lightbox -----
function initLightbox()
{

	if (!document.getElementsByTagName) { return; }
	var anchors = document.getElementsByTagName("a");

	for (var i=0; i<anchors.length; i++){
		var anchor = anchors[i];

		if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "lightbox")) {
			lightboxAnchors.push(anchor);
			anchor.setAttribute('name',lightboxAnchors.length-1);
			anchor.onclick = function () {showLightbox(this); return false;}
		}
	}

	var objBody = document.getElementsByTagName("body").item(0);
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','overlay');
	objOverlay.onclick = function () {hideLightbox(); return false;}
	objBody.insertBefore(objOverlay, objBody.firstChild);

	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	var imgPreloader = new Image();
	imgPreloader.onload=function(){
		var objLoadingImageLink = document.createElement("a");
		objLoadingImageLink.setAttribute('href','#');
		objLoadingImageLink.onclick = function () {hideLightbox(); return false;}
		objOverlay.appendChild(objLoadingImageLink);
		var objLoadingImage = document.createElement("img");
		objLoadingImage.src = lightboxConfig.loadingImage;
		objLoadingImage.setAttribute('id','loadingImage');
		objLoadingImageLink.appendChild(objLoadingImage);
		imgPreloader.onload=function(){};
		return false;
	}
	imgPreloader.src = lightboxConfig.loadingImage;

	var objLightbox = document.createElement("div");
	objLightbox.setAttribute('id','lightbox');
	objBody.insertBefore(objLightbox, objOverlay.nextSibling);

	var objImage = document.createElement("img");
	objImage.setAttribute('id','lightboxImage');
	objLightbox.appendChild(objImage);

	var lightboxClose = document.createElement("a");
	lightboxClose.setAttribute('id','closeLightbox');
	lightboxClose.setAttribute('href','#');
	lightboxClose.setAttribute('title',lightboxConfig.closeText);
	lightboxClose.onclick = function () {hideLightbox(); return false;}
	objLightbox.appendChild(lightboxClose);
	if(!lightboxConfig.closeEndShow) {
		lightboxClose.style.display = 'none';
	}

	var objPrev = document.createElement("a");
	objPrev.setAttribute('id','prevLink');
	objPrev.setAttribute('href','#');
	objPrev.setAttribute('title',lightboxConfig.prevText);
	objPrev.onclick = function () {prevLightbox(); return false;}
	objLightbox.appendChild(objPrev);

	var objNext = document.createElement("a");
	objNext.setAttribute('id','nextLink');
	objNext.setAttribute('href','#');
	objNext.setAttribute('title',lightboxConfig.nextText);
	objNext.onclick = function () {nextLightbox(); return false;}
	objLightbox.appendChild(objNext);

	var objLightboxDetails = document.createElement("div");
	objLightboxDetails.setAttribute('id','lightboxDetails');
	objLightbox.appendChild(objLightboxDetails);

	var objCaption = document.createElement("div");
	objCaption.setAttribute('id','lightboxCaption');
	objLightboxDetails.appendChild(objCaption);

	if(lightboxConfig.detailsShow) {
		if(lightboxConfig.closeShow) {
			var objClose = document.createElement("a");
			objClose.setAttribute('id','closeLink');
			objClose.setAttribute('href','#');
			objClose.setAttribute('title',lightboxConfig.closeText);
			objClose.onclick = function () {hideLightbox(); return false;}
			objLightbox.appendChild(objClose);
		}
	} else {
		objLightboxDetails.style.display = 'none';
	}
}


// ----- Adds event to window.onload -----
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
			oldonload();
			func();
		}
	}
}


// ----- Start lightbox -----
addLoadEvent(initLightbox);	

