// ======================================================================================
// This Photo Gallery written by Courts Carter, pizzabytheslice.com, v2.3.1 (1999-2007)
// Use, distribute, modify (improve. please?) according to Creative Commons agreement:
// http://creativecommons.org/licenses/by-nc/2.0/
// ======================================================================================
// 
//			DO NOT DIRECTLY ACCESS ANY VARIABLE
//			USE "GETTER" OR "SETTER" FUNCTIONS
// ---------------------------------------------------


// ==================================================================
// OBJECT:	pbtsImg
// Scope:	semi-public
// USE:		 
// DOES:	 storage
// ==================================================================
function pbtsImg(){
	this.src		= null; // image file names, for the src attribute of image tag; will be concatenated to thumb/fullsize URL
	this.descr	= "";
	this.width	= "";
	this.height	= "";
	this.title	= "";
	this.extra1	= "";
	this.extra2	= "";
	this.extra3	= "";
	this.extra4	= "";
	this.date		= "";
	// for thumbs:
	this.img			= new Image();
	this.isLoaded	= false; // cofirmed that image has been loaded... used with timer event
} // pbtsImg (object)

// array of pbtsImg objects, image attributes, flags, and image object
var g_aryImgs				= new Array(0);
var g_defaults			= new pbtsImg();// fills-in for missing image attributes
g_defaults.height	= 1;
g_defaults.width	= 1;
// slideshow
var g_isSshow				= false,// toggle
		g_SshowIntId	 	= null, // timer interval id/handle
		g_SshowLoopIdx 	= 0;		// counts times through the slideshow
var g_thisImg, 
		g_nextImg,
		g_prevImg,
		g_lastImg;

var g_ThmsPerPg,
		g_firstImgThisPg;
var g_isShowingAll= false, // true when the Thum Grid is showing all images
		g_isAutoScroll= false; // if true and showAll set then the page is scrolled to show the image when thumb image is clicked.
var g_aryAllowed	= new Array("file","description","width","height","title","date","extra1","extra2","extra3","extra4");
var g_aryAddArg	 	= new Array(0);
g_aryAddArg	= g_aryAddArg.concat(g_aryAllowed);

var g_callbackFn	 = function(){ return true;};
var g_SshowDelay	 = 4000; // delay in miliseconds
// ElementIds, if blank = not shown
var g_EID_Info		= "", // File Info
		g_EID_Img			= "theimage", // Image
		g_EID_Descr		= "imgdescr", // Image Description/Caption
		g_EID_Title		= "",
		g_EID_Extra1	= "",
		g_EID_Extra2	= "",
		g_EID_Extra3	= "",
		g_EID_Extra4	= "",
		g_EID_Date		= "",
		g_EID_PgNav		= "", // Thumbnail page index
		g_EID_Thms		= "", // Area containing the thumbnails
		g_EID_Link		= ""; // Perma-link to a specific image
var g_ThmDir			= "images/photos/thumbs/", 
		g_ImgDir			= "images/photos/", 
		g_HTMLThmPg		= "",
		g_HTMLImgPg		= "",
		g_Spacer			= "images/shim.gif", 
		g_loadingImg	= g_Spacer;
var g_ThmWidth		= 175,
		g_ThmClass		= "", // 
		g_ThmEmptyClass= "", // 
		g_ThmSpacing	= 2,
		g_ThmCols			= 3, 
		g_ThmRows			= 3,
		g_isThmTitles	= false,
		g_isThmAll		= false;
var g_ThmPrefix		= ""; // 
var g_isWrap			= true,
		g_isRotate90	= false;
var g_isSetDescrWidth= false;
		
// ====================================================================================================
//	 P R I V A T E
// ====================================================================================================

// ==================================================================
// FUNCTION: pbtsReadURLIdx
// scope:		private
// USE:			all
// DOES:		 looks at URL parameters, accepts either "img=???", where ??? is index of image within 
//					 this gallery or "file=???", where ??? is image filename
// RETURNS:	index of image requested in the URL
// PARAMS:	 none
// ==================================================================
function pbtsReadURLIdx() {
	var tempStr=new String(window.location.search).toLowerCase();
	var id=0;
	if (tempStr.substr(1,3)=="img"){
		// strip "img=" from URL
		tempStr= new String( tempStr.substr( 5, tempStr.length-5 ));
		if (tempStr.length < 1)
			tempStr= "0";
		else if (isNaN(tempStr))
			alert("\""+tempStr+"\" is not a valid number.");
		else
			id= parseInt( tempStr );
		if (id >= g_aryImgs.length)
			id = (g_aryImgs.length-1);
	} else if (tempStr.substr(1,4)=="file") {
		// stip "file=" from URL
		var found=false;
		tempStr= tempStr.substr( 6, tempStr.length-6 );
		for (var i=0;i < g_aryImgs.length; i++) {
			if (g_aryImgs[i].src.toLowerCase() == tempStr) {
				id=i;
				found=true;
				break;
			} // if
		} // for
		if (!found)
			alert( "Sorry, did not find file \""+ tempStr+"\".");
	} // if URL handler
	return	id;
} // pbtsReadURLIdx

