var ie = 6;
var lastA;

$ = function (id) { return document.getElementById(id); }
$C = function (el, cl) { el.setAttribute("class", cl); el.setAttribute("className", cl); }
$ID = function (el) { return el.getAttribute('id'); }
$GC = function (el) { 
	if (el.getAttribute('class') != null) 
		return el.getAttribute('class'); 
	else if (el.className)
		return el.className; 
	else
		return null;
}
function removeChildren(el) { if (el != null) while (el.hasChildNodes()) el.removeChild(el.firstChild); }
window.onload = function () {
	var body = document.getElementsByTagName('body')[0];

	// append lightbox
	var div = document.createElement('div'); div.setAttribute('id', 'view');
	div.setAttribute('title', 'Click anywhere in the area to exit');
	body.appendChild(div);

	var div = document.createElement('div'); div.setAttribute('id', 'visible');
	body.appendChild(div);

	imgs = document.getElementsByTagName('img');
	for(var i = 0; i < imgs.length; i++) {
		var img = imgs[i];
		var a = img.parentNode.nodeName == "A" ? img.parentNode : null;
		if (a) {
			a.onclick = function() {
				return preview(this);
			}
		}
	}
	$('view').onclick = function () {
		$('view').style.display = "none";
		$('visible').style.display = "none";
		clearLast();
	}

	if (navigator.userAgent.toLowerCase().indexOf('msie') != -1 && ie != 7) {
		var newMargin = document.body.offsetHeight + 200;
		if (newMargin < 250) newMargin = 250;
		$('me').style.marginBottom = newMargin + 'px';
		$('view').style.height = 
			(document.body.offsetHeight > document.body.scrollHeight 
				? document.body.offsetHeight : document.body.scrollHeight) + "px";
	} 
//	else if (document.body) {
//		$('me').style.marginBottom = document.body.offsetHeight + 'px';
//	} else 
//		$('me').style.marginBottom = window.scrollMaxY + 'px';
}

function preview(a) {
	clearLast();
	lastA = a;
	var previewA = document.createElement('a');
	previewA.setAttribute('href', '#');
	previewA.onclick = function(event) {
		var width = window.innerWidth ? window.innerWidth : document.body.offsetWidth;
		var e = event || window.event;
		return next(a, e && width && e.clientX < width / 2);
	};
	previewA.setAttribute('title', 'Click on image to view next');
	var img = document.createElement('img');
	img.setAttribute('src', 'imgs/decor/loading.gif');
	img.setAttribute('id', 'loading');
	previewA.appendChild(img);

	var p = document.createElement('p');
	p.appendChild(document.createTextNode(a.getAttribute('title')));
	previewA.appendChild(p);

	removeChildren($('visible'));

	if ($ID(a.parentNode) == "me")
		$C(a.parentNode, 'selected me');
	else if ($GC(a.parentNode) && $GC(a.parentNode).indexOf('spacer') != -1)
		$C(a.parentNode, 'selected spacer');
	else
		$C(a.parentNode, 'selected');

	$('visible').appendChild(previewA);
	$('visible').style.display = "block";

	if (navigator.userAgent.toLowerCase().indexOf('msie') != -1 && ie != 7) {
		var newTop = document.body.scrollTop > document.documentElement.scrollTop 
			? document.body.scrollTop
			: document.documentElement.scrollTop;
			
		$('visible').style.top = (newTop + 260) + 'px';
	} 
	$('view').style.display = "block";

	var preloadImg = new Image();
	preloadImg.onload = function () {
		var img = $('loading');
		img.parentNode.insertBefore(preloadImg, img);
		img.parentNode.removeChild(img);
//		img.width = this.width;
//		img.height = this.height;
//		img.setAttribute('id', '');
//		img.setAttribute('src', this.getAttribute('src'));
	};
	preloadImg.setAttribute('src', a.getAttribute('href'));

	return false;
}
function clearLast() {
	if (lastA) {
		if ($ID(lastA.parentNode) == 'me') 
			$C(lastA.parentNode, 'me');
		else if ($GC(lastA.parentNode).indexOf('spacer') != -1) 
			$C(lastA.parentNode, 'spacer');
		else 
			$C(lastA.parentNode, '');
	}
}
function next(a, backward) {
	clearLast();
	var nextLi = backward ? a.parentNode.previousSibling : a.parentNode.nextSibling;
	if (nextLi == null)
		nextLi = backward ? a.parentNode.parentNode.lastChild : a.parentNode.parentNode.firstChild;
	if ($GC(nextLi) == "hr")
		nextLi = backward ? nextLi.previousSibling : nextLi.nextSibling;

	return preview(nextLi.firstChild);
}
