//================================================================================
//  ParaSoft's Javascript Photo Album, version 2.0, March 26, 2004
//  (ParaSoft @ shaw.ca)
//  Changed this version: 
//
//  This photo album started life as Suzy's Javascript Photo Album, 
//	 version 5.2, April 29, 2002
//  You may contact her at suzy @ kitykity.com for assistance. (Remove spaces in email address)
//
//  Most of the routines were re-written or had code added.
//  A large number of new routines were added by Peter Pommerehne (ParaSoft @ shaw.ca)
//  webmaster of TGIF Singles in Vancouver BC, Canada (http://www.tgifbc.com)
//  to present the album with more controls and as a true slide show,
//	and now bears virtually no resemblance to SuzyAlbum.
//
//	This photo album currently uses overLIB 4.00 released 2004-03-23 by Erik Bosrup
//	a very slick and useful DHTML javascript.
//  website: http://www.bosrup.com/web/overlib/
//	email: mailto:erik@bosrup.com
//
//  This javascript code may be used on other Websites if:  
//  1) This notice is left unchanged  
//  2) A link to http://www.tgifbc.com is placed on that page
//  You may contact me at ParaSoft @ shaw.ca for assistance. (Remove spaces in email address)
//     
//================================================================================
self.name = "main";		// names current window as "main"
PicVar		= 0;
Debug			= false;		// Turn debugging on or off
timeout		= 0;
myWindow  = 0;
resAdj		= 0;
CycleTime	= 5;				// Default image cycle in seconds (1-99)
maxWin		= .95;			// Maximum popup window size relative to parent WINDOW size
var winLeft=0,winTop=20,winWidth=0,winHeight=0;
var issIE	=document.all?true:false;

//================================================================================
//	Initialize ParaSoftAlbum
//================================================================================
function InitParaSoftAlbum() {
	if (CycleTime < 1 || CycleTime > 99)
		CycleTime = 5;
	if (window.document.forms.ParaSoftForm) {
		form = window.document.forms.ParaSoftForm;
		delay = CycleTime*1000;
		if (slideshow)
		{
			TimeParaSoftPic();
		}
	}
}

//================================================================================
//	Show the current image in the album
//================================================================================
function ShowParaSoftPic(newpic) {
//	if (document.ParaSoftForm.Sound.checked) {	//=================================
//		playSound('f1');													// Play flipping sound
//	}
  current = newpic;
	ParaSoftPicBounds(ParaSoftArray[current].PicVal);	// Set image size for Album

  SelectionBox = document.ParaSoftForm.ParaSoftDropdown;
  SelectionBox.options[current].selected = true;

  document.getElementById("ParaSoftSpot").innerHTML = '<a href=javascript:OVLviewer("' + ParaSoftArray[current].PicVal + '");><img name="PicShowing" src=' + ParaSoftArray[current].PicVal + ' height=' + imgHeight + ' width=' + imgWidth + ' border=0></a>';

  document.getElementById("Journal").innerHTML = '<br><p>' + ParaSoftArray[current].DatVal + '</p><p>' + ParaSoftArray[current].TxtVal + '</p>';

	if (slideshow)
	{
		if (document.ParaSoftForm.WhereView.checked) {
			timerID = setTimeout("OVLviewer(ParaSoftArray[current].PicVal)",0)
		}
	}
}

//================================================================================
//	Play sound
//================================================================================
//function playSound(){	//=======================================
//	document.f1.play();
//}

//================================================================================
//	Randomize images while in slide show
//================================================================================
function RandomParaSoftPic() {
  randompic = Math.floor(Math.random()*PicVar);
  last = current;
  ShowParaSoftPic(randompic);
}

//================================================================================
//	Go to first image in the album
//================================================================================
function BeginParaSoftPic() {
  PauseParaSoftPic();
  current = 0;
  ShowParaSoftPic(current);
}

//================================================================================
//	Go to pevious image in the album
//================================================================================
function PreviousParaSoftPic() {
  PauseParaSoftPic();
  last = current;
  current--;
  if(current<0) current = PicVar - 1;
  ShowParaSoftPic(current);
}

//================================================================================
//	Pause the slideshow
//================================================================================
function PauseParaSoftPic() {
	if (over != null) cClick();			// Close any previous overlib pop-ups
	document.ParaSoftForm.WhereView.checked = false;	// Uncheck slide popup 
	clearTimeout(timeout);
	timeout = 0;
	slideshow=false;
}

//================================================================================
//	Play the slideshow
//================================================================================
function SlideParaSoftPic() {
	slideshow=true;
	if (document.ParaSoftForm.Random.checked) {
    RandomParaSoftPic();
  }
  current++;
  if (current>=PicVar) current = 0;
  ShowParaSoftPic(current);
	
	if (window.document.forms.ParaSoftForm) {
		form = window.document.forms.ParaSoftForm;
		delay = form.delay.value*1000;
		if (delay < 1) {
			delay = CycleTime*1000;
			form.delay.value = CycleTime;
		}
		TimeParaSoftPic();
	}
}