// ==================================================================
function pbtsArrayExists(p_find,p_array){
	var found=false;
	for (var j=0;j <= p_array.length; j++) {
		if (p_find== p_array[j]) {
			found=true;
			break;
		}// if
	}// for
	return found;
}// pbtsArrayExists


// ==================================================================
// FUNCTION: pbtsTimeLoadImg
// SCOPE:		PRIVATE
// ==================================================================
function pbtsTimeLoadImg(){
	if (g_aryImgs[g_thisImg].img.complete) {
		document.getElementById(g_EID_Img).src= g_aryImgs[g_thisImg].img.src;
		g_aryImgs[g_thisImg].isLoaded= true;
		clearInterval(g_TimerId);
		g_callbackFn( g_thisImg );	
	}
} // pbtsTimeLoadImg


// ==================================================================
// FUNCTION: pbtsInitImgPg
// SCOPE:		PRIVATE
// USE:			Call this AFTER the setters on an image page
// DOES:		 
// RETURNS:	none
// PARAMS:	 none
// ==================================================================
function pbtsInitImgPg() {
	g_nextImg= g_thisImg + 1 ;
	g_prevImg= g_thisImg - 1 ;
	g_lastImg= g_aryImgs.length-1;
	switchPic( g_thisImg, false );
} // pbtsInitImgPg


// ==================================================================
// FUNCTION: pbtsInitThmbPg
// SCOPE:		PRIVATE
// USE:			Call this AFTER the setters on an thumbnail page
// DOES:		 xxxx
// RETURNS:	none
// PARAMS:	 none
// ==================================================================
function pbtsInitThmbPg() {
	g_ThmsPerPg= (g_ThmCols * g_ThmRows);
	g_lastPage = Math.ceil( g_aryImgs.length/ g_ThmsPerPg);
	pbtsGetFirstIdx();
	pbtsWritePgNav();
	pbtsWriteThmGrid();
} // pbtsInitThmbPg

// ==================================================================
// FUNCTION: pbtsGetFirstIdx
// USE:			
// DOES:		 Using the Current Image Index calculate which thumbnail page it falls on, return the 
//					 index of the first image on this page
//					 Sets global g_firstImgThisPg 
// RETURNS:	see above
// PARAMS:	 NONE
// ==================================================================
function pbtsGetFirstIdx() {
	if ((g_thisImg % g_ThmsPerPg) == 0) {
		g_firstImgThisPg = g_thisImg;
	} else {
		g_firstImgThisPg = g_thisImg - (g_thisImg % g_ThmsPerPg);
	}
	return g_firstImgThisPg;
} // pbtsGetFirstIdx

