// JavaScript Document
<!-- hide script from old browsers
//------------XML Data-------------------//

//FUNCTION to READ XML DATA FILE
function parseXMLfile(file,node){
	var xmlDoc;
	if (window.XMLHttpRequest)
 	 {
  	xhttp=new window.XMLHttpRequest();
  	}
	else // for older IE 5/6
 	 {
  	xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  	}
	xhttp.open("GET",file,false);
	xhttp.send("");
	xmlDoc=xhttp.responseXML;
	x  = xmlDoc.getElementsByTagName(node);
	return x;
}
/*
OLD FUNCTION DEFINITION, did not work on Safari or Chrome	
		function parseXMLfile(file,node){
		var xmlDoc;
		if (window.ActiveXObject){// code for IE
			xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		}
		else if (document.implementation.createDocument){// code for Mozilla, Firefox, Opera, etc.
			xmlDoc=document.implementation.createDocument("","",null);
		}
		else{
			alert('Your browser cannot handle this script');
		}
		xmlDoc.async=false;
		xmlDoc.load(file); //Data file
		x  = xmlDoc.getElementsByTagName(node);
		return x;
	}
*/
	
//FUNCTION to Read an Data entry
// - requires global xml obj called x
	function getData(id, tag, x){
		//var xmlArr = "x"
		//return eval(xmlArr + "["+id+"]").getElementsByTagName(tag)[0].childNodes[0].nodeValue;
		return x[id].getElementsByTagName(tag)[0].childNodes[0].nodeValue; //uses global xml object named x	
	}

//------------Panoramic Viewer-------------------//
		
//FUNCTION to Create Viewer Applet
	function writePanApp(file,width,height,pan,panmin,panmax,fov,title,location,date,desc,tilt,tiltmin,tiltmax,horizPos,showTools,pwidth,pheight){
		document.write("<div align='center'>");
		document.write("<table class='imgHolder' border='1'><tr><td>"); //put in table so border can be shown
		document.write("<APPLET name='ptviewer' archive='../scripts/ptviewer.jar' code='ptviewer.class' width='"+width+"' height='"+height+"' mayscript='true'>");
			document.write("<param name=file 	value='"+file+"'>");
			document.write("<param name=cursor 	value='move'>");
			if(pan != "null")		{document.write("<param name=pan 	value='"+pan+"'>");}
			if(panmin != "null")	{document.write("<param name=panmin	value='"+panmin+"'>");}
			if(panmax != "null")	{document.write("<param name=panmax 	value='"+panmax+"'>");}
			if(fov != "null")		{document.write("<param name=fov 	value='"+fov+"'>");}
			if(tilt != "null")		{document.write("<param name=tilt 	value='"+tilt+"'>");}
			if(tiltmin != "null")	{document.write("<param name=tiltmin 	value='"+tiltmin+"'>");}
			if(tiltmax != "null")	{document.write("<param name=tiltmax 	value='"+tiltmax+"'>");}
			if(horizPos != "null")	{document.write("<param name=horizonPosition 	value='"+horizPos+"'>");}
			if(showTools != "null")	{document.write("<param name=showToolbar 	value='"+showTools+"'>");}
			if(pwidth != "null")	{document.write("<param name=pwidth 	value='"+pwidth+"'>");}
			if(pheight != "null")	{document.write("<param name=pheight 	value='"+pheight+"'>");}	
		document.write("</APPLET>");
		document.write("</td></tr></table>"); //put in table so border can be shown
		document.write("</div>");
		
		writeFileInfo(title,location,date,desc);
		
		//document.write("<br />");
		//write panoramic info		
		//document.write("<font size='-4'> Panoramic info: " + file + "," + width + "," + height + "," + pan + "," + panmin  + "," + panmax + "," + fov + "</font>");
	}
//FUNCTION to Create Viewer Applet from Data
	function writePanAppByID(id, x){
		var file = getData(id, "file", x);
		var width = getData(id, "width", x);
		var height = getData(id, "height", x);
		var pan = getData(id, "pan", x);
		var panmin = getData(id, "panmin", x);
		var panmax = getData(id, "panmax", x);
		var fov = getData(id, "fov", x);
		var title = getData(id, "title", x);
		var location = getData(id, "location", x);
		var date = getData(id, "date", x);
		var desc = getData(id, "desc", x);
		var tilt = getData(id, "tilt", x);
		var tiltmin = getData(id, "tiltmin", x);
		var tiltmax = getData(id, "tiltmax", x);
		var horizPos = getData(id, "horizPos", x);
		var showTools = getData(id, "showTools", x);
		var pwidth = getData(id, "pwidth", x);
		var pheight = getData(id, "pheight", x);
		writePanApp(file,width,height,pan,panmin,panmax,fov,title,location,date,desc,tilt,tiltmin,tiltmax,horizPos,showTools,pwidth,pheight);
	}

