/*
 * SparPhotoGallery.js
 * Version: 1.00
 * Created On: April 10, 2004
 * Author: Jordan McCullough
 * Copyright: Ambient Ideas, LLC, 2004
 *
 * Collection of methods to provide forward and backward navigation through photo galleries.
 * Method to display popup window of zoomed (large) photos and other photo related functions.
 */

function nextPhoto(baseFileName, baseFileExtension, photoArraySize, imageObjToSet, directionForward){
	//retrieve position (number of current image)
	//For loop through all chars in image object src file name
	//test character if numeric, add to number string variable
	//parse result number string into an integer variable
	positionVarNumberString = "";
	
	//Files used with SparPhotoGallery may only have numbers for sequence tracking.
	//No other numbers are allowed in file name or the parsing of the image path will fail.
	for (i = document[imageObjToSet].src.length - 1; i >= 2; i--) {
		var thisChar = document[imageObjToSet].src.substring(i-1, i);
		//Once numeric character is found, continue storing numeric characters until a non-numeric character is found
		
		//If numbers have previously been found and we encounter a non-numeric, break out of the for loop.
		if(positionVarNumberString.length > 0 && ((thisChar < "0") || (thisChar > "9")) ){
			break;
		}
		//If numeric character is found, append it to the left side of the existing number sequence.
		else if ((thisChar >= "0") && (thisChar <= "9")) {
      		positionVarNumberString = thisChar + positionVarNumberString;
    	}
	}
	
	positionVar = parseInt(positionVarNumberString);

	//Loop: Create array with basefilename and photoarraysize number appended to each file
	photoArray = new Array(photoArraySize);
	
	for(i = 0; i < photoArraySize; i++){
		//Array of image names "i" through "photoArraySize"
		photoArray[i] = baseFileName + i + "." + baseFileExtension;
	}
	
	
	//test if positionVar is less than photoArraySize - 1
	//if < photoArraySize - 1, advance positionVar by 1 
	//Else set positionVar to 0
	
	//Test if the next or previous image is the beginning or end of sequence.
	//If so, loop to first or last photo (depending on direction of positionVar).
	if (directionForward == true){
		if (positionVar < (photoArray.length-1) ){
			positionVar += 1;
		}
		else{
			positionVar = 0;
		}
	} 
	else if(directionForward == false){
		if (positionVar > 0 ){
			positionVar -= 1;
		}
		else{
			positionVar = photoArraySize-1;
		}
	}

	//retrieve photoArray[positionVar] as an object
	//Save photo object into imageObjToSet
	document[imageObjToSet].src = photoArray[positionVar];
}

function zoomPhoto(zoomedFileExtension, imageObjToSet, popupWindowWidth, popupWindowHeight){
	baseFileName = "";
	zoomedFileName = "";
	
	//read all chars up to the first dot "." into a filename plus path variable
	//Read from right to left, obtaining the full image path left of the file extension "."
	for (i = document[imageObjToSet].src.length - 1; i >= 2 ; i--) {
		var thisChar = document[imageObjToSet].src.substring(i-1, i);
		
		if (thisChar == "." ) {
      		baseFileName = document[imageObjToSet].src.substring(0, i-1);
			break;
    	}
	}
	
	//append zoomed file extension (i.e. "-zoomed.jpg"
	zoomedFileName = baseFileName + zoomedFileExtension;
	
	//Open a popup window with the image path
	openPopupWindow(zoomedFileName, popupWindowWidth, popupWindowHeight);
}

function openPopupWindow(URL, popupWindowWidth, popupWindowHeight) {
	var leftVal = getMiddleX(popupWindowWidth);
	var topVal = getMiddleY(popupWindowHeight);
	popupWindow = window.open(URL, "flyer", "menubar,independent,scrollbars,width="+popupWindowWidth+",height="+popupWindowHeight+",left="+leftVal+",top="+topVal+"")
	popupWindow.focus()
}

function getMiddleX(popupWindowWidth){
	if (screen) {
       	var x = (screen.availWidth - popupWindowWidth)/2;
	}
	return x;
}
	
function getMiddleY(popupWindowHeight){
	if (screen) {
        var y = (screen.availHeight - popupWindowHeight)/2;
	}
	return y;
}