// JavaScript Document
var currentPhoto = -1;
var currentPhotoIndex = 0;
var slideShowState = false;
var haveSetTimeout = false;
var PresentationPhotos = new Array();
var theInterval;
var lenDesc;

function PhotoInfo(PhotoID, FileName, ThumbnailFileName, Caption, Description, AddInfo, Width, Height)
{
	//alert('PhotoInfo');
	this.PhotoID = PhotoID;
	this.FileName = FileName;
	this.ThumbnailFileName = ThumbnailFileName;
	this.Caption = Caption;
	this.Description = Description;
	this.AddInfo = AddInfo;
	this.Width = Width;
	this.Height = Height;
}
function PhotoInfoDump()
{
	//alert('PhotoInfoDump');
	document.write("PhotoID = "+this.PhotoID+'<br />');
	document.write("FileName = "+this.FileName+'<br />');
	document.write("ThumbnailFileName = "+this.ThumbnailFileName+'<br />')
	document.write ("Caption = "+this.Caption+'<br />');
	document.write("Description = "+this.Description+'<br />');
	document.write("AddInfo = "+this.AddInfo+'<br />');
	document.write("Width = "+this.Width+'<br />');
	document.write("Height = "+this.Height+'<br />');
}
PhotoInfo.prototype.PhotoInfoDump = PhotoInfoDump;