//------------Swf (Flash Movie) Writer -------------------//
	function writeSwfByID(id, x){
		var file = getData(id, "file", x);
		var width = getData(id, "width", x);
		var height = getData(id, "height", x);	
		var title = getData(id, "title", x);
		var location = getData(id, "location", x);
		var date = getData(id, "date", x);
		var desc = getData(id, "desc", x);
		
		writeSwf(file,width,height,title,location,date,desc);	
	}
	function writeSwf(file,width,height,title,location,date,desc){
		document.write("<div align='center'>");
		document.write("<table class='imgHolder' ><tr><td>"); //put in table so border can be shown
		document.write("<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0' width='"+width+"' height='"+height+"'>");
  		document.write("<param name='movie' value='"+file+"'>");
  		document.write("<param name='quality' value='high'>");
  		document.write("<embed src='"+file+"' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width='"+width+"' height='"+height+"'></embed>");
		document.write("</object>");
		document.write("</td></tr></table>"); //put in table so border can be shown
		document.write("</div>");
		
		writeFileInfo(title,location,date,desc); //write info
	}
	
//------------Image Writer -------------------//
//FUNCTION writes an image from external data
	function writeImageByID(id, x){
		var file = getData(id, "file", x);
		var width = getData(id, "width", x);
		var height = getData(id, "height", x);	
		var title = getData(id, "title", x);
		var location = getData(id, "location", x);
		var date = getData(id, "date", x);
		var desc = getData(id, "desc", x);		
		
		writeImage(file,width,height,title,location,date,desc,id,3);
	}
//FUNCTION  writes an image file
	function writeImage(file,width,height,title,location,date,desc,id){
		//Build title string
		var txt = ""
		if(title != "null"){ txt = txt + title;}
		if(location != "null"){ txt = (txt == "") ? location : txt + " - " + location;}
		if(date != "null"){ txt = (txt == "") ? date : txt + " - " + date;}
		
		//document.write("<center>");
		document.write("<table class='imgHolder' align='center'><tr><td>");
		
		document.write("<div align='center' "); //<div style="overflow:auto;width:800px;height:100px;">
		document.write("style='overflow:auto;");
		if(width != "null"){ document.write("width:" + width + "px;")}
		if(height != "null"){ document.write("height:" + width + "px;")}
		document.write("'>");
		
		document.write("<img class='plain' src='"+file+"' alt='"+txt+"' title='"+txt+"' border='0' onClick='nextImg("+id+")'>"); //write image (use actul img dims).
		document.write("</div>");
		document.write("</td></tr></table>");
		//document.write("</center>");
		
		writeFileInfo(title,location,date,desc); //write info
		
	}
	
//------------Blog Writer -------------------//
//FUNCTION writes blog from external data
	function writeBlogByID(id, x){
		var file = getData(id, "file", x);
		var title = getData(id, "title", x);
		var date = getData(id, "date", x);
		var desc = getData(id, "desc", x);		
		
		writeBlog(file,title,date,desc,id);
	}
//FUNCTION  writes a blog entry
	function writeBlog(file,title,date,desc,id){
		
		//write header
		if(title != "null"){
			document.write("<p align='left'>");
			document.write("<strong>" + title + "</strong>"); //write title
			if(date != "null"){
				document.write("<br />");
				document.write(date);//write date
			}
			document.write("</p>");
		}else{
			if(date != "null") {document.write(date);}
		}
		
		//write image (use actul img dims).
		if(file != "null"){
		document.write("<p align='left'>");
		document.write("<img src='"+file+"' alt='"+title+"' title='"+title+"' border='0' onClick='nextImg("+id+")'>");
		document.write("</p>");
		}
		
		//write description
		document.write(desc);		
	}
	

//FUNCTION nextImg(id) - moves slideshow to location to next image (previous index)
// requires global xml object with name x
	function nextImg(id){
		var n = x.length;
		var nextImg = Number(id) - 1;
		if(nextImg < 0){nextImg = n - 1;}
		window.location = "index.htm?" + nextImg; 
	}
	
