var lightboximgs;
function initLightbox() {
	i = 0;
	lightboximgs = new Array();
	lista = document.getElementsByTagName("a");
	for(i in lista) {
		if(lista[i].rel && lista[i].rel.match(/lightbox/)) {
			lista[i].num = i;
			lista[i].onclick = function () {
				doLightbox(this);
				return false;
			}
			lightboximgs[i] = lista[i];
		}
	}
}
var activenum = 0;
var imageContainer, outerImageContainer, lightbox;
var imgs, theImg;
function doLightbox(who) {
	if(typeof(lightbox) == "undefined") {
		createLightBox();
		imgs = new Array();
	}
	
	overlay.style.display = "block";
	lightbox.style.display = "block";
	
	activenum = who.num;
	imgs[who.num] = new Image();
	
	imgs[who.num].onload = function() {
		doBox(this);
	}
	imgs[who.num].src = who.href;
}
function doBox(who) {
	if(who.width) {
		outerImageContainer.style.width = (who.width + 20) + "px";
		outerImageContainer.style.height = (who.height + 20) + "px";
		
		t = document.documentElement.clientHeight - who.height - 70;
		t = t > 0 ? t / 2 : 0;
		t = t + document.documentElement.scrollTop;
		
		lightbox.style.top = t + "px";
		
		
		if(!theImg) {
			theImg = document.createElement("img");
			imageContainer.appendChild(theImg);
		}
		theImg.src = who.src;
		theImg.style.opacity = 0;
		theImg.style.filter = "alpha(opacity=0)";
		
		showInfo();
		
		setTimeout("showImage()", 39);
	}
	
	//lightbox.style.backgroundColor = "pink";
}
function showImage() {
	imgop = parseInt(theImg.style.opacity * 100);
	imgop = imgop + 10;
	if(imgop >= 100) {
		imgop = 99;
	} else {
		setTimeout("showImage()", 39);
	}
	theImg.style.opacity = imgop / 100;
	theImg.style.filter = "alpha(opacity=" + imgop + ")";
}

function showInfo() {
	num = parseInt(activenum) + 1;
	numberDisplay.innerHTML = "Image " + num + " of " + lightboximgs.length;
	
	if(lightboximgs[activenum].title) {
		caption.innerHTML = lightboximgs[activenum].title;
	} else {
		caption.innerHTML = "";
	}
}
var goAndDo;
function doImage(what) {
	num = parseInt(activenum) + what;
	
	if(lightboximgs[num]) {
		goAndDo = lightboximgs[num];
		hideImage();
	}
}
function hideImage() {
	imgop = parseInt(theImg.style.opacity * 100);
	imgop = imgop - 10;
	if(imgop <= 0) {
		imgop = 0;
		doLightbox(goAndDo);
	} else {
		setTimeout("hideImage()", 39);
	}
	theImg.style.opacity = imgop / 100;
	theImg.style.filter = "alpha(opacity=" + imgop + ")";
}

var caption, numberDisplay, nextimage;
function createLightBox() {
	
	overlay = createElementAdd("overlay", "div", document.body);
	h = window.outerHeight > document.body.scrollHeight ? window.outerHeight : document.body.scrollHeight;
	overlay.style.height = "100%";
	overlay.style.opacity = ".5";
	overlay.style.filter = "alpha(opacity=50)";
	//overlay.style.height = "100%";
	
	lightbox = createElementAdd("lightbox", "div", document.body);
	lightbox.style.top = "100px";
	
	outerImageContainer = createElementAdd("outerImageContainer", "div", lightbox);
	imageContainer = createElementAdd("imageContainer", "div", outerImageContainer);
	imagePrefNext = createElementAdd("imagePrefNext", "div", outerImageContainer);
	imageDataContainer = createElementAdd("imageDataContainer", "div", outerImageContainer);
	imageData = createElementAdd("imageData", "div", imageDataContainer);
	imageDetails = createElementAdd("imageDetails", "div", imageData);
	
	caption = createElementAdd("caption", "span", imageDetails);
	numberDisplay = createElementAdd("numberDisplay", "span", imageDetails);
	
	
	previmage = createElementAdd("previmage", "a", imagePrefNext);
	previmage.innerHTML = "< < previous image";
	previmage.href = "#";
	previmage.onclick = function() {
		doImage(-1);
	}
	
	nextimage = createElementAdd("nextimage", "a", imagePrefNext);
	nextimage.innerHTML = "next image > >";
	nextimage.href = "#";
	nextimage.onclick = function() {
		doImage(1);
	}
	
	bottomNav = createElementAdd("bottomNav", "div", imageData);
	bottomNavClose = createElementAdd("bottomNavClose", "a", bottomNav);
	bottomNavClose.innerHTML = "close";
	bottomNavClose.onclick = function() { closeLightbox() };
}
function closeLightbox() {
	overlay.style.display = "none";
	lightbox.style.display = "none";
}

function createElementAdd(who, what, where) {
	itm = document.createElement(what);
	itm.id = who;
	where.appendChild(itm);
	return itm;
}