function loadPresentation(PresentationID)
{
	//alert ('LoadPresentation: PresentationID='+PresentationID);
	if (window.XMLHttpRequest) {
		tojh_XMLHTTP = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		tojh_XMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (tojh_XMLHTTP) {
		url = '/2006/GetPresentationInfoXML.php?PresentationID='+PresentationID;
		tojh_XMLHTTP.onreadystatechange = handleStateChangePresentation;
		tojh_XMLHTTP.open("GET", url , true);
		tojh_XMLHTTP.send(null);
	}
	resizeThumbnails();
	highlightThumbnail(0);
}
function handleStateChangePresentation()
{
	// only if req shows "complete"
	if (tojh_XMLHTTP.readyState == 4) 
	{
		// only if "OK"
		//alert("tojh_handleStateChange1");
		if (tojh_XMLHTTP.status == 200) 
		{
			// ...processing statements go here...
			//alert(tojh_XMLHTTP.responseText);
			handlePresentationLoading();
			return(false);
		} else 
		{
			alert("There was a problem retrieving the XML data:\n" + tojh_XMLHTTP.statusText);
		}
	}
}
function handlePresentationLoading()
{
	//alert ('handlePresentationLoading: Start');
	if (tojh_XMLHTTP.responseText.indexOf('<tojh_') == -1) 
	{
		alert('Invalid Response Text: Tell Jim'); //did not get correct XML response.
		return;
	}
	var response  = tojh_XMLHTTP.responseXML.documentElement;
	var nPhoto = response.getElementsByTagName("NumberPhotos")[0].firstChild.data;
	//alert ('handlePresentationLoading: LoopStart');
	for (iPhoto = 0; iPhoto < nPhoto; iPhoto++)
	{
		//alert("iPhoto = "+iPhoto);
		var fName 	= response.getElementsByTagName("FileName")[iPhoto].firstChild.data;
		//alert('fName = '+fName);
		var ThumbName 	= response.getElementsByTagName("ThumbnailFileName")[iPhoto].firstChild.data;
		var Caption	= response.getElementsByTagName("ShortCaption")[iPhoto].firstChild.data;
		//alert('Caption = '+Caption);
		var Description	= response.getElementsByTagName("Description")[iPhoto].firstChild.data;
		//alert('Description = '+Description);
		var AddInfo	= response.getElementsByTagName("AddInfo")[iPhoto].firstChild.data;
		//alert('AddInfo = '+AddInfo);
		var PhotoID	= response.getElementsByTagName("PhotoID")[iPhoto].firstChild.data;
		//alert('PhotoID = '+PhotoID);
		var Height	= response.getElementsByTagName("Height")[iPhoto].firstChild.data;
		//alert('Height = '+Height);
		var Width	= response.getElementsByTagName("Width")[iPhoto].firstChild.data;
		//alert('Width = '+Width);
		//alert('Save Info');
		PresentationPhotos[iPhoto] = new PhotoInfo(PhotoID, fName, ThumbName, Caption, Description, AddInfo, Width, Height);
		//alert ("Saved");
		TOJH_preloadImages(fName);
	}
	//alert ('handlePresentationLoading: LoopEnd');
/*	for (iPhoto = 0; iPhoto < nPhoto; iPhoto++)
	{
		//alert(PresentationPhotos[iPhoto]);
		document.write("<br />Photo "+iPhoto+'<br />');
		PresentationPhotos[iPhoto].PhotoInfoDump();
	}
*/
	loadFirstPhoto();
}
function handleResize()
{
	//alert('handleResize');
	resizeThumbnails();
	if(PresentationPhotos.length > 0)
		HandleBothPhotos(currentPhotoIndex);
	else
		LoadBothPhotos(currentPhoto);
}
function HandleBothPhotos(iPhoto)
{
	//alert('HandleBothPhotos('+iPhoto+')');
	handleSmallPresentationPhoto(iPhoto);
	handleMainPresentationPhoto(iPhoto);
}
	
function handleTimeout()
{
	//alert('handleTimeout: slideShowState='+slideShowState);
	if(slideShowState)
	{
		loadNextPhoto();
		//alert(lenDesc);
		var secondsToWait = Math.max((lenDesc/300)*5, 10);
		//alert(secondsToWait);
		theInterval = window.setTimeout('handleTimeout();',secondsToWait*1000);
	}
	else
		LoadSmallPhoto(-1);
}
function LoadBothPhotos(PhotoID)
{
	//alert('LoadBothPhotos');
	if (window.XMLHttpRequest) {
		tojh_XMLHTTP = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		tojh_XMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (tojh_XMLHTTP) {
		url = '/2006/GetPhotoInfoXML.php?PhotoID='+PhotoID;
		tojh_XMLHTTP.onreadystatechange = handleStateChangeBothPhotos;
		tojh_XMLHTTP.open("GET", url , true);
		tojh_XMLHTTP.send(null);
	}
	if (PresentationPhotos.length > 0)
	{
		alert('LoadBothPhotos');
		theInterval = window.setTimeout('handleTimeout();',10000);
	}
	currentPhoto = PhotoID;
}
function LoadMainPhoto(PhotoID){
	//alert ('LoadMainPhoto: '+PhotoID);
	if (window.XMLHttpRequest) {
		tojh_XMLHTTP = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		tojh_XMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (tojh_XMLHTTP) {
		url = '/2006/GetPhotoInfoXML.php?PhotoID='+PhotoID;
		tojh_XMLHTTP.onreadystatechange = handleStateChangeMainPhoto;
		tojh_XMLHTTP.open("GET", url , true);
		tojh_XMLHTTP.send(null);
	}
	currentPhoto = PhotoID;
}
function LoadSmallPhoto(PhotoID){
	//alert ('LoadSmallPhoto: '+PhotoID);
	if (window.XMLHttpRequest) {
		tojh_XMLHTTP = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		tojh_XMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (tojh_XMLHTTP) {
		if (PhotoID == -1)
			url = '/2006/GetRandomPhotoInfoXML.php';
		else
			url = '/2006/GetPhotoInfoXML.php?PhotoID='+PhotoID;
			
		tojh_XMLHTTP.onreadystatechange = handleStateChangeSmallPhoto;
		tojh_XMLHTTP.open("GET", url , true);
		tojh_XMLHTTP.send(null);
	}
	if (PresentationPhotos.length > 0)
	{
		theInterval = window.setTimeout('handleTimeout();',10000);
	}
}
function windowWidth() {
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
  return(myWidth);
}
function handleMainPhoto()
{
	//alert('handleMainPhoto');
	if (tojh_XMLHTTP.responseText.indexOf('<tojh_photo>') == -1) 
	{
		alert('Invalid Response Text: Tell Jim'); //did not get correct XML response.
		return;
	}
	var response  = tojh_XMLHTTP.responseXML.documentElement;
	var fName 		= response.getElementsByTagName("FileName")[0].firstChild.data;
	//alert("Fname: " + fName);
	var picCaption 	= response.getElementsByTagName("ShortCaption")[0].firstChild.data;
	//alert(picCaption);
	var picHeight = -1;
	if (tojh_XMLHTTP.responseText.indexOf('<Height>') != -1)
	{
		picHeight = response.getElementsByTagName("Height")[0].firstChild.data;
	}
	//alert(picHeight);
	var picWidth = -1;
	if (tojh_XMLHTTP.responseText.indexOf('<Width>') != -1)
	{
		picWidth = response.getElementsByTagName("Width")[0].firstChild.data;
	}
	//alert(picWidth);
	var divPictureBox = document.getElementById("PictureBox");
	var winWidth = windowWidth();
	//alert(winWidth);
	var picBoxWidth = parseFloat(divPictureBox.style.width);
	if (divPictureBox.style.width.indexOf('%') != -1)
	{
		picBoxWidth = (picBoxWidth/100)*winWidth;
	}
	var picBoxHeight = picBoxWidth*0.75;
	var picRatio = picHeight/picWidth;
	//alert(picBoxWidth);
	//alert(picBoxHeight);
	//alert(picHeight);
	//alert(picWidth);
	//alert(picRatio);
	var mainPicture = document.getElementById("MainPicture");
	//alert("mainPicture");
	if (picWidth > picBoxWidth)
	{
		picWidth = picBoxWidth;
		picHeight = picWidth*picRatio;
	}
	if (picHeight > picBoxHeight)
	{
		picHeight = picBoxHeight;
		picWidth = picHeight/picRatio;
	}
	mainPicture.src = fName;
	mainPicture.height = picHeight;
	mainPicture.width = picWidth;
	//alert(mainPicture.src);
	var Desc = response.getElementsByTagName("Description")[0].firstChild.data;
	var picAddInfo = response.getElementsByTagName("AddInfo")[0].firstChild.data;
	//alert(picAddInfo);
	var tblCaption = document.getElementById("CaptionBox");
	tblCaption.innerHTML = picCaption;
	//alert(tblCaption.innerHTML);
	var DescTextBox = document.getElementById("DescriptionBox");
	DescTextBox.innerHTML = Desc;
	//alert(DescTextBox.style.left);
	DescTextBox.style.left = (parseFloat(picWidth)+1)+'px';
	//alert(parseFloat(picWidth)+1);
	//alert(DescTextBox.style.left);
	resizeDescription();
	
	var tblAddInfo = document.getElementById("AddInfoBox");
	tblAddInfo.innerHTML = picAddInfo;
	var tblFilenameInfo = document.getElementById("FilenameBox");
	tblFilenameInfo.innerHTML = '<a href="' + fName + '" title="View image by itself" target="_blank" >' + fName + '</a>';
}
function handleSmallPhoto()
{
	if (tojh_XMLHTTP.responseText.indexOf('<tojh_photo>') == -1) 
	{
		alert('Invalid Response Text: Alert Jim'); //did not get correct XML response.
		return;
	}
	var response  = tojh_XMLHTTP.responseXML.documentElement;
	var fName 		= response.getElementsByTagName("ThumbnailFileName")[0].firstChild.data;
	//alert("Fname: " + fName);
	var picID 	= response.getElementsByTagName("PhotoID")[0].firstChild.data;
	//alert(picID);
	var picHeight = -1;
	if (tojh_XMLHTTP.responseText.indexOf('<Height>') != -1)
	{
		picHeight = response.getElementsByTagName("Height")[0].firstChild.data;
	}
	//alert(picHeight);
	var picWidth = -1;
	if (tojh_XMLHTTP.responseText.indexOf('<Width>') != -1)
	{
		picWidth = response.getElementsByTagName("Width")[0].firstChild.data;
	}
	//alert(picWidth);
	var divPictureBox = document.getElementById("BannerThumbnailBox");
	var winWidth = windowWidth();
	//alert(winWidth);
	var picBoxWidth = parseFloat(divPictureBox.style.width);
	if (divPictureBox.style.width.indexOf('%') != -1)
	{
		picBoxWidth = (picBoxWidth/100)*winWidth;
	}
	var picBoxHeight = picBoxWidth*0.75;
	var picRatio = picHeight/picWidth;
	//alert(picBoxWidth);
	//alert(picBoxHeight);
	//alert(picHeight);
	//alert(picWidth);
	//alert(picRatio);
	//alert("thumbnailPicture");
	if (picWidth > picBoxWidth)
	{
		picWidth = picBoxWidth;
		picHeight = picWidth*picRatio;
	}
	if (picHeight > picBoxHeight)
	{
		picHeight = picBoxHeight;
		picWidth = picHeight/picRatio;
	}
	var theHTML1 = '<a href="/2006/displayonephoto.php?PhotoID=' + picID + '" title="' + 'Click for a large photo"  >';
	var theHTML2 = '<img src="'+fName+'" width ="' +  picWidth + '" height="' + picHeight + '" border="0" /> </a>';
	divPictureBox.innerHTML = theHTML1 + theHTML2;
}
function handleSmallPresentationPhoto(iPhoto)
{
	var fName 	= PresentationPhotos[iPhoto].ThumbnailFileName;
	//alert("Fname: " + fName);
	var picID 	= PresentationPhotos[iPhoto].PhotoID;
	//alert(picID);
	var picHeight = PresentationPhotos[iPhoto].Height;
	//alert(picHeight);
	var picWidth = PresentationPhotos[iPhoto].Width;
	//alert(picWidth);
	var divPictureBox = document.getElementById("BannerThumbnailBox");
	var winWidth = windowWidth();
	//alert(winWidth);
	var picBoxWidth = parseFloat(divPictureBox.style.width);
	if (divPictureBox.style.width.indexOf('%') != -1)
	{
		picBoxWidth = (picBoxWidth/100)*winWidth;
	}
	var picBoxHeight = picBoxWidth*0.75;
	var picRatio = picHeight/picWidth;
	//alert(picBoxWidth);
	//alert(picBoxHeight);
	//alert(picHeight);
	//alert(picWidth);
	//alert(picRatio);
	//alert("thumbnailPicture");
	if (picWidth > picBoxWidth)
	{
		picWidth = picBoxWidth;
		picHeight = picWidth*picRatio;
	}
	if (picHeight > picBoxHeight)
	{
		picHeight = picBoxHeight;
		picWidth = picHeight/picRatio;
	}
	var theHTML1 = '<a href="/2006/displayonephoto.php?PhotoID=' + picID + '" title="' + 'Click for a large photo"  >';
	var theHTML2 = '<img src="'+fName+'" width ="' +  picWidth + '" height="' + picHeight + '" border="0" /> </a>';
	divPictureBox.innerHTML = theHTML1 + theHTML2;
}
function handleStateChangeMainPhoto() {
	// only if req shows "complete"
	if (tojh_XMLHTTP.readyState == 4) {
		// only if "OK"
		//alert("tojh_handleStateChange1");
		if (tojh_XMLHTTP.status == 200) {
			// ...processing statements go here...
			//alert(tojh_XMLHTTP.responseText);
			handleMainPhoto();
			return(false);
		} else {
			alert("There was a problem retrieving the XML data:\n" + tojh_XMLHTTP.statusText);
		}
	}
}
function handleStateChangeSmallPhoto() {
	// only if req shows "complete"
	if (tojh_XMLHTTP.readyState == 4) {
		// only if "OK"
		//alert("tojh_handleStateChange1");
		if (tojh_XMLHTTP.status == 200) {
			// ...processing statements go here...
			//alert(tojh_XMLHTTP.responseText);
			handleSmallPhoto();
		} else {
			alert("There was a problem retrieving the XML data:\n" + tojh_XMLHTTP.statusText);
		}
	}
}
function handleStateChangeBothPhotos()
{
	// only if req shows "complete"
	if (tojh_XMLHTTP.readyState == 4) {
		// only if "OK"
		//alert("tojh_handleStateChange1");
		if (tojh_XMLHTTP.status == 200) {
			// ...processing statements go here...
			//alert(tojh_XMLHTTP.responseText);
			handleMainPhoto();
			handleSmallPhoto();
		} else {
			alert("There was a problem retrieving the XML data:\n" + tojh_XMLHTTP.statusText);
		}
	}
}
function highlightThumbnail(iThumbnail)
{
	//alert('highlightThumbnail: '+iThumbnail);
	//alert('ImageThumbnail'+iThumbnail);
	if (currentPhotoIndex != iThumbnail)
	{
		unHighlightThumbnail(currentPhotoIndex);
	}
	imgThumbnail = document.getElementById('ImageThumbnail'+iThumbnail);
	//alert(imgThumbnail);
	imgThumbnail.style.borderColor='#FFFF00';
	imgThumbnail.border=2;
	currentPhotoIndex = iThumbnail;
}
function unHighlightThumbnail(iThumbnail)
{
	imgThumbnail = document.getElementById('ImageThumbnail'+iThumbnail);
	//alert(imgThumbnail);
	imgThumbnail.border=1;
	imgThumbnail.style.borderColor='#0000FF';
}

function handleMainPresentationPhoto(iPhoto)
{
	//alert('handleMainPresentationPhoto');
	var fName 		= PresentationPhotos[iPhoto].FileName;
	//alert("Fname: " + fName);
	var picCaption 	= PresentationPhotos[iPhoto].Caption;
	//alert(picCaption);
	var picHeight = PresentationPhotos[iPhoto].Height;
	//alert(picHeight);
	var picWidth = PresentationPhotos[iPhoto].Width;
	//alert(picWidth);
	var divPictureBox = document.getElementById("PictureBox");
	var winWidth = windowWidth();
	//alert(winWidth);
	var picBoxWidth = parseFloat(divPictureBox.style.width);
	if (divPictureBox.style.width.indexOf('%') != -1)
	{
		picBoxWidth = (picBoxWidth/100)*winWidth;
	}
	var picBoxHeight = picBoxWidth*0.75;
	var picRatio = picHeight/picWidth;
	//alert(picBoxWidth);
	//alert(picBoxHeight);
	//alert(picHeight);
	//alert(picWidth);
	//alert(picRatio);
	var mainPicture = document.getElementById("MainPicture");
	//alert("mainPicture");
	if (picWidth > picBoxWidth)
	{
		picWidth = picBoxWidth;
		picHeight = picWidth*picRatio;
	}
	if (picHeight > picBoxHeight)
	{
		picHeight = picBoxHeight;
		picWidth = picHeight/picRatio;
	}
	mainPicture.src = fName;
	mainPicture.height = picHeight;
	mainPicture.width = picWidth;
	//alert(mainPicture.src);
	var Desc = PresentationPhotos[iPhoto].Description;
	lenDesc = Desc.length;
	//alert(lenDesc);
	var picAddInfo = PresentationPhotos[iPhoto].AddInfo;
	//alert(picAddInfo);
	var tblCaption = document.getElementById("CaptionBox");
	tblCaption.innerHTML = picCaption;
	//alert(tblCaption.innerHTML);
	var DescTextBox = document.getElementById("DescriptionBox");
	DescTextBox.innerHTML = Desc;
	//alert(DescTextBox.style.left);
	DescTextBox.style.left = (parseFloat(picWidth)+1)+'px';
	//alert(parseFloat(picWidth)+1);
	//alert(DescTextBox.style.left);
	resizeDescription();
	
	var tblAddInfo = document.getElementById("AddInfoBox");
	tblAddInfo.innerHTML = picAddInfo;
	var tblFilenameInfo = document.getElementById("FilenameBox");
	tblFilenameInfo.innerHTML = '<a href="' + fName + '" title="View image by itself" target="_blank" >' + fName + '</a>';
}
function loadPresentationPhoto(iPhoto)
{
	//alert('loadPresentationPhoto: '+iPhoto);
	highlightThumbnail(iPhoto);
	//alert('loadPresentationPhoto: PhotoID='+PresentationPhotos[iPhoto]);
	handleSmallPresentationPhoto(iPhoto);
	handleMainPresentationPhoto(iPhoto);
	currentPhotoIndex = iPhoto;	
	currentPhoto = PresentationPhotos[currentPhotoIndex].PhotoID;
}
function loadFirstPhoto()
{
	//alert('loadFirstPhoto');
	loadPresentationPhoto(0);
}
function loadLastPhoto()
{
	loadPresentationPhoto(PresentationPhotos.length-1);
}
function loadPrevPhoto()
{
	newPhotoIndex = (currentPhotoIndex == 0? PresentationPhotos.length-1: currentPhotoIndex-1);
	loadPresentationPhoto(newPhotoIndex);
}
function loadNextPhoto()
{
	newPhotoIndex = (currentPhotoIndex == PresentationPhotos.length-1? 0 : currentPhotoIndex+1);
	loadPresentationPhoto(newPhotoIndex);
}
function startSlideShow()
{
	//alert('startSlideShow');
	window.clearTimeout(theInterval);
	loadNextPhoto();
	slideShowState = true;
	var secondsToWait = Math.max((lenDesc/300)*5, 10);
	//alert(secondsToWait);
	theInterval = window.setTimeout('handleTimeout();',secondsToWait*1000);
}
function stopSlideShow()
{
	//alert('stopSlideShow');
	slideShowState = false;
	window.clearTimeout(theInterval);
}
function toggleSlideShow()
{
	//alert('toggleSlideShow');
	if (slideShowState)
		stopSlideShow();
	else
		startSlideShow();
}
function resizeThumbnails()
{
	//alert('resizeThumbnails');
	var Factor = windowWidth()/(340*5);
	//alert(Factor);
	//alert(PresentationPhotos.length);
	for (iThumbnail=0; iThumbnail < PresentationPhotos.length; iThumbnail++)
	{
		imgThumbnail = document.getElementById('ImageThumbnail'+iThumbnail);
		//alert(imgThumbnail);
		var thWidth = imgThumbnail.width;
		var thHeight = imgThumbnail.height;
		var Ratio = thWidth/thHeight;
		if (thHeight > thWidth)
		{
			imgThumbnail.height = Math.round(60*Factor+0.5);
			imgThumbnail.width = Math.round(imgThumbnail.height*Ratio + 0.5);
		} 
		else
		{
			imgThumbnail.width = Math.round(80*Factor+0.5);
			imgThumbnail.height = Math.round(imgThumbnail.width/Ratio+0.5);
		}
		//alert(imgThumbnail.width);
		//alert(imgThumbnail.height);
	}
}
function resizeDescription()
{
	//alert('resizeDescription');
	var Factor = windowWidth()/(320*5);
	var DescTextBox = document.getElementById("DescriptionBox");
	//alert(DescTextBox.style.fontSize);
	DescTextBox.style.fontSize = Math.round(Factor*100)+'%';
	//alert(DescTextBox.style.fontSize);
}
function TOJH_preloadImages() { //v3.0
	//alert('TOJH_preloadImages: Start');
	var d=document; 
	  if(d.images)
	  { 
		if(!d.TOJH_p) d.TOJH_p=new Array();
		var i,j=d.TOJH_p.length,a=TOJH_preloadImages.arguments; 
		for(i=0; i<a.length; i++)
		{ 
			d.TOJH_p[j]=new Image; d.TOJH_p[j++].src=a[i];
		}
	  }
	//alert('TOJH_preloadImages: End');
}