//================================================================================
//	Go to next image in the album
//================================================================================
function NextParaSoftPic() {
  PauseParaSoftPic();
  last = current;
  TotalImages = document.ParaSoftForm.ParaSoftDropdown.options.length;
  current++;
  if (current>=PicVar) current = 0;
  ShowParaSoftPic(current);
}

//================================================================================
//	Go to last image in the album
//================================================================================
function EndParaSoftPic() {
  PauseParaSoftPic();
  current = PicVar - 1;
  ShowParaSoftPic(current);
}

//================================================================================
//	Search for all images containing search criteria
//================================================================================
function FindPic() {
  TotalFound = 0;
  SearchString = document.ParaSoftForm.SearchWord.value;
  SearchString = SearchString.toLowerCase();
  WriteResults = window.open("searchclear.htm","resultwindow","height=450, width=450, toolbar=0, status=0, menubar=0, resizable=1, scrollbars=1");
  WriteResults.document.write('<div style="text-align:center; font:8pt verdana">'
    + 'You searched for:<br><i>' + SearchString + '</i><br><b>Results:</b><br>');
  for (loop=0; loop < PicVar ; loop++) {
    Keyword = ParaSoftArray[loop].TxtVal;
    Keyword = Keyword.toLowerCase();
    URL = ParaSoftArray[loop].PicVal;
    title = ParaSoftArray[loop].TitVal;
    title = title.toLowerCase();
    SearchResult = Keyword.indexOf(SearchString);
    SearchResult2 = title.indexOf(SearchString);
    if (SearchResult != "-1" || SearchResult2 != "-1") {
      last = current;
      WriteResults.document.write('<br><a href=javascript:ShowParaSoftPic(' + loop + '); target="main">' + title + '</a>');
      TotalFound++;
    }
  }
  WriteResults.document.write('<p><b>Returned ' + TotalFound + ' results.</b>');
}

//================================================================================
//	Set the slideshow cycle time
//================================================================================
function TimeParaSoftPic() {
	timeout = setTimeout(SlideParaSoftPic,delay);
}

//================================================================================
//	Set a new slideshow cycle time
//================================================================================
function ResetDelay() {
	if (window.document.forms.ParaSoftForm) {
		PauseParaSoftPic();
		form = window.document.forms.ParaSoftForm;
		delay = form.delay.value*1000;
		if (delay < 1) {
			delay = CycleTime*1000;
			form.delay.value = CycleTime;
		}
		TimeParaSoftPic();
	}
}

//================================================================================
//	OVLviewer  - Generates a formated pop-up using overlib() to display an image.
//  Called by: - ShowParaSoftPic()
//================================================================================
function OVLviewer(image) {
	WinScreenRes();										// Get screen resolution
																		// and current dimensions of window (width & height)
	ParaSoftWinBounds(image);					// Set image & window size for popup window

	if (slideshow)
		if (!document.ParaSoftForm.WhereView.checked) PauseParaSoftPic();

	htmlLine  = '<div align="center">'
	htmlLine += '<table class=popTable width="'+winWidth+'">'
	htmlLine += '<tr>'
	htmlLine += '<td class=popTitle width="'+(winWidth-60)+'">'+ParaSoftArray[current].TitVal+'</td>'
	htmlLine += '<td width="60">'
	htmlLine += '<div class=popClose align="right"><a class=popClose href="javascript:return PauseParaSoftPic();" onmouseover="return PauseParaSoftPic();">Close</a></div>'
	htmlLine += '</td>'
	htmlLine += '</tr>'
	htmlLine += '<tr>'
	htmlLine += '<td class=popImage align="center" colspan="2">'
	htmlLine += '<img name="PicShowing" src=' + ParaSoftArray[current].PicVal + ' height="' + winHeight + '" width="' + winWidth + '" border=0></td>'
	htmlLine += '</tr>'
	htmlLine += '</table>'
	htmlLine += '</div>'

	OVLpopImage();
}

//================================================================================
// ParaSoftPicBounds - Sets the image boundaries. i.e. the maximum size of either 
//								 width and/or height of an image in the album. 
//								 Keeps the original aspect ratio of the image
//				 Caller: ShowParaSoftPic()
//================================================================================
function ParaSoftPicBounds(picName) {
		aspRatio					= 1;
    ShowingImage			= new Image();
    ShowingImage.src	= picName;
    imgWidth					= ResAdjust(ShowingImage.width);
    imgHeight					= ResAdjust(ShowingImage.height);

	if (imgWidth > (maxBounds * resAdj) || imgHeight > (maxBounds * resAdj))
		{
			if (imgWidth > imgHeight)						// Process a landscape image
			{
					aspRatio  = (maxBounds * resAdj) / imgWidth;
					imgWidth  = (maxBounds * resAdj);
					imgHeight = Math.round(imgHeight * aspRatio);
			}
			else																// Process a portrait image
			{
					aspRatio  = (maxBounds * resAdj) / imgHeight;
					imgWidth  = Math.round(imgWidth * aspRatio);
					imgHeight = (maxBounds * resAdj);
			}
		}
}

