// get gallery links
addLoadEvent(prepareGallery);
// check for site links with rel="external" in them
addLoadEvent(externalLinks);

function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i < anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external"){
			anchor.target = "_blank";
			anchor.className = "external";
			anchor.title = "opens in a new window";
		}
	}
}

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

// change the image named "big_photo" with whichever thumbnail is clicked.
// use the thumbnail links href for the replacement src
function prepareGallery(){
	// check to make sure the browser supports DOM
	if(document.getElementById && document.getElementsByTagName){
		if(document.getElementById('photo_thumbs')){
			var thumbs = document.getElementById('photo_thumbs');
			var links = thumbs.getElementsByTagName('a');
			var bigphoto = document.getElementById('big_photo');
			
			for(var i = 0; i < links.length; i++){
				links[i].onclick = function(){
					bigphoto.style.filter="blendTrans(duration=2)"
					return showPic(this);
				};
			}
		}
	}
}

function showPic (whichpic) {
	if (document.getElementById) {
		var bigPhoto = document.getElementById('big_photo');
		var caption = document.getElementById('photo_caption');
		// 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 = whichpic.href;
			bigPhoto.style.visibility = "visible";
			fadeIn('big_photo',0);	

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

		imgPreload.src = whichpic.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();
    }
  }
}

