var imgNext;
var imgPrev;

function showBox(theTarget) {
	var theBody = document.getElementsByTagName('body')[0];
	var pageCoords = getPageCoords();

	var theShadow = document.createElement('div');
	theShadow.id = 'shadow';
	if (navigator.appName=='Microsoft Internet Explorer'&& parseFloat(navigator.appVersion)<=6) {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[2] + (pageCoords[0] - 55) / 2) + 'px';
	theShadow.appendChild(theLoading);
	var theLoadingBtn = document.createElement('div');
	theLoadingBtn.id = 'loadingbtn';	
	theLoadingBtn.onclick = function() { closeBox(); }
	theLoading.appendChild(theLoadingBtn);
	
	var imgPreload = new Image();
	imgPreload.onload = function() {
		
		var theBox = document.createElement('div');
		theBox.id = 'litebox';		
		theBox.className = 'on';
		theBody.appendChild(theBox);
		
		var theOIC = document.createElement('div');
		theOIC.id = 'outerImageContainer';		
		theOIC.style.width = imgPreload.width + 24 + 'px';		
		theOIC.style.marginTop = parseInt(pageCoords[2] + (pageCoords[0] - imgPreload.height-20) / 2) +  'px';
	
		var theLTop = document.createElement('div');
		var theRTop = document.createElement('div');		
		var theTop = document.createElement('div');
				
		theLTop.className = 'tl-lbox';
		theTop.className = 't-lbox';
		theTop.style.width = imgPreload.width + 4 + 'px';
		theRTop.className = 'tr-lbox';
		
		var theInIC = document.createElement('div');
		theInIC.id = 'inImageContainer';
		
		var theRmid = document.createElement('div');				
		theRmid.className = 'mr-lbox';		
		
		var theLmid = document.createElement('div');				
		theLmid.className = 'ml-lbox';
		
		var theIC = document.createElement("div");
		theIC.setAttribute('id','imageContainer');		
		
		var theIBox = document.createElement("div");
		theIBox.setAttribute('id','imageBox');	
		theIBox.id = 'imageBox';		
		theIBox.style.height=imgPreload.height + 'px';
		
		var theHoverNav = document.createElement("div");
		theHoverNav.setAttribute('id','hoverNav');
		theHoverNav.style.height=imgPreload.height + 'px';
		
		var theImage = document.createElement('img');
		theImage.src = theTarget.href;
		theImage.alt = theTarget.title;
		theImage.width = imgPreload.width;		
	
		var theLBottom = document.createElement('div');
		var theBottom = document.createElement('div');		
		var theRBottom = document.createElement('div');
				
		theLBottom.className = 'bl-lbox';
		theBottom.className = 'b-lbox';
		theBottom.style.width = imgPreload.width + 4 + 'px';
		theRBottom.className = 'br-lbox';		
		
		var percent= theImage.height/pageCoords[0];
		if (percent>1) {	
		var height = parseInt(theImage.height/percent);
		var width=parseInt(theImage.width/percent);	
		theImage.style.height =height+'px';
		theImage.style.width =width+'px';		
		theTop.style.width = width+4+'px';
		theHoverNav.style.height=height + 'px';
		theIBox.style.height=height + 'px';
		theBottom.style.width = width+4+'px';		
		theOIC.style.width=width+24+'px';				
		theOIC.style.marginTop = parseInt(pageCoords[2] + (pageCoords[0] - height) / 2) + 'px';		
		}
		
		var theCaption = document.createElement('div');
		theCaption.id = 'caption';	
		
		var theName = document.createElement('div');
		theName.className = 'caption-name';	
		theName.innerHTML = (theImage.alt) ? theImage.alt : '';
		
		var theAuthor = document.createElement('div');
		theAuthor.className = 'author-name';			
			
		var theClose = document.createElement("a");
		theClose.id='topNavClose';
		theClose.setAttribute('href','javascript:void(0);');
		theClose.onclick = function() { closeBox(); }
		
		var allThumbs = new Array();
		var allLinks = document.getElementsByTagName('a');
		var linkLen = allLinks.length;
		for (i=0,j=0; i<linkLen; i++) {
			if (allLinks[i].getAttribute('rel') == 'lightbox') {
				allThumbs[j++] = allLinks[i];
			}
		}
		linkLen = allThumbs.length;
		if(linkLen>1){
		for (i=0; i!=linkLen; i++) {
		if (allThumbs[i].href == theTarget.href) {
				if (allThumbs[i-1]) {kpl=i-1;}
				else {kpl=linkLen-1;}
				if (allThumbs[kpl]) {
					var thePrevLink = document.createElement('a');
					thePrevLink.className = 'prevLink';
					thePrevLink.title = allThumbs[kpl].title;					
					thePrevLink.href = allThumbs[kpl].href;
					thePrevLink.onclick = function() { closeBox(); showBox(this); return false; }
					theHoverNav.insertBefore(thePrevLink, theHoverNav.firstChild);
					imgPrev = allThumbs[kpl];
				}
				if (i!=linkLen-1) {	kp=i+1;}
				else {kp=0;}
				if (allThumbs[kp]) {
					var theNextLink = document.createElement('a');
					theNextLink.className = 'nextLink';
					theNextLink.title = allThumbs[kp].title;
					theNextLink.href = allThumbs[kp].href;				
					theNextLink.onclick = function() { closeBox(); showBox(this); return false; }
					theHoverNav.insertBefore(theNextLink, theHoverNav.firstChild);
					imgNext = allThumbs[kp];
				}
			}
		}
		}
		theLoading.removeChild(theLoadingBtn);
		theShadow.removeChild(theLoading);
		theBox.appendChild(theOIC);
		
		theOIC.appendChild(theLTop);
		theOIC.appendChild(theTop);
		theOIC.appendChild(theRTop);
				
		theOIC.appendChild(theInIC);		
		theInIC.appendChild(theLmid);		
		theLmid.appendChild(theRmid);
		theRmid.appendChild(theIC);	
		
		theIC.appendChild(theIBox);	
		theIBox.appendChild(theImage);		
		theIBox.appendChild(theClose);
		theIBox.appendChild(theHoverNav);			
				
		theOIC.appendChild(theLBottom);
		theOIC.appendChild(theBottom);
		theOIC.appendChild(theRBottom);
		
		theIC.appendChild(theCaption);		
		theCaption.appendChild(theName);	
		theCaption.appendChild(theAuthor);
		
		document.onkeypress = getKey;
		return false;
	}
	imgPreload.src = theTarget.href;
}

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(theBox);
	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];
		if (anchor.getAttribute('href') && (anchor.getAttribute('rel') == 'lightbox')) {
			anchor.onclick = function() { showBox(this); return false; }
		}
	}
	anchor = null;
}


function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

addLoadEvent(initLitebox);