//FUNCTION  writes a thumbnail image file, with hyperlink	
	function writeThumb(file,href,alt){
		if(alt == "null"){alt= "";}
		document.write("<div align='center'>"); //write formatting
		document.write("<a href='"+href+"'>"); //write link
		document.write("<img src='"+file+"' alt='"+alt+"' title='"+alt+"' border='0'>"); //write image
		document.write("</a>"); 
		document.write("</div>");
	}

//------------Index Table Writer -------------------//
function index(i,j,c){
	return String(i) + ", " + String(j) + " c=" + c;
}
//FUNCTION  writes a thumbnail table, with hyperlinks to viewer
//Requires XML data lookup
function createThumbTable(cols,nitems,x){
	var c = 0; //cell counter
	var m = cols;
	var n = nitems / cols;
	var rem = nitems % cols;
	if(rem > 0){n++;} //add extra row if needed
	
	//document.write("<table width='75%' border='1' cellspacing='1' cellpadding='1'>");
	document.write("<table align='center'>");  
  	for(i=0;i<=n-1;i++){ //row
  		document.write("<tr>");		
		for(j=0;j<=m-1;j++){//col
  			if(c <= nitems-1){			
				document.write("<td>");
				//FILL CELL HERE
					//FIND Thumb Image
				var id = (nitems - 1) - c; //reverse the index value (show oldest first)
				var thumbFile = getData(id, "thumb", x);
				var alt = getData(id, "title", x);
					//Write Thumb
				writeThumb(thumbFile,"index.htm?" + id, alt);
				document.write("<br />");
				//document.write(index(i,j,c)); //writes table identifier info (e.g. matrix n,m vals & cell count index
				//FILL CELL END 
				c++;
				document.write("</td>");
			}
		}
		document.write("</tr>");
  	}
	document.write("</table>");
}

//------------Logic-------------------//
	function chooseAndWriteImage(id,type, x){
		//id refers to data file index
		if(type == "img"){			writeImageByID(id, x);}//write image
		else if(type == "pan"){		writePanAppByID(id, x);}//write panoramic viewer applet
		else if(type == "swf"){		writeSwfByID(id, x);} //write flash swf object to file
		else if (type == "blog"){	writeBlogByID(id, x);} //write blog entry
	}

//------------General-------------------//

//FUNCTION write control bar	
	function writeControlBar(id,n){
		//calculate controller varialbes
		//note: keeping newest work first
		var idFirst = Number(n) - 1; //take latest record first
		var idLast = 0; //put oldest records at the end
		var nof = Number(n) - id;
		
		var idPrev = Number(id) + 1;
		if(idPrev > n-1){idPrev = idLast;}
		
		var idNext = Number(id) - 1;
		if(idNext < 0){idNext = idFirst;}
		
		//back link
		//document.write("<p align='center' style='line-height:30%'>Back to <a href='../index.htm'>Geoff's Homepage</a></p>");
				
		//format
		document.write("<p align='center' style='line-height:1'>");
		//index btn
		document.write("<a href='index.htm'>Back to Gallery</a>");
		document.write( " : ");	
		//first btn
		document.write("<a href='index.htm?" + idFirst + "' title='Go to First (newest) image in gallery'>First</a>");
		document.write(" : ");
		//prev btn
		document.write("<a href='index.htm?" + idPrev + "' title='Go to Previous (newer) image in gallery'>Prev</a>");
		document.write(" : ");
		//next btn
		document.write("<a href='index.htm?" + idNext + "' title='Go to Next (older) image in gallery'>Next</a>");
		document.write(" : ");
		//last btn
		document.write("<a href='index.htm?" + idLast + "' title='Go to Last (oldest) image in gallery'>Last</a>");
		document.write(" : ");		
		//# of # display
		document.write("(" + String(nof) + " of " + n +")");
		document.write("</p>");	
	}

//FUNCTION write file info
	function writeFileInfo(title,location,date,desc){
		var txt = ""
		if(title != "null")		{txt = txt + title;}
		if(location != "null")	{txt = (txt == "") ? location : txt + " - " + location;}
		if(date != "null")		{txt = (txt == "") ? date : txt + " - " + date;}
		document.write("<p align='center'>");
		if(txt != ""){
			document.write(txt);
		}
		document.write("</p>");
		if(desc != "null"){
			document.write("<p>");		
			document.write(desc);
			document.write("</p>");
		}
	}
	

// end hiding script from old browsers -->
