var __PhotoShow__Object__ = new Array;
var __PhotoShow__Count__ = 0;

function PhotoShow (photoShowDivID) {
	var PhotoShowIndex = __PhotoShow__Count__;
	__PhotoShow__Count__++;
	__PhotoShow__Object__[PhotoShowIndex] = this;

	var PhotoShowDivField = document.getElementById(photoShowDivID);
	var ImageViewerField = null;
	var PhotoCaptionField = null;
	var ImageSrc = new Array();
	var ThumbSrc = new Array();
	var PhotoCaption = new Array();
	var PhotoTitle = new Array();
	var CachedImages = new Array();
	var ImageCount = 0;
	var ImagePageNumber = 0;
	var ImagesLoaded = false;
	var SlideShowTimer = null;
	var LoadImageTimer = null;
	var ShowImageTimer = null;
	
	this.AddPhoto = function(imageSrc, thumbSrc, caption, title) {
		ImageSrc[ImageCount] = imageSrc;
		ThumbSrc[ImageCount] = thumbSrc;
		PhotoCaption[ImageCount] = caption;
		PhotoTitle[ImageCount] = title;
		ImageCount++;
	}

	this.Show = function() {
		PhotoShowDivField.style.display = "block";
		SlideShow();
	}
	
	this.Hide = function() {
		if (SlideShowTimer) {
			clearTimeout(SlideShowTimer);
			SlideShowTimer = null;
		}
		PhotoShowDivField.style.display = "none";
	}
	
	this.ShowImage = ShowImage;
	
	function ShowImage(PageNumber) {
		ImagePageNumber = PageNumber % ImageCount;
		StopSlideShow();
	}
	
	function StopSlideShow() {
		if (SlideShowTimer) {
			clearTimeout(SlideShowTimer);
			SlideShowTimer = null;
		}
		ShowCurrentImage();
	}

	function ShowCurrentImage() {
		if (ShowImageTimer) {
			clearTimeout(ShowImageTimer);
			ShowImageTimer = null;
		}
		if (ImageCount > 0 && CachedImages[ImagePageNumber].src && CachedImages[ImagePageNumber].complete) {			
			// If this browser supports IE filters then use them.
			// Add support in the future for other browsers.
			if (ImageViewerField.filters) {
				ImageViewerField.style.filter="blendTrans(duration=0.5)";
				ImageViewerField.filters.blendTrans.Apply();
			}      
			ImageViewerField.src = ImageSrc[ImagePageNumber];
			if (ImageViewerField.filters) {
				ImageViewerField.filters.blendTrans.Play();
			}
			PhotoCaptionField.innerHTML = "<b>" + PhotoCaption[ImagePageNumber] + "</b>";
		} else {
			PhotoCaptionField.innerHTML = "<b>Waiting for '" + PhotoTitle[ImagePageNumber] + "'</b>";
			LoadNextImage();
			ShowImageTimer = setTimeout(ShowCurrentImage, 500);
		}
	}
	
	this.Preload = function() {
		html = "<div style=\"600px;height:405px\">" +
			"<img title=\"\" alt=\"Image Viewer\" id=\"__PhotoShow__ImageViewer__" +
			PhotoShowIndex + "__\" style=\"border:thin solid black;\" src=\"" + ImageSrc[0] + "\"><br>" +
			"</div><div id=\"__PhotoShow__PhotoCaption__" + PhotoShowIndex +
			"__\" style=\"text-align:center\"><b>" + PhotoCaption[0] + "</b></div>";

		if (ImageCount > 1) {
			html += "<button onclick=\"__PhotoShow__Object__[" + PhotoShowIndex + "].PreviousImage();\" style=\"text-align:center;width:120px;\">&lt;&lt; Previous &lt;&lt;</button>";
			html += "<button onclick=\"__PhotoShow__Object__[" + PhotoShowIndex + "].SlideShow();\" style=\"width:120px;\">Slide Show</button>";
			html += "<button onclick=\"__PhotoShow__Object__[" + PhotoShowIndex + "].NextImage();\" style=\"width:120px;\">&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;Next&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;</button>";
			html += "<br><div style=\"text-align:center;float:left;overflow:auto;width:600px;height:200px\"><table><tr>";

			column = 0;
			htmlThumbRow = '';
			htmlCaptionRow = '';
			for (i = 0; i < ImageCount; i++) {
				if (column >= 5) {
					html += "<tr>" + htmlThumbRow + "</tr>";
//					html += "<tr>" + htmlCaptionRow + "</tr>";
					htmlThumbRow = '';
					htmlCaptionRow = '';
					column = 1;
				} else {
					column++;
				}
//				htmlCaptionRow += "<td>" +
				htmlThumbRow += "<td><img alt=\"Page " + i +
					"\" title=\"" + PhotoTitle[i] +
					"\" onclick=\"__PhotoShow__Object__[" +
					PhotoShowIndex + "].ShowImage(" + i +
					");\" border=\"2\" src=\"" +
					ThumbSrc[i] + "\">" +
					"<br><a href=\"javascript:__PhotoShow__Object__[" +
					PhotoShowIndex + "].ShowImage(" + i +
					");\">" +
					PhotoTitle[i] + "</a></td>";
			}
			html += "<tr>" + htmlThumbRow + "</tr>";
//			html += "<tr>" + htmlCaptionRow + "</tr>";
			html += "</table></div>";
		}
		PhotoShowDivField.innerHTML = html;
		ImageViewerField = document.getElementById("__PhotoShow__ImageViewer__" + PhotoShowIndex + "__");
		PhotoCaptionField = document.getElementById("__PhotoShow__PhotoCaption__" + PhotoShowIndex + "__");
		
		for (i = 0; i < ImageCount; i++) {
			CachedImages[i] = new Image();
//			CachedImages[i].src = ImageSrc[i];
		}
		ShowImage(0);
		SlideShowTimer = setTimeout(SlideShow, 3000);
	}
	
	function LoadNextImage() {
		if (LoadImageTimer) {
			clearTimeout(LoadImageTimer);
			LoadImageTimer = null;
		}
		i = ImagePageNumber;
		while (ImageCount > 0 && !ImagesLoaded) {
			if (!CachedImages[i].src) {
				CachedImages[i].src = ImageSrc[i];
			}
			if (!CachedImages[i].complete) {
				LoadImageTimer = setTimeout(LoadNextImage, 100);
				break;
			}
			i = (i + 1) % ImageCount;
			if (i == ImagePageNumber) {
				ImagesLoaded = true;
				break;
			}
		}
	}
	
	this.NextImage = function() {
		this.ShowImage(ImagePageNumber + 1);
	}
	
	this.PreviousImage = function() {
		this.ShowImage(ImagePageNumber - 1);
	}
	
	this.SlideShow = SlideShow;
	
	function SlideShow() {
		if (ImageCount > 1) {
			NextPageNumber = (ImagePageNumber + 1) % ImageCount;
			if (CachedImages[NextPageNumber].src && CachedImages[NextPageNumber].complete) {			
				ShowImage(NextPageNumber);
				SlideShowTimer = setTimeout(SlideShow, 3000);
			} else {
				PhotoCaptionField.innerHTML = "<b>Waiting for '" + PhotoTitle[NextPageNumber] + "'</b>";
				LoadNextImage();
				SlideShowTimer = setTimeout(SlideShow, 500);
			}
		}
	}
}

