var numImages=20;
var imagesPerPage=5;
var thumbFolder="/fw/portals/13/skins/fortneyweygandt/thumbnail/";
var largeFolder="/fw/portals/13/skins/fortneyweygandt/lrgImagesOptimized/";
var imageWidth=98;
var imageMargin=3;
var page=0;
var pageCount=numImages/imagesPerPage;
var scrollTo=0;
var scrollPerFrame=10;
var scrollFPS=30;
var pause=false;
var pauseDuration=5000;
var lastImage=1;
var pageDuration=5000;

var debug=true;

function thisMovie(movieName) {
	if (window[movieName] && window[movieName].sendToActionScript) {
		return window[movieName];
	} else if (document[movieName] && document[movieName].sendToActionScript) {
	return document[movieName];
	}

}

function imageName(i) {
	return "image"+(i<10?"0":"")+i+".jpg";
}

function onLoad() {
	var divThumbnails=document.getElementById("thumbnails");
	for (var i=1; i<=numImages+imagesPerPage; i++) {
		var image=document.createElement("img");
		var wrap=((i-1)%numImages)+1;

		image.src=thumbFolder+imageName(wrap);
		image.style.border="none";

		var a=document.createElement("a");
		a.href="#";

		a.appendChild(image);
		a.style.position="absolute";
		a.style.left=((i-1)*(imageWidth+imageMargin)) + "px";
		a.id=imageName(wrap);
		a.onclick=onThumbnailClick;


		divThumbnails.appendChild(a);
	}

	newPage();
}

function onThumbnailClick() {
	if (thisMovie("gallery") && thisMovie("gallery").sendToActionScript) {
		(thisMovie("gallery").sendToActionScript(largeFolder+this.id));
		pause=true;
		setTimeout("pause=false",pauseDuration);
	}
}

function newPage() {
	if (!pause && thisMovie("gallery") && thisMovie("gallery").sendToActionScript) {
		// make new page visible
		page+=1;

		scrollTo=-page*(imageWidth+imageMargin)*imagesPerPage;
			
		if (page>=pageCount) page=0;

		scroll();

		// fade in random image that isn't in page
		var firstVisible=page*imagesPerPage+1;
		var i=lastImage;
		while (i==lastImage) {
			i=Math.floor(Math.random()*(numImages-imagesPerPage));
			if (i>=firstVisible) i+=imagesPerPage;
		}
		lastImage=i;
		

		thisMovie("gallery").sendToActionScript(largeFolder+imageName(i));

		setTimeout("newPage();",pageDuration);
	} else {
		setTimeout("newPage();",50);
	}
}

function scroll() {
	var divThumbnails=document.getElementById("thumbnails");
	var currentScroll=parseInt(divThumbnails.style.left.replace("px",""));
	currentScroll-=scrollPerFrame;
	if (currentScroll<=scrollTo) currentScroll=scrollTo;

	divThumbnails.style.left=currentScroll + "px";

	if (currentScroll>scrollTo) {
		setTimeout("scroll();",1000/scrollFPS);
	} else {
		if (page==0) {
			divThumbnails.style.left="0px";
		}
	}
}

document.write("<object id='gallery' classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0' height='275' width='502'>");
document.write("<param name='src' value='/fw/portals/13/skins/fortneyweygandt/gallery.swf'/>");
document.write("<param name='wmode' value='opaque' />");
document.write("<embed name='gallery' src='/fw/portals/13/skins/fortneyweygandt/gallery.swf' pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash' wmode='opaque' height='275' width='502'/>");
document.write("</object>");

window.gallery=document.forms[0].gallery;