// ==================================================================
// FUNCTION: pbtsWriteThmGrid
// USE:			thumbnail page
// DOES:		 
// NOTES:		chooses min of width or height
// RETURNS:	none
// PARAMS:	 change:
//						OLD: ElementId (optional) for area containing the thumbnails
//						NEW: isShowAll (optional) if true shows all thumbs
// ==================================================================
function pbtsWriteThmGrid() {
	var innerHTML= "";
	var title="";
	var start, stop;
	var isOnePg = (g_HTMLThmPg == g_HTMLImgPg); // 
	var spcHt= 0;
	var spcWd= 0;
	if (g_defaults.width > g_defaults.height){
		spcWd = g_ThmWidth;
		spcHt = Math.round(g_ThmWidth*(g_defaults.height/g_defaults.width));
	} else {
		spcWd = Math.round(g_ThmWidth*(g_defaults.width/g_defaults.height));
		spcHt = g_ThmWidth;
	}

	g_isShowingAll= (arguments.length > 0)?arguments[0]:false;
	
	if (g_isShowingAll) {
		start= 0; 
		stop=	g_aryImgs.length;
	} else {
		start= g_firstImgThisPg; 
		stop=	g_firstImgThisPg+g_ThmsPerPg;
	}

	for (var i=start; i < stop; i++ ) {

		if ((i % g_ThmCols) == 0) 
			innerHTML += "<tr>";

		if (i < g_aryImgs.length) {
			if ((g_isThmTitles) && (g_aryImgs[i].title.length > 0))
				title= "<br>"+g_aryImgs[i].title;
			else
				title= "";
			innerHTML += "<td valign=top \""+g_ThmClass+"\">";
			if (g_aryImgs[i].width > g_aryImgs[i].height) {
				if (isOnePg)
					innerHTML += "<a href='javascript:switchPic("+i+");' title=\"click for larger image\">";
				else 
					innerHTML += "<a href='"+g_HTMLImgPg+"?img=" + i + "' title=\"click for larger image\">";
				innerHTML +="<img src=\"" +g_ThmDir+ g_ThmPrefix+ g_aryImgs[i].src+ "\" border=0 width="+g_ThmWidth+"></a>"+title+"</td>";
			} else {
				if (isOnePg)
					innerHTML += "<a href='javascript:switchPic("+i+");' title=\"click for larger image\">";
				else 
					innerHTML += "<a href='"+g_HTMLImgPg+"?img=" + i + "' title=\"click for larger image\">";
				innerHTML +="<img src=\"" +g_ThmDir+ g_ThmPrefix+ g_aryImgs[i].src+ "\" border=0 height="+g_ThmWidth+"></a>"+title+"</td>";
			}
		} else {
			innerHTML += "<td valign=top "+g_ThmEmptyClass+" ><img src=\""+g_Spacer+"\" width="+spcWd+" height="+spcHt+"></td>";
		} // if

		if ((i % g_ThmCols) == (g_ThmCols-1)) 
			innerHTML += "</tr>";

	} // for

	document.getElementById(g_EID_Thms).innerHTML= "<table cellspacing='"+g_ThmSpacing+"'>"+ innerHTML+ "</table>";

} // pbtsWriteThmGrid

// ==================================================================
// FUNCTION: pbtsKeyEvent
// SCOPE:		PRIVATE
// USE:			
// DOES:		 handles keyPress events
// HISTORY:	modified detector code from: www.howtocreate.co.uk
// RETURNS:	true
// PARAMS:	 
// ==================================================================
function pbtsKeyEvent(p_e) {
	var action= 0;
	if (typeof(p_e) == "undefined") // IE likes to use omnipresent "event"
		p_e=event; 

	if( typeof( p_e.which ) == "number") //NS 4, NS 6+, Mozilla 0.9+, Opera
		action = p_e.which;
	else if( typeof( p_e.keyCode ) == "number") //IE, NS 6+, Mozilla 0.9+
		action = p_e.keyCode;
	else if( typeof( p_e.charCode ) == "number") //also NS 6+, Mozilla 0.9+
		action = p_e.charCode;
	else
		action="";

	switch (action) { 
		case	72: //	Help: H
		case 104:
			showHelp();
			break;
		case	83: //	Start/Stop Slideshow: S
		case 115:
			if (!g_isSshow)
				startSlideshow();
			else
				stopSlideshow();
			break;
		case	80: //	Previous: P
		case 112:
			gotoPrevPic();
			break;
		case	78: //	Next:	N
		case 110:
			gotoNextPic();
			break;
		case	76: // Last/End: "L" 
		case 108: // "l"
			gotoLastPic();
			break;
		case	70: // First/Home: "F"
		case 102: // "f"
			gotoFirstPic();
			break;
		case	85: //	page Up: U
		case 117:
			if ((g_firstImgThisPg+g_ThmsPerPg) < g_aryImgs.length) {
				g_firstImgThisPg=g_firstImgThisPg+g_ThmsPerPg;
				pbtsWriteThmGrid();
			}
			break;
		case	68: //	page Down: D
		case 100:
			if (g_firstImgThisPg >= g_ThmsPerPg) {
				g_firstImgThisPg=g_firstImgThisPg-g_ThmsPerPg;
				pbtsWriteThmGrid();
			}
			break;
		default: 
			// numbers 0-9 {1-9 & 0=10}
			if ((action >= 48) && (action<=57)){
				var i	= action-48;
				if (i > 0)
					i--;
				else
					i=9; // press 0 for tenth image
				if ((g_firstImgThisPg+i) < g_aryImgs.length) {
					switchPic(g_firstImgThisPg+i, false);
					} // inbounds
				} // 0-9 pressed
			// end:default
	} // switch

	return true;
} //pbtsKeyEvent

