/*
 * Javascript created by SMH on 26.3.2008
 * Javascript modified by SMH on 28.6.2008
 *
 */

var browserAgt=navigator.userAgent.toLowerCase();
var is_ie=((browserAgt.indexOf("msie") != -1) && (browserAgt.indexOf("opera") == -1)); //is browser MSIE?
var is_mac=((browserAgt.indexOf("mac") != -1)); //is operating system Mac?
var platform=navigator.platform.toLowerCase();
var is_iphone=((platform.indexOf("iphone") != -1)); //is hardware an iPhone?
var buffer1 = 0;
var buffer2 = 0;

//MSIE measures the dimensions of a new window differently from other browsers.
//Make the following adjustments.
if (is_ie) {
	buffer1 = 40; //increases window.open height to accommodate specific needs of new MSIE window
	buffer2 = 68; //increases window.resizeTo height to accommodate specific needs of new MSIE window
} else {
}

//MSIE v5.23 for the Mac doesn't have px units in its image measurements.
//Other browsers need the units to be stripped before doing math.
if (is_mac && is_ie) {
	var pxLength = 0;
} else {
	var pxLength = 2;
}

//MSIE 5.23 on Mac browsers displays the navbar with different border placement.
//The cell widths need to be adjusted for consistency.
if (is_mac && is_ie) {
	var borderWidthAdj = 2;
} else {
	var borderWidthAdj = 0;
}

function showAlert() {
	alert('You clicked the image!');
}

function showAlert(message) {
	alert(message);
}
	
function showArrayElements() {
	alert('starting function showArrayElements');
	alert('array length is '+ imageSources.length +'.');
	for(var i=0; i<imageSources.length; i++) {
		alert('The #'+ i +' image is '+ imageSources[i]);
	}
}


function calcMaxAspectRatio(widths, heights) {
	var maxAspectRatio = 1;
	var aspectRatios = new Array();
	for (var i=0; i<widths.length; i++) {
		aspectRatios[i] = widths[i]/heights[i];
		if (aspectRatios[i] > maxAspectRatio) {
			maxAspectRatio = aspectRatios[i];
		}
	}
//	maxAspectRatio = Math.max.apply(Math,aspectRatios); (not supported by MSIE 5.2 for Mac)
	return maxAspectRatio;
}

var scrnScaleFactor = 1;

function calcEnlargedSlideFactor(maxAspectRatio, stdHeight) {
	if ((stdHeight + 145 + buffer1) > window.screen.availHeight) {
		scrnScaleFactor = window.screen.availHeight / (stdHeight + 145 + buffer1);
	}
	if (maxAspectRatio > window.screen.availWidth/window.screen.availHeight) {
		if (stdHeight * scrnScaleFactor * maxAspectRatio > window.screen.availWidth) {
			scrnScaleFactor = window.screen.availWidth/(stdHeight * scrnScaleFactor * maxAspectRatio);
		}
	}
	return scrnScaleFactor;
}

function loadImages(imageSources, widths, heights, slideImages) {
	if (document.images) {

		//process images if browser is can use them
		for (var i=0; i<imageSources.length; i++) {

			//preload image
			slideImages[i] = new Image;
			slideImages[i].src = imageSources[i];

			//scale image to thumbnail size
			//scale to larger dimension of 110 pixels
			var dimMax=110;
			if (widths[i]>=heights[i]) {
				slideImages[i].style.width=dimMax;
				slideImages[i].style.height=Math.round(dimMax*(heights[i]/widths[i]));
			} else {
				slideImages[i].style.width=Math.round(dimMax*(widths[i]/heights[i]));
				slideImages[i].style.height=dimMax;
			}
		}
	} else {

		//output "test" if browser cannot use images
		for (var i=0; i<imageSources.length; i++) {
			slideImages[i] = 'test';
		}
		document.rollOverImg = 'test';
	}

	//return array of slide images for use in other contexts
	return slideImages;
}

//*******************************************
//

//scale enlargements on page to this multiple of the thumbnail
scaleFactor = 4.2;

//
//*******************************************

function insertSlideThumbs(slideImages) {

	//write html code for all thumbs to existing table cell
	for (var i=0; i<slideImages.length; i++) {

		//insert a break character every second thumb
		if (i%2 == 0) {
			document.write("<br>");
		}

		//write html code for a single thumb
		if (is_iphone) {
			document.write("<img src="+ slideImages[i].src +" style='width:"+ slideImages[i].style.width +"; height:"+ slideImages[i].style.height +"; cursor:pointer; border:4px solid #FFFFFF' onMouseOver='var i = "+ i +"; document.rollOverImg.src=slideImages[i].src; document.rollOverImg.style.width=Math.round(scaleFactor*slideImages[i].style.width.substr(0,slideImages[i].style.width.length-pxLength)); document.rollOverImg.style.height=Math.round(scaleFactor*slideImages[i].style.height.substr(0,slideImages[i].style.height.length-pxLength));'>");
		} else {
			document.write("<img src="+ slideImages[i].src +" style='width:"+ slideImages[i].style.width +"; height:"+ slideImages[i].style.height +"; cursor:pointer; border:4px solid #FFFFFF' onClick='var i = "+ i +"; showEnlargedImage(slideImages[i]);' onMouseOver='var i = "+ i +"; document.rollOverImg.src=slideImages[i].src; document.rollOverImg.style.width=Math.round(scaleFactor*slideImages[i].style.width.substr(0,slideImages[i].style.width.length-pxLength)); document.rollOverImg.style.height=Math.round(scaleFactor*slideImages[i].style.height.substr(0,slideImages[i].style.height.length-pxLength));'>");
		}
	}

	//end with a break character
	document.write("<br>");
}

