/**
 * @author David E. Still
 */

Event.observe(window, "load", function(){
	var firstImg = null;
	$A($("thumbs").getElementsByTagName("a")).each(function(thumbnail){
		if (firstImg == null) firstImg = thumbnail;
		Event.observe(thumbnail, "click", photoSwitch.bind(thumbnail));
		thumbnail.caption = thumbnail.getElementsByTagName("img")[0].alt;
		thumbnail.rel = thumbnail.href;
		thumbnail.href = "javascript:void(null);";
	});
	photoSwitch(firstImg);
});

function photoSwitch(firstImg) {
	thumb = (firstImg.href)?firstImg:this;
	/* highlight selected thumbnail */
	$A($("thumbs").getElementsByTagName("a")).each(function(anchor){
		$(anchor).removeClassName("selected");
	});
	thumb.addClassName("selected");
	/* write caption */
	var header = $("photo").getElementsByTagName("h1");
	if (header.length > 0) header[0].innerHTML = thumb.getElementsByTagName("img")[0].title;
	/* fade out current photo */
	currentImg = $("photo").getElementsByTagName("img");
	if (currentImg.length > 0) new Effect.Fade(currentImg[0], { afterFinish: function(){ this.remove(); }.bind(currentImg[0]) });
	/* fade in new photo */
	var preload = new Image();
	preload.onload = function() {
		var thumbImg = this.getElementsByTagName("img")[0];
		var newImg = Builder.node("img", { src: this.rel, border: 0, style: "display: none;", title: thumbImg.title, alt: thumbImg.alt, onload: "new Effect.Appear(this);" });
		$("photo").appendChild(newImg);
		new Effect.Appear($(newImg));
	}.bind(thumb);
	preload.src = thumb.rel;
}

function prinths() {
	var prinths = $("photo").getElementsByTagName("img")[0];
	var imgPath = prinths.src.replace("headshots/", "headshots/print/");
	var page = "<html><head><title>Printable Headshot</title>\n";
	page += "<link rel='stylesheet' type='text/css' href='style/hs.css' />\n";
	page += "<link rel='stylesheet' type='text/css' href='style/hsprint.css' media='print' /></head><body>\n";
	page += "<img src='"+imgPath+"' border='0' />\n";
	page += "<div><a href='javascript:window.print();'>print</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href='javascript:window.close();'>close</a></div>\n";
	page += "</body></html>";
	var newPage = window.open("", "printableHeadshot", "resizable=yes,scrollbars=yes,status=yes,width=400,height=600");
	newPage.document.write(page);
	newPage.document.close();
}