// get gallery links
addLoadEvent(goGallery);

// adapted from an ALA article
// http://alistapart.com/articles/behavioralseparation

// use the thumbnail links <a href=""> for the replacement image src
// and use the <a title=""> for the caption

// change the IDs below
var thumbs_id = 'portfolio_thumbs';
var image_id = 'portfolio_image';
var caption_id = 'portfolio_caption';

function goGallery(){
  prepareGallery(thumbs_id, image_id, caption_id);
}

function prepareGallery(thumbs_id, image_id, caption_id){
	// check to make sure the browser supports DOM
	if(document.getElementById && document.getElementsByTagName){
		if(document.getElementById(thumbs_id)){
			var thumbs = document.getElementById(thumbs_id);
			var links = thumbs.getElementsByTagName('a');
			var bigphoto = document.getElementById(image_id);
			
			for(var i = 0; i < links.length; i++){
				links[i].onclick = function(){
					bigphoto.style.filter="blendTrans(duration=2)"
					return showPic(this, image_id, caption_id);
				};
			}
		}
	}
}

function showPic (pic, i_id, c_id) {
	if (document.getElementById) {
		var bigPhoto = document.getElementById(i_id);
		var caption = document.getElementById(c_id);
		// hide the caption until the new photo loads
		caption.style.visibility = "hidden";
		// hide the image until it loads
		bigPhoto.style.visibility = "hidden";

		// preload image
		imgPreload = new Image();

		imgPreload.onload=function(){
			bigPhoto.src = pic.href;
			bigPhoto.style.visibility = "visible";
			fadeIn(i_id,0);	

			// insert caption using link title
			if (pic.title) {
				caption.childNodes[0].nodeValue = pic.title;
			} else {
				caption.childNodes[0].nodeValue = pic.childNodes[0].nodeValue;
			}
			caption.style.visibility = "visible";
			return false;
		}

		imgPreload.src = pic.href;	
		return false;
	} else {
		return true;
	}
}

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

function setOpacity(obj, opacity) {
  opacity = (opacity == 100)?100.00: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;
}

// load a bunch of functions that will execute when the page is done loading
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {oldonload();}
      func();
    }
  }
}