// ==================================================================
// FUNCTION: pbtsWritePgNav
// USE:			thumbnail page
// DOES:		 xxxx
// RETURNS:	xxxx
// PARAMS:	 ElementID (optional) where innerHTMl will write the thumbnail page links
// ==================================================================
function pbtsWritePgNav() {
	var innerHTML= "";

	if (arguments.length > 0)
		g_EID_PgNav= arguments[0];

	if (g_EID_PgNav == "")
		return;

	for (var i=1; i <= g_lastPage; i++ ) {
		if ((g_firstImgThisPg >= ((i-1)*g_ThmsPerPg)) && (g_firstImgThisPg <= ((i*g_ThmsPerPg)-1)))
			innerHTML += " <b>" + i + "</b> ";
		else
			innerHTML +=" <a href='javascript:rePgIdx(" + ((i-1)*g_ThmsPerPg) + ");' title=\"page "+i+" thumbnails\">" + i + "</a> ";
	} // for
	if (g_isThmAll && (g_lastPage > 1))
		innerHTML +="&nbsp;<a href='javascript:showAll();' title=\"show all thumbnails\">all</a> ";
	document.getElementById(g_EID_PgNav).innerHTML= innerHTML;

} // pbtsWritePgNav

// ==================================================================
// FUNCTION: pbtsGetLink
// DOES:		 returns string, a Link to image
// RETURNS:	string
// PARAMS:	 index
// ==================================================================
function pbtsGetLink(i){
	var s=new String(document.location);
	var re=/^([^?]+)\?/
	if (s.match(re)){
		var m=s.match(re);
		s=m[1];
	}
	return "<a href=\""+s+"?file="+g_aryImgs[i].src+"\">link</a>";
}

// ====================================================================================================
//	 W R I T T E N
// ====================================================================================================

// ==================================================================
// FUNCTION: rePgIdx
// USE:			thumbnail page
// DOES:		 rewrite page index
// RETURNS:	
// PARAMS:	 
// ==================================================================
function rePgIdx(p_imgIdx) {
	g_thisImg= p_imgIdx;
	pbtsGetFirstIdx();
	pbtsWritePgNav();
	pbtsWriteThmGrid();
	if (g_HTMLThmPg == g_HTMLImgPg)
		switchPic( p_imgIdx, false );
	} // rePgIdx

// ==================================================================
// FUNCTION: switchPic
// USE:			image page
// DOES:		 xxxx
// RETURNS:	xxxx
// PARAMS:	 p_idx: index of image to show
//					 p_scroll (default true) deteremines whether the page should scroll to the image IF AutoScroll is TRUE:
// ==================================================================
function switchPic( p_idx ) {
	var pic= document.getElementById(g_EID_Img);
	var isScroll= g_isAutoScroll && g_isShowingAll;
	if (arguments.length > 1)
		isScroll= isScroll && arguments[1];
	if (isScroll)
		window.scrollTo(0,pic.top);
	if (p_idx < 0) {
		if (g_isWrap)
			switchPic(g_lastImg);
		else
			alert( "this is the first picture" );
	} else if (p_idx > g_lastImg) {
		if (g_isWrap)
			switchPic(0);
		else
			alert( "this is the first picture" );
	} else {
		// to avoid ugly resizing 'blank' the image before changing dimensions
		if (!g_aryImgs[p_idx].isLoaded)
			pic.src = g_loadingImg;
		else
			pic.src= g_ImgDir + g_aryImgs[p_idx].src;

		// 
		pic.width	= g_aryImgs[p_idx].width;
		pic.height = g_aryImgs[p_idx].height;
		if (g_EID_Descr != "") {
			if (g_isSetDescrWidth)
				document.getElementById(g_EID_Descr).innerHTML= "<p style=\"width:" +g_aryImgs[p_idx].width+ "px;\">"+g_aryImgs[p_idx].descr+"</p>";
			else
				document.getElementById(g_EID_Descr).innerHTML= g_aryImgs[p_idx].descr;
		}
		if (g_EID_Info != "") document.getElementById(g_EID_Info).innerHTML= "file: "+ g_aryImgs[p_idx].src +" (" +g_aryImgs[p_idx].width+ "W x "+ g_aryImgs[p_idx].height+"H)";
		if (g_EID_Title	!= "") document.getElementById(g_EID_Title).innerHTML = g_aryImgs[p_idx].title;
		if (g_EID_Extra1 != "") document.getElementById(g_EID_Extra1).innerHTML= g_aryImgs[p_idx].extra1;
		if (g_EID_Extra2 != "") document.getElementById(g_EID_Extra2).innerHTML= g_aryImgs[p_idx].extra2;
		if (g_EID_Extra3 != "") document.getElementById(g_EID_Extra3).innerHTML= g_aryImgs[p_idx].extra3;
		if (g_EID_Extra4 != "") document.getElementById(g_EID_Extra4).innerHTML= g_aryImgs[p_idx].extra4;
		if (g_EID_Link != "") document.getElementById(g_EID_Link).innerHTML= pbtsGetLink(p_idx);
		if (g_EID_Date	 != "") {
			if (g_aryImgs[p_idx].date!="")
				document.getElementById(g_EID_Date).innerHTML	= "taken :"+g_aryImgs[p_idx].date;
			else
				document.getElementById(g_EID_Date).innerHTML	= "";
		}

		g_thisImg= p_idx;
		g_nextImg= p_idx + 1 ;
		g_prevImg= p_idx - 1 ;

		// now all is in place to either exit or download the image.
		if (!g_aryImgs[p_idx].isLoaded) {
			g_aryImgs[p_idx].img.src = g_ImgDir + g_aryImgs[p_idx].src;
			g_TimerId = setInterval("pbtsTimeLoadImg()", 250 );
		} else {
			g_callbackFn( p_idx );
		}
	} // if
} // switchPic

