function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function imgSelector(imgSelect){
	var curImg = document.getElementById('curImg');
	curImg.style.visibility = "hidden";
	
	var prevImg = document.getElementById("currentImage");
	
	if(prevImg){
		prevImg.className = "galImg";
		prevImg.id = "";
	}
		
	imgSelect.id = "currentImage";
	
	var curImgParent = document.getElementById('curImgWrap');
	//var curImgLink = curImg.parentNode;
	var imgId = imgSelect.getElementsByTagName('img')[0].id;
	var source = imgSelect.getAttribute("href");	
	
	curImg.setAttribute("src", source);
	//curImgLink.setAttribute("href", source);
	
	var locationArray = source.split("/");
	var imgTitle = locationArray[locationArray.length - 1];
	imgTitle = imgTitle.split(".")[0];
	imgTitle = imgTitle.replace(/%20/g, ' ');
	document.getElementById("imgName").innerHTML = imgTitle;
	
	adjustMainImage(curImg, imgId, curImgParent);
	
	curImg.style.visibility = "visible";
	return false;
}

function adjustMainImage(ci, srcImgId, ciWrap){
	var containerHeight = ciWrap.offsetHeight;
	var containerWidth = ciWrap.style.width;
	var galTitle = document.getElementsByTagName('h1')[0];
	var imgTitle = document.getElementsByTagName('h2')[0];
	var srcImg = document.getElementById(srcImgId);
	var headingHeight = parseInt(galTitle.offsetHeight) + parseInt(imgTitle.offsetHeight);
	
	//offsets for the img's border, padding and margin
	var hOffset = 34;
	var wOffset = 12;
	
	var availDimensions = {};
	availDimensions.height = containerHeight - headingHeight - hOffset;
	availDimensions.width = parseInt(containerWidth) - wOffset;
	
	var newImage = {};
	newImage.height = getNaturalHeight(srcImg);
	newImage.width = getNaturalWidth(srcImg);
	
	while (newImage.height > availDimensions.height || newImage.width > availDimensions.width){
		resizeObject(newImage, availDimensions);
	}
	
	ci.style.height = newImage.height + "px";
	ci.style.width = newImage.width + "px";
}

function resizeObject(newObj, available){
	var fixer = 1;
	if (newObj.height > available.height){
		fixer = available.height / newObj.height;
	} else if (newObj.width > available.width) {
		fixer = available.width / newObj.width;
	}
	
	newObj.height = newObj.height * fixer;
	newObj.width = newObj.width *fixer;
}

function adjustPage(){
	var bodyHeight = getBodyHeight();
	
	var galBody = document.getElementById("picBody");
	galWidth = galBody.offsetWidth;
	
	var gl = galBody.getElementsByTagName("ul")[0];
	gl.style.height = bodyHeight + "px";
	
	var cl = document.getElementById("curImg");
	var clParent = document.getElementById("curImgWrap");
	clParent.style.width = (galWidth - 380) + 'px';
	clParent.style.height = bodyHeight + "px";
	
	adjustMainImage(cl, cl.id, clParent);
	allImagesLoaded();
	
	document.getElementById("galleryPlaceHolder").style.display = "none";
	galBody.style.visibility = 'visible';
}

function getWindowHeight() {
	var windowHeight=0;
	if (typeof(window.innerHeight)=='number') {
		windowHeight=window.innerHeight;
	} else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		} else {
			if (document.body && document.body.clientHeight) {
				windowHeight=document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}

function getBodyHeight() {
	var windowHeight = getWindowHeight();
	
	var headerHeight = document.getElementById('headWrap').offsetHeight;
	//var footerHeight = document.getElementById('footWrap').offsetHeight;
	
	return (windowHeight - (headerHeight)) * .97;
}

function prepareGallery() {
  if (!document.getElementsByTagName) return false;
  if (!document.getElementById) return false;
  if (!document.getElementById("imgGallery")) return false;
  var gallery = document.getElementById("imgGallery");
  var links = gallery.getElementsByTagName("a");
  for ( var i=0; i < links.length; i++) {
	if(i == 0)
		links[i].id = "currentImage";
	links[i].className = "galImg";
    links[i].onclick = function() {
		return imgSelector(this);
	}
    links[i].onkeypress = links[i].onclick;
  }
}

function getNaturalHeight(img) {
	if( img.naturalHeight ) {
		return img.naturalHeight;
	} else {
		lgi = new Image();
		lgi.src = img.src;
		return lgi.height;
	}
}

function getNaturalWidth(img) {
	if( img.naturalWidth ) {
		return img.naturalWidth;
	} else {
		lgi = new Image();
		lgi.src = img.src;
		return lgi.width;
	}
}

function allImagesLoaded() {
	var images = document.images;

	for (image in images){
		
		while(image.complete == false) {
			setTimeout("wait", 10000);
		}
	}
}

addLoadEvent(prepareGallery);
addLoadEvent(adjustPage);

window.resize = function (){
	adjustPage();
}