//================================================================================
// ParaSoftWinBounds - Sets the width, height and position of the OVLviewer() 
//										 window based on the size of the window.
//										 Keeps the original aspect ratio of the image
//				 Caller: ShowParaSoftPic()
//================================================================================
function ParaSoftWinBounds(picName) {
	WShowingImage			= new Image();
	WShowingImage.src	= picName;
	OimgWidth					= WShowingImage.width;			// Original image width
	OimgHeight				= WShowingImage.height;			// Original image height
	wrkWinW						= docWinWidth  * maxWin;		// Adjusted document window width
	wrkWinH						= docWinHeight * maxWin;		// Adjusted document window height
	winWidth					= OimgWidth;
	winHeight					= OimgHeight;

	if ((winWidth > wrkWinW) || (winHeight > wrkWinH)){
		if (wrkWinW > wrkWinH){	// Process for a landscape document window
			if (OimgWidth > OimgHeight){							// Process a landscape image
				imgAspRatio = OimgHeight / OimgWidth;		// Get image aspect ratio
				winHeight  = wrkWinH-20;								// Set image to maximum allowable height
				winWidth   = winHeight / imgAspRatio;		// Adjust image width to aspect ratio
				if (winWidth > wrkWinW){	// Works
					winWidth	= wrkWinW;									// Set image to maximum allowable width
					winHeight = (winWidth * imgAspRatio)-20;		// Adjust image width to aspect ratio
				}
			}
			else {																		// Process a portrait image
				imgAspRatio = OimgWidth / OimgHeight;		// Get image aspect ratio
				winHeight  = wrkWinH-20;								// Set image to maximum allowable height
				winWidth   = winHeight * imgAspRatio;		// Adjust image width to aspect ratio
				if (winHeight > wrkWinH){
					winHeight	= wrkWinH-20;								// Set image to maximum allowable width
					winWidth = winHeight * imgAspRatio;		// Adjust image width to aspect ratio
				}
			}
		}
		else {									// Process for a portrait document window
				if (OimgWidth > OimgHeight){							// Process a landscape image
					imgAspRatio = OimgWidth / OimgHeight;		// Get image aspect ratio
					winWidth		= wrkWinW;									// Set image to maximum allowable width
					winHeight   = (winWidth / imgAspRatio)-20;		// Adjust image width to aspect ratio
				}
				else {																		// Process a portrait image
					imgAspRatio = OimgWidth / OimgHeight;		// Get image aspect ratio
					winHeight  = wrkWinH-20;										// Set image to maximum allowable height
					winWidth   = winHeight * imgAspRatio;		// Adjust image width to aspect ratio
					if (winHeight > wrkWinH){
						winHeight	= wrkWinH-20;								// Set image to maximum allowable width
						winWidth = winHeight / imgAspRatio;		// Adjust image width to aspect ratio
					}
				}
		}
	}
	winLeft = (docWinWidth/2)-(winWidth/2);					// Popup window location
	winLeft = winLeft+document.body.scrollLeft;			// Popup window location
}

//================================================================================
// ResAdjust - Adjusts an album image down depending on the screen resolution
//================================================================================
function ResAdjust(imageSize) {
	origSize = imageSize;
	newSize  = 0;
	if (resAdj == 0 ){
	WinScreenRes();
	}
	newSize = origSize * resAdj;
	if (newSize > origSize)
//		return imageSize = origSize;
		return origSize;
	else
//		return imageSize = newSize;
		return newSize;
}

//================================================================================
// WinScreenRes - Determine the current screen resolution based on "scrBase"
//							- Find the width and height of the current document window 
//================================================================================
function WinScreenRes() {
	res	 = screen.width;
	resAdj = res/scrBase;	// "scrBase" is defined in the JavaScript where you 
												// specify the images for the photo album.

	// Find current horizontal inner window width
	if (eval(self.document.body) && eval("typeof self.document.body.clientWidth=='number'") && eval(self.document.body.clientWidth)) {
	docWinWidth = eval(self.document.body.clientWidth);
	} else if (typeof(self.innerWidth) == 'number') {
	docWinWidth = self.innerWidth;
	}

	// Find current vertical inner window height
	if (eval(self.document.body) && eval("typeof self.document.body.clientHeight=='number'") && eval(self.document.body.clientHeight)) {
	docWinHeight = eval(self.document.body.clientHeight);
	} else if (typeof(self.innerHeight)=='number') {
	docWinHeight = self.innerHeight;
	}
}

//================================================================================
// Debug display routine
// call with: if (Debug)DebugDisp("value = ",value,"text msg: ","");
//================================================================================
function DebugDisp(parm1,parm2,parm3,parm4) {
	if (confirm(parm1+parm2+parm3+parm4));
	else Debug = false;
}

//==============================================================================
//	Call overlib() to create a popup
//==============================================================================
function OVLpopImage(){
	if (winWidth > 10 && winHeight > 10)	{	// Don't popup very small images
		overlib(htmlLine,
			FULLHTML,
			WIDTH,winWidth,
			STICKY,
			DELAY,'100',
			FIXY,document.body.scrollTop + 5,
			FIXX,winLeft
			);
	}
}