// ====================================================================================================
//	 D E V E L O P M E N T
// ====================================================================================================

// ==================================================================
// FUNCTION: writeMaxWidthShim
// USE:			detail page
// DOES:		 xxxx
// RETURNS:	xxxx
// PARAMS:	 xxxx
// ==================================================================
function writeMaxWidthShim(p_id) {
	var maxWidth=0;// 
	for (var i=g_firstImgThisPg; i< (g_firstImgThisPg+g_ThmsPerPg); i++ ) {
		if (i < g_aryImgs.length) {
			if (g_aryImgs[i].width > maxWidth)
				maxWidth= g_aryImgs[i].width;
		} // if
	} // for
	document.getElementById(p_id).innerHTML= "<img src=\""+g_Spacer+"\" width="+maxWidth+" height=\"1\">";
} // writeMaxWidthShim


// ====================================================================================================
//		 P U B L I C
// ====================================================================================================

// ==================================================================
// FUNCTION: addImg
// USE:			all
// DOES:		 adds an image
// RETURNS:	index of the new Image item
// PARAMS:	 g_aryAllowed, unless you've re-ordered the list
// ==================================================================
function addImg(){
	if (arguments.length > g_aryAddArg.length) {
		alert( "Photo Gallery function \"addImg\" received too many arguments. Call had "+arguments.length+" arguments.");
		return false;
	}//if

	var tempImg= new pbtsImg;
	tempImg.src		 	= "";
	tempImg.descr		= g_defaults.descr;
	tempImg.title		= g_defaults.title;
	tempImg.date		= g_defaults.date;
	tempImg.extra1	= g_defaults.extra1;
	tempImg.extra2	= g_defaults.extra2;
	tempImg.extra3	= g_defaults.extra3;
	tempImg.extra4	= g_defaults.extra4;
	tempImg.isLoaded= false;

	if (g_isRotate90) {
		tempImg.width = g_defaults.height;
		tempImg.height= g_defaults.width;
		g_isRotate90	= false;
	} else {
		tempImg.width		= g_defaults.width;
		tempImg.height	= g_defaults.height;
	}

	for (var i=0; i < arguments.length; i++) {
		if (arguments[i]!=null) {
			switch (g_aryAddArg[i]){
				case "file"		: tempImg.src	 = arguments[i]; break;
				case "description": tempImg.descr = arguments[i]; break;
				case "width"	: tempImg.width = arguments[i]; break;
				case "height"	: tempImg.height= arguments[i]; break;
				case "title"	: tempImg.title = arguments[i]; break;
				case "date"		: tempImg.date	= arguments[i]; break;
				case "extra1"	: tempImg.extra1= arguments[i]; break;
				case "extra2"	: tempImg.extra2= arguments[i]; break;
				case "extra3"	: tempImg.extra2= arguments[i]; break;
				case "extra4"	: tempImg.extra4= arguments[i]; break;
			}// switch
		}//if null skip
	}// for

	g_aryImgs[g_aryImgs.length]=tempImg;
	return g_aryImgs.length;

} // addImg


