/*
	ContactSheet JS: Image preview within a given area
	by Jamie Pickett - http://www.liftllc.com

	Table of Contents
	-----------------
	Configuration
	
	Functions
	- pause()
	- getKey()
	- listenKey()
	- showLightbox()
	- initLightbox()
	- addLoadEvent()
	
	Function Calls
	- addLoadEvent(initLightbox)

*/

// Configuration

// If you would like to use a custom loading image or close button reference them in the next two lines.
var loadingImage = 'galleries/loading.gif';		
//var closeButton = 'close.gif';	
var borderWidth = 1;
var borderStyle = 'solid';
var borderColor = '#ffffff';
var initImage = 'galleries/default.jpg';
var useFadeIn = true;


// pause(numberMillis)
// Pauses code execution for specified time. Uses busy code, not good.
// Code from http://www.faqts.com/knowledge_base/view.phtml/aid/1602

function pause(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}

// getKey(key)
// Gets keycode. If 'x' is pressed then it hides the lightbox.

function getKey(e){
	if (e == null) { // ie
		keycode = event.keyCode;
	} else { // mozilla
		keycode = e.which;
	}
	key = String.fromCharCode(keycode).toLowerCase();
	
	if(key == 'x'){ hideLightbox(); }
}


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

// Preloads images. Pleaces new image in lightbox then centers and displays.

function showLightbox(objLink)
{
	// prep objects
	//var objCaption = document.getElementById('lightboxCaption');
	var objImage = document.getElementById('lightboxImage');
	var objLoadingImage = document.getElementById('loadingImage');
	var objEnlargement = document.getElementById('enlargement');
	
	// center loadingImage if it exists
	if (objLoadingImage) {
		objLoadingImage.style.top = (objEnlargement.offsetTop + (objEnlargement.offsetHeight/2) - (objLoadingImage.height/2)) + 'px';
		objLoadingImage.style.left = (objEnlargement.offsetLeft + (objEnlargement.offsetWidth/2) - (objLoadingImage.width/2)) + 'px';
		objLoadingImage.style.display = 'block';
	}

	// preload image
	imgPreload = new Image();
	//What to do when the image is finished loading
	imgPreload.onload=function(){
		set_photo_size(imgPreload);
		//alert(arrayImageSize);
		objImage.width = arrayImageSize[0];
		objImage.height = arrayImageSize[1];
		objImage.style.border = String(borderWidth+'px '+borderStyle+' '+borderColor);
		objImage.style.visibility = 'hidden';
		objImage.src = objLink.href;
		
		//objImage.style.marginTop = 0 + (objEnlargement.offsetHeight-objImage.height-(borderWidth*2))/2+'px';
		
		//fade the new image in
		if(useFadeIn == true){
			objImage.style.visibility = 'visible';
			setOpacity(objImage, 0);
			fadeIn(objImage.id,0);
		}else{
			objImage.style.visibility = 'visible';
		}
		
		
		/*
		if(objLink.getAttribute('title')){
			objCaption.style.display = 'block';
			//objCaption.style.width = imgPreload.width + 'px';
			objCaption.innerHTML = objLink.getAttribute('title');
		} else {
			objCaption.style.display = 'none';
		}
		*/
		
		// A small pause between the image loading and displaying is required with IE,
		// this prevents the previous image displaying for a short burst causing flicker.
		if (navigator.appVersion.indexOf("MSIE")!=-1){
			pause(250);
		} 

		if (objLoadingImage){
			objLoadingImage.style.display = 'none';
		}

		return false;
	}
	
	imgPreload.src = objLink.href;
}

//
// initLightbox()
// Function runs on window load, going through link tags looking for rel="lightbox".
// These links receive onclick events that enable the lightbox display for their targets.
// The function also inserts html markup at the top of the page which will be used as a
// container for the overlay pattern and the inline image.
//
function initLightbox()
{
	//prep objects
	var objEnlargement = document.getElementById('enlargement');
	var objBody = document.getElementsByTagName("body").item(0);
	
	if (!document.getElementsByTagName){ return; }
	var anchors = document.getElementsByTagName("a");

	// loop through all anchor tags
	for (var i=0; i<anchors.length; i++){
		var anchor = anchors[i];

		if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "lightbox")){
			anchor.onclick = function () {showLightbox(this); return false;}
		}
	}
	
	//create the image needed for the enlargement
	var objEnlargeImage = document.createElement("img");
	//objEnlargeImage.src = initImage;
	objEnlargeImage.setAttribute('id','lightboxImage');
	objEnlargement.appendChild(objEnlargeImage);
	
	//create the image needed for the enlargement
	var objLoadingImage = document.createElement("img");
	objLoadingImage.src = loadingImage;
	objLoadingImage.setAttribute('id','loadingImage');
	//objLoadingImage.style.position = 'absolute';
	//objLoadingImage.style.zIndex = '150';
	objBody.appendChild(objLoadingImage);
}

set_photo_size = function(imgObj){
		var container = document.getElementById('enlargement');
		var targ = { w:container.offsetWidth - (borderWidth*2), h:container.offsetHeight - (borderWidth*2)};
		var orig = { w:imgObj.width, h:imgObj.height };
		// shrink image with the same aspect
		var ratio = 1.0;
		if ((orig.w >= targ.w || orig.h >= targ.h) && orig.h && orig.w)
			ratio = ((targ.w / orig.w) < (targ.h / orig.h)) ? targ.w / orig.w : targ.h / orig.h;
		imagWidth  = Math.floor(orig.w * ratio);
		imagHeight = Math.floor(orig.h * ratio);
		
		arrayImageSize = new Array(imagWidth,imagHeight);
		return arrayImageSize;
	}
	
	
	
// fade photo functions

function setOpacity(obj, opacity) {
  opacity = (opacity == 100)?99.999:opacity;
  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";
  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;
  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;
  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
}

function fadeIn(objId,opacity) {
  if (document.getElementById) {
    obj = document.getElementById(objId);
    if (opacity <= 100) {
      setOpacity(obj, opacity);
      opacity += 20;
      window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 100);
    }
  }
}

//
// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		func();
		oldonload();
		}
	}

}

addLoadEvent(initLightbox);	// run initLightbox onLoad