var currentPic=0,maxPicHeight=0,maxPicWidth=0,picArray=new Array();

function Pic(picShortDesc,picLocation,picWidth,picHeight,picCaption,picBGColor,picThumbLocation)
{
	this.shortDesc=picShortDesc;
	this.location=picLocation;
	this.width=picWidth;
	this.height=picHeight;
	this.caption=picCaption;
	this.bgColor=picBGColor;
	this.thumbLocation=picThumbLocation;
}

function changeDims()
{
//Top pane width
	document.styleSheets[0].rules[1].style.width=(document.body.clientWidth - parseInt(document.styleSheets[0].rules[4].style.width,10)) + "px";
//Main  pane width
	maxPicWidth=document.body.clientWidth - parseInt(document.styleSheets[0].rules[2].style.width,10);
	document.styleSheets[0].rules[3].style.width=maxPicWidth + "px";
//Main pane height
	maxPicHeight=document.body.clientHeight - parseInt(document.styleSheets[0].rules[1].style.height,10) - parseInt(document.styleSheets[0].rules[6].style.height,10);
	document.styleSheets[0].rules[3].style.height=maxPicHeight + "px";
//Left pane height
	document.styleSheets[0].rules[2].style.height=(document.body.clientHeight - parseInt(document.styleSheets[0].rules[1].style.height,10) - parseInt(document.styleSheets[0].rules[5].style.height,10)) + "px";
//Caption pane width
	document.styleSheets[0].rules[6].style.width=document.styleSheets[0].rules[3].style.width;
}

function initialLoad()
{
	loadPics();
	changeDims();
	buildList();
	showPic();
}

function buildList()
{
	var thePhotoList=document.getElementById("photoList");
	for (var i=0; i<(picArray.length); i++)
		{
		thePhotoList.appendChild(document.createElement("LI"));
		thePhotoList.lastChild.appendChild(document.createElement("A"));
		thePhotoList.lastChild.lastChild.href="javascript:showPic(" + i + ");";
		thePhotoList.lastChild.lastChild.className="picListNormal";
		thePhotoList.lastChild.lastChild.appendChild(document.createTextNode(picArray[i].shortDesc));
		}
}

function showPic(picIndex)
{
	if (picIndex == null)
		{
		picIndex=currentPic;
		}
	var theImage=document.getElementById("thePic");
	var thePrevButton=document.getElementById("prevButton");
	var theNextButton=document.getElementById("nextButton");
	var thePhotoList=document.getElementById("photoList");
	var theCaption=document.getElementById("captionWindow");
	thePhotoList.childNodes[currentPic].lastChild.className="picListNormal";
	thePhotoList.childNodes[picIndex].lastChild.className="picListCurrent";
	thePrevButton.style.visibility=(picIndex == 0) ? "hidden" : "";
	theNextButton.style.visibility=(picIndex == (picArray.length - 1)) ? "hidden" : "";
	var thePath=document.location.toString().substr(0,(document.location.toString().lastIndexOf("/") + 1));
	theImage.src=thePath + picArray[picIndex].location;
	theCaption.firstChild.firstChild.data=picArray[picIndex].caption;
	var thePicWidth=picArray[picIndex].width;
	var thePicHeight=picArray[picIndex].height;
	var aspectRatioMax=(maxPicWidth / maxPicHeight);
	var aspectRatioPic=(thePicWidth / thePicHeight);
	var newImgWidth=(aspectRatioPic < aspectRatioMax) ? Math.round(maxPicHeight * aspectRatioPic) : maxPicWidth;
	var newImgHeight=(aspectRatioPic < aspectRatioMax) ? maxPicHeight : Math.round(maxPicWidth / aspectRatioPic);
	theImage.width=newImgWidth;
	theImage.height=newImgHeight;
//	alert("maxPicWidth: " + maxPicWidth + "\nmaxPicHeight: " + maxPicHeight + "\npicWidth: " + picArray[picIndex].width + "\npicHeight: " + picArray[picIndex].height + "\naspectRatioMax: " + aspectRatioMax + "\naspectRatioPic: " + aspectRatioPic + "\nnewImgWidth: " + newImgWidth + "\nnewImgHeight: " + newImgHeight + "\ncurrImgWidth: " + theImage.width + "\ncurrImgHeight: " + theImage.height);
	currentPic=picIndex;
}

function zoomWindow()
{
	var theImage=document.getElementById("thePic");
	var thePicWidth=picArray[currentPic].width;
	var thePicHeight=picArray[currentPic].height;
	var theZoomWindowParm="location=no,menubar=no,resizable=yes,left=0,top=0"
	if (((thePicWidth + 20) <= (screen.availWidth - 8)) && ((thePicHeight + 30) <= (screen.availHeight - 45)))
		{
		theZoomWindowParm += ",width=" + (thePicWidth + 20) + ",height=" + (thePicHeight + 30);
		}
	else
		{
		if ((thePicWidth + 41) <= (screen.availWidth - 8))
			{
			theZoomWindowParm += ",scrollbars=yes,width=" + (thePicWidth + 41);
			theZoomWindowParm += ",height=" + (screen.availHeight - 45);
			}
		else
			{
			if ((thePicHeight + 51) <= (screen.availHeight - 45))
				{
				theZoomWindowParm += ",scrollbars=yes,width=" + (screen.availWidth - 8);
				theZoomWindowParm += ",height=" + (thePicHeight + 51);
				}
			else
				{
				theZoomWindowParm += ",scrollbars=yes,width=" + (screen.availWidth - 8);
				theZoomWindowParm += ",height=" + (screen.availHeight - 45);
				}
			}
		}
	var newWindow=window.open(picArray[currentPic].location,"zoomWindow",theZoomWindowParm);
}