// ==================================================================
// FUNCTION: addImg90
// USE:			all
// DOES:		 adds an image, 90 degree rotation (portrait v. landscape)
// RETURNS:	see addImg
// PARAMS:	 see addImg
// ==================================================================
function addImg90(){
	var i=0;
	var p_ary= new Array( g_aryAllowed.length );
	for (i=0;i < g_aryAllowed.length; i++) p_ary[i]=null;
	for (i=0;i < arguments.length; i++) p_ary[i]=arguments[i];
	g_isRotate90= true;
	return addImg( p_ary[0],p_ary[1],p_ary[2],p_ary[3],p_ary[4],p_ary[5],p_ary[6],p_ary[7],p_ary[8],p_ary[9] );
} // addImg90

// ==================================================================
// FUNCTION: goto[Prev | Next | First | Last ]Pic
// USE:			image page
// DOES:		 switches picture
// RETURNS:	xxxx
// PARAMS:	 none
// ==================================================================
function gotoPrevPic(){return switchPic( g_prevImg, false );}
function gotoNextPic(){return switchPic( g_nextImg, false );}
function gotoFirstPic(){return switchPic(0, false);}
function gotoLastPic(){return switchPic( g_lastImg, false );}

// ==================================================================
// FUNCTION: gotoThumbs
// USE:			image page
// DOES:		 changes page to ...
// RETURNS:	none
// PARAMS:	 none
// ==================================================================
function gotoThumbs() { 
	window.location = g_HTMLThmPg +"?img="+ g_thisImg ;
	return false;
} //gotoThumbs

// ==================================================================
function showHelp() {
	alert( "Photo Gallery Help (v2.3.1)\nwww.pizzabytheslice.com\n\nF= First image\nL= Last\n\nN= Next Image\nP=Previous\n\n1-9,0 enlarge thumbnail 1-10\n\nS= Start/Stop Slideshow (delay is "+ (g_SshowDelay/1000)+ " seconds)\n\nU= page Up (next thumbnail page)\nD= page Down"	);
} // showHelp

function showAll() { pbtsWriteThmGrid(true);}

// ==================================================================
function startSlideshow() {
	stopSlideshow();
	g_SshowIntId= window.setInterval( "gotoNextPic()", g_SshowDelay );
	g_isSshow= true;
	g_SshowLoopIdx= 0;
} // startSlideshow

function stopSlideshow() {
	if (g_isSshow) {
		g_isSshow= false;
		clearInterval(g_SshowIntId);
	}
} // stopSlideshow

// DOES:		starts/stops slideshow. 
// RETURNS: the new slideshow state; true if slideshow has been started, false if it has been STOPPED.
function toggleSlideshow() {
	if (g_isSshow)
		stopSlideshow();
	else
		startSlideshow();
	return g_isSshow;
} // toggleSlideshow

// ==================================================================
function enableQuickKeys() {
	document.onkeypress= pbtsKeyEvent;
} // enableQuickKeys

// ==================================================================
// FUNCTION: writeGallery
// USE:			All. Call AFTER you've called all of your "setters"
// DOES:		 xxxx
// RETURNS:	xxxx
// PARAMS:	 imageIndex (optional) if missing reads index of current Image from the URL
// ==================================================================
function writeGallery() {
	var tempStr=new String(window.location.pathname).toLowerCase();
	var thm= new String(g_HTMLThmPg).toLowerCase();
	var img= new String(g_HTMLImgPg).toLowerCase();

	if (g_aryImgs.length < 1) {
		alert( "Photo Gallery function \"writeGallery\" failed because the gallery contains no images." );
		return false;
	}

	g_thisImg= (arguments.length > 0)?arguments[0]:pbtsReadURLIdx();

	if (thm.toString() == img.toString()) {
		pbtsInitThmbPg();
		pbtsInitImgPg();
	} else if ((thm.length > 0) && (tempStr.indexOf(thm.toString()) >= 0)) {
		pbtsInitThmbPg();
	} else if ((img.length > 0) && (tempStr.indexOf(img.toString()) >= 0)) {
		pbtsInitImgPg();
	} else {
		alert( "Photo Gallery function \"writeGallery\" failed to recognize Thumbnail or Image page" );
	}
	
} // writeGallery