function insertSlideEnlargement(startImage) {

	//write html code for enlarged slide within existing table cell
	//startImage appears, then to be replaced by rollOverImg
	document.write("<img src="+ startImage.src +" name='rollOverImg' style='width:"+ Math.round(scaleFactor*startImage.style.width.substr(0,startImage.style.width.length-pxLength)) +"; height:"+ Math.round(scaleFactor*startImage.style.height.substr(0,startImage.style.height.length-pxLength)) +"; border: 0px solid #F3F3CE' alt='Project Image'>");
}

var OpenWindow = null;

function showEnlargedImage(slideObject) {

	//scale enlarged image
	height= stdHeight * scrnScaleFactor;
	width=Math.round(height*(slideObject.style.width.substr(0,slideObject.style.width.length-pxLength)/slideObject.style.height.substr(0,slideObject.style.height.length-pxLength)));

	//write html code to new window, or bring open window to foreground for reuse
	//Safari requires the window.open height + width to have units, so "px" is added
	if(OpenWindow == null || OpenWindow.closed) {
            OpenWindow=window.open("/selected_image", "Project_Photo", "height="+ (height + 90 + buffer1) +"px, width="+ (width + 65) +"px, directories=no, location=no, scrollbars=no");
	} else {
		OpenWindow.focus();                                     //bring already open window to foreground and reuse
		OpenWindow.resizeTo((width + 70), (height + 143 + buffer2));  //ensure that the image will fit the window.
	}
	OpenWindow.document.write("<title>Project Photo</title>");
	OpenWindow.document.write("<body bgcolor=#F3F3CE>");
	OpenWindow.document.write("<div align='center'>");
	OpenWindow.document.write("<img src="+ slideObject.src +" style='width:"+ width +"px; height:"+ height +"px; border:20px solid #F3F3CE'>");
	OpenWindow.document.write("</div>");
	OpenWindow.document.write("<div align='center'>");
	OpenWindow.document.write("<form>");
	OpenWindow.document.write("<input type='button' value='Close Window' onClick='window.close()'>");
	OpenWindow.document.write("</div>");
	OpenWindow.document.write("</body>");
	OpenWindow.document.write("</html>");
	OpenWindow.document.close();
	self.name="main";
}

function cellInitBgColor(activeNavCell,numNavCells) {
	var activeNavCellID = 'cell'+ activeNavCell;
	var navCellBorderColor = '#888844';
	var activeNavCellBorderColor = '#BBBB88';
	var activeCellBgColor = '#BBBB88';
	var currentCell;
	var currentCellBorderColor;
	for (var i=0; i<numNavCells; i++) {
		if (i==activeNavCell) {
			currentCellBorderColor = activeNavCellBorderColor;
		}
		else {
			currentCellBorderColor = navCellBorderColor;
		}
		currentCell = 'cell'+ i;
		if (i==0) {
			document.getElementById(currentCell).style.borderRight = '1px solid '+ currentCellBorderColor;
		}
		else if (i==(numNavCells-1)) {
			document.getElementById(currentCell).style.borderLeft = '1px solid '+ currentCellBorderColor;
		}
		else {
			document.getElementById(currentCell).style.borderLeft = '1px solid '+ currentCellBorderColor;
			document.getElementById(currentCell).style.borderRight = '1px solid '+ currentCellBorderColor;
		}
	}
	document.getElementById(activeNavCellID).style.backgroundColor = activeCellBgColor;
}

function cellInitBgColorMSIE(activeNavCell,numNavCells) {
	var activeNavCellID = 'cell'+ activeNavCell;
	var navCellBorderColor = '#888844';
	var activeNavCellBorderColor = '#BBBB88';
	var activeCellBgColor = '#BBBB88';
	var currentCell;
	var currentCellBorderColor;
	for (var i=0; i<numNavCells; i++) {
		if (i==activeNavCell) {
			currentCellBorderColor = activeNavCellBorderColor;
		}
		else {
			currentCellBorderColor = navCellBorderColor;
		}
		currentCell = 'cell'+ i;
		if (i==0) {
//			document.all[cell0].style.borderRight = '1px solid'+ currentCellBorderColor;
			document.getElementById(currentCell).style.borderRight = '1px solid '+ currentCellBorderColor;
		}
		else if (i==(numNavCells-1)) {
			document.getElementById(currentCell).style.borderLeft = '1px solid '+ currentCellBorderColor;
		}
		else {
			document.getElementById(currentCell).style.borderLeft = '1px solid '+ currentCellBorderColor;
			document.getElementById(currentCell).style.borderRight = '1px solid '+ currentCellBorderColor;
		}
	}
	document.getElementById(activeNavCellID).style.backgroundColor = activeCellBgColor;
}

function mouseOutBgColor(activeCell,currentCell) {
	if(activeCell==currentCell) {
		bgColor='#BBBB88';
	} else {
		bgColor='#666633';
	}
	return bgColor;
}

function mouseOutBorderColor(activeCell,currentCell) {
	if(activeCell==currentCell) {
		borderColor='1px solid #BBBB88';
	} else {
		borderColor='1px solid #888844';
	}
	return borderColor;
}