// ==================================================================
// FUNCTION: reorderArgs
// USE:			all
// DOES:		 can change the add order at any time
// RETURNS:	true if successful
// PARAMS:	 attribute 
// ==================================================================
function reorderArgs() {
	var i=0,
			Str=new String,
			Ary=new Array(0);
	// validate attribes
	for (i=0;i<arguments.length;i++) {
		Str= arguments[i];
		Ary[Ary.length]=Str.toLowerCase();
		if (!pbtsArrayExists(Ary[i],g_aryAllowed)) {
			alert( "Photo Gallery function \"reorderArgs\" did not recognize attribute \""+Ary[i]+"\"" );
			return false;
		}// if
	}// for i
	// just add the missing args
	for (i=0;i<g_aryAllowed.length;i++){
		if (!pbtsArrayExists(g_aryAllowed[i],Ary))
			Ary[Ary.length]=g_aryAllowed[i];
	}// for
	// now copy 
	for (i=0;i<Ary.length;i++)
		g_aryAddArg[i]=Ary[i];
	return true;
} // reorderArgs


// ==================================================================
// FUNCTION: setElementId
// USE:			all
// DOES:		 
// RETURNS:	
// PARAMS:	 attribute, element's Id
// ==================================================================
function setElementId( p_attrb, p_id ) {
	switch (new String(p_attrb).toLowerCase()) {
		case "image"		: g_EID_Img			= p_id;break;
		case "description": g_EID_Descr	= p_id;break;
		case "title"		: g_EID_Title		= p_id;break;
		case "extra1"		: g_EID_Extra1	= p_id;break;
		case "extra2"		: g_EID_Extra2	= p_id;break;
		case "extra3"		: g_EID_Extra3	= p_id;break;
		case "extra4"		: g_EID_Extra4	= p_id;break;
		case "date"			: g_EID_Date	 	= p_id;break;
		case "info"			: g_EID_Info	 	= p_id;break;
		case "pages"		: g_EID_PgNav		= p_id;break;
		case "thumbs"		: g_EID_Thms	 	= p_id;break;
		case "link"			: g_EID_Link	 	= p_id;break;
		default:
			alert( "Photo Gallery function \"setElementId\" did not recognize attribute \""+p_attrb+"\"" );
			break;
	} // switch
}//setElementId


// ==================================================================
// FUNCTION: setDefault
// USE:			
// DOES:		 establishes the value to be used for all subsequent calls to addImg 
// RETURNS:	
// PARAMS:	 p_attribute, p_defaultValue 
// ==================================================================
function setDefault( p_attribute, p_defaultValue ){
	if (!pbtsArrayExists(p_attribute,g_aryAllowed)) {
		alert( "Photo Gallery function \"setDefault\" did not recognize attribute \""+p_attribute+"\"" );
		return false;
	}// if
	switch (p_attribute){
		case "description": g_defaults.descr= p_defaultValue; break;
		case "width"	: g_defaults.width	=	p_defaultValue; break;
		case "height"	: g_defaults.height	= p_defaultValue; break;
		case "title"	: g_defaults.title	=	p_defaultValue; break;
		case "date"		: g_defaults.date		=	p_defaultValue; break;
		case "extra1"	: g_defaults.extra1	= p_defaultValue; break;
		case "extra2"	: g_defaults.extra2	= p_defaultValue; break;
		case "extra3"	: g_defaults.extra3	= p_defaultValue; break;
		case "extra4"	: g_defaults.extra4	= p_defaultValue; break;
		default:
			alert( "Photo Gallery function \"setDefault\" \""+p_attribute+"\" does not accept defaults." );
			break;
	}// switch
} // setDefault


// ==================================================================
// FUNCTION: setters
// USE:			PUBLIC
// ==================================================================

// -------------------------------------------
/* file names and locations functions */
function setThumbDir(p_path){ g_ThmDir=p_path; }
function setImgDir(p_path){ g_ImgDir=p_path; }
function setThumbPage(p_val){ g_HTMLThmPg=p_val; }
function setImgPage(p_val){ g_HTMLImgPg=p_val; }
function setSpacer(p_val){ g_Spacer=p_val; }
function setLoadingImg(p_Image){ g_loadingImg=p_Image; }
function setThumbPrefix(p_pre){ g_ThmPrefix=p_pre; }

// -------------------------------------------
/* thumbnail appearance functions */
function setRows( p_rows ){ g_ThmRows=	p_rows;}
function setCols( p_cols ){ g_ThmCols=	p_cols;}
function setThumbClass(p_class){ g_ThmClass=" class='"+p_class+"'"; }
function setThumbEmptyClass(p_class){ g_ThmEmptyClass=" class='"+p_class+"'"; }
function setThumbWidth(p_val){ g_ThmWidth=p_val; }
function setThumbSpacing(p_val){ g_ThmSpacing=p_val; }
function setThumbTitles(p_on){ g_isThmTitles=p_on; }
function setThumbAll(p_on){ g_isThmAll=p_on; }
function setAutoScroll(p_on){ g_isAutoScroll=p_on; }
// -------------------------------------------
/* behavior and defaults functions */
function setDelay(p_miliseconds){ g_SshowDelay= p_miliseconds;}
function setWrap(p_on){ g_isWrap=p_on; }
function setDefDims( p_width, p_height ){ 
 if (p_width < 1) p_width=1;
 g_defaults.width=p_width; 
 if (p_height < 1) p_height=1;
 g_defaults.height=p_height;
}

// -------------------------------------------
/* Element Id setting functions */
function setImageId( p_id )		 	{ g_EID_Img		=	p_id;}
function setImageDescrId( p_id ){ g_EID_Descr	=	p_id;}
function setImageInfoId( p_id ) { g_EID_Info 	=	p_id;}
function setImageTitleId( p_id ){ g_EID_Title	=	p_id;}

function setThumbIndexId( p_id ){ g_EID_PgNav	=	p_id;}
function setThumbGridId( p_id ) { g_EID_Thms	=	p_id;}

// -------------------------------------------
// setOnChange 
function setCallback(p_fn){ g_callbackFn=p_fn;}

// ==================================================================
// setSetDescrWidth ... this is a temp kludge
// this will fix the width of the caption (i.e. the description) for an image to be the same as the image's width.
// this is useful when the image detail and thumbnails appear on the same page.
// ==================================================================
function setSetDescrWidth(p_on){ g_isSetDescrWidth=p_on; } 

// ==================================================================
// FUNCTION: getters (returns values of stuff)
// USE:			PUBLIC
// ==================================================================

function getCount() { return g_aryImgs.length; }

// ==================================================================
// FUNCTION: getIsSlideshow
// USE:			all
// RETURNS:	returns TRUE if currently in slideshow mode, false otherwise (slideshow has been stopped or never started)
// PARAMS:	 none
// ==================================================================
function getIsSlideshow() { return g_isSshow; }

// ==================================================================
// FUNCTION: getValue
// USE:			all
// DOES:		 
// RETURNS:	value of the specified attribute for the specified image (or current one, in none given)
// PARAMS:	 p_attribute
//					 index (optional)
// ==================================================================
function getValue( p_attribute ) {
	var i=(arguments.length > 1)?arguments[1]:g_thisImg;
	switch (new String(p_attribute).toLowerCase()) {
		case "image":			return g_aryImgs[i]; 
		case "file":			return g_aryImgs[i].src; 
		case "description": return g_aryImgs[i].descr; 
		case "width":			return g_aryImgs[i].width; 
		case "height":		return g_aryImgs[i].height;
		case "title":			return g_aryImgs[i].title;
		case "extra1":		return g_aryImgs[i].extra1;
		case "extra2":		return g_aryImgs[i].extra2;
		case "extra3":		return g_aryImgs[i].extra3;
		case "extra4":		return g_aryImgs[i].extra4;
		case "date":			return g_aryImgs[i].date;

		case "dir":				return g_ImgDir; 
		case "thumbdir":	return g_ThmDir; 
		case "pathname":	return g_ImgDir+g_aryImgs[i].src; 

		case "count":			return g_aryImgs.length;
		case "current":		return g_thisImg; 
		case "maxwidth":
			var m=-1;
			for (var i=0;i < g_aryImgs.length; i++)
				m=Math.max(m, g_aryImgs[i].width);
			return m;
			break;
		case "maxheight":
			var m=-1;
			for (var i=0;i < g_aryImgs.length; i++)
				m=Math.max(m, g_aryImgs[i].height);
			return m;
			break;
		default:
			alert( "Photo Gallery function \"getValue\" did not recognize attribute \""+p_attribute+"\"" );
			break;
	} // switch
}//getValue

