// ======================================================================================
// An Editor / Wizard for maintaining the Pizza Photo Gallery 
// written by Courts Carter, pizzabytheslice.com, (c) 2005
// version: 
	var C_VERSIONINFO = "0.06beta";

//
// Use, distribute, modify (improve. please?) according to Creative Commons agreement:
// http://creativecommons.org/licenses/by-nc/2.0/
// 
// ======================================================================================

// first, let's extend the pbtsImg Class
// g_aryImgs[j].copy(aryLocks[lockedIdx]);
pbtsImg.prototype.copy = function(p_source){
	this.src		= p_source.src;
	this.descr	= p_source.descr;
	this.width	= p_source.width;
	this.height	= p_source.height;
	this.title	= p_source.title;
	this.extra1	= p_source.extra1;
	this.extra2	= p_source.extra2;
	this.extra3	= p_source.extra3;
	this.extra4	= p_source.extra4;
	this.date		= p_source.date;
	this.isLoaded	= p_source.isLoaded;
	this.img			= new Image();
	this.img.src	= p_source.img.src;
}

// aryMovers[aryMovers.length]	= g_aryImgs[i].getCopy();
pbtsImg.prototype.getCopy = function(){
	i=new pbtsImg;
	i.copy(this);
	return i;
}

// ==================================================================
// Function: Function
// Descr:		Description
// ==================================================================
function resetArys(){
 g_aryImgs		= new Array(0);
 }

// ==================================================================
// function: getThumbHTML
// Does:		 do i want to leave the image kludge in? it's expensive
// ==================================================================
function getThumbHTML(p_idx){
	var title="";
	var URL="";

	if (g_aryImgs[p_idx].src == ""){
		URL= C_SPACER_IMG;
	} else {
		if (g_ThmDir !="")
			URL= document.demo.prefBaseURL.value+ g_ThmDir+ g_aryImgs[p_idx].src;
		else if (g_ImgDir !="")
			URL= document.demo.prefBaseURL.value+ g_ImgDir+ g_aryImgs[p_idx].src;
		else
			 URL= C_SPACER_IMG;
	}

	var rtn= "<a href='javascript:"+gv_onThmbClickFn+"("+p_idx+");'><img src=\""+URL+"\" border=0 ";

	if (g_aryImgs[p_idx].width > g_aryImgs[p_idx].height)
		rtn+= "width="+g_ThmWidth;
	else
		rtn+= "height="+g_ThmWidth;

	rtn+= "></a>";

	if ((g_isThmTitles) && (g_aryImgs[p_idx].title.length > 0) && (!document.demo.hideTitles.checked))
		rtn+= "<br>"+g_aryImgs[p_idx].title;

	if (document.demo.hideFilenames.checked )
		return rtn;
	else
		return rtn+"<br>"+g_aryImgs[p_idx].src;

}// getThumbHTML

// ==================================================================
// FUNCTION: writeThmbEdit
// USE:			thumbnail page
// DOES:		 
// NOTES:		chooses min of width or height
// RETURNS:	none
// PARAMS:	 none or three, your call
// ==================================================================
function writeThmbEdit() {
	var innerHTML= "";
	var spcHt		= 0;
	var spcWd		= 0;
	var EID			= g_EID_Thms;			 // the thumbnail table element ID
	var idRoot	 = C_THUMBID_Normal; // root for each ell's element ID
	var ThmCols	= 3;

	if (arguments.length > 0){
		// kludge Alert
		EID		= arguments[0];
		idRoot = C_THUMBID_Sort;
		ThmCols= 8;
		gv_onThmbClickFn = "sortAddPic";
	} else {
		gv_onThmbClickFn = "editPic";
	}
	

	// set spacer dimenstions
	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;
	}

	// loop over all images
	for (var i=0; i < g_aryImgs.length; i++ ) {
		if ((i % ThmCols) == 0) 
			innerHTML+=	"<tr>";

		if (i < g_aryImgs.length) {
			innerHTML+=	"<td id=\"" +idRoot+i+"\" valign=top \""+g_ThmClass+"\">" +getThumbHTML(i)+"</td>";
		} else {
			innerHTML+=	"<td valign=top "+g_ThmEmptyClass+" ><img src=\""+g_Spacer+"\" width="+spcWd+" height="+spcHt+"></td>";
		} // if

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

	} // for

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

} // writeThmbEdit

// ==================================================================
// Function: makeSearchable
// Descr:		Description
// ==================================================================
function strNoUndefined(p_val){
	if (p_val != undefined)
		return p_val;
	else
		return "";
}

// ==================================================================
// Function: parseName
// Descr:		
// ==================================================================
function parseName(p_in){
	var myString = new String(p_in);
	var rExp = /[-_]/gi;
	myString= myString.replace(rExp, " ").toString()
	rExp = /.(jpg|gif)/gi;
	return myString.replace(rExp, "").toString();
}

// ==================================================================
// Function: makeSearchable
// Descr:		Description
// ==================================================================
function makeSearchable(){
	var myDate	 = new Date();
	var title		= "";
	var aTitle	 = "";
	var alt			= "";
	var rtn			= "<!-- generated by PBTS Gallery Editor ("+C_VERSIONINFO+") on "+ myDate.toGMTString()+" -->\n<h1>Photo Gallery</h1>\n<p>pictures,images,graphics</p>\n";

	for (var i=0; i < g_aryImgs.length; i++){
		if (g_aryImgs[i].title != ""){
			title ="\n<h2>"+g_aryImgs[i].title +"</h2>";
			aTitle= " title=\""+ g_aryImgs[i].title +"\"";
			alt	 = " alt=\""	+ g_aryImgs[i].title +"\"";
		} else {
			title = "";
			aTitle= "";
			alt	 = "";
		}

		rtn+=	title+ "\n<p>"+
			"<a href=\""+g_ImgDir+g_aryImgs[i].src+"\""+ aTitle +">"+
			"<img src=\""+g_ImgDir+g_aryImgs[i].src+"\" height=\""+g_aryImgs[i].height+"\" width=\""+g_aryImgs[i].width+"\""+ alt +">"+
			"\n "+ g_aryImgs[i].descr +"</a>"+
			trimStr(strNoUndefined(g_EID_Extra1[i])+ " ")+
			trimStr(strNoUndefined(g_EID_Extra2[i])+ " ")+
			trimStr(strNoUndefined(g_EID_Extra3[i])+ " ")+
			trimStr(strNoUndefined(g_EID_Extra4[i])+ " ")+
			strNoUndefined(g_EID_Date[i])+ 
			" "+g_aryImgs[i].src+
			" "+parseName(g_aryImgs[i].src)+
			"</p>\n";
		} // for

	document.demo.searchable.value= rtn+ "\n<!-- End of PBTS Gallery Editor code -->\n";

	} // searchable


// ==================================================================
// Function: Function
// Descr:		Description
// ==================================================================
function makeScript(){
	var myDate = new Date();
	var rtn="// --- Copy and Paste this code into your Gallery Page ---\n// generated by PBTS Gallery Editor ("+C_VERSIONINFO+") on "+ myDate.toGMTString()+"\n";
	
	rtn+= "\nsetDefDims( "+g_defaults.width+", "+g_defaults.height+" );";
	rtn+= "\nsetThumbWidth( "+document.demo.ThumbWidth.value+" );";
	rtn+= "\nsetThumbDir( \""+document.demo.ThumbDir.value+"\" );";
	rtn+= "\nsetImgDir( \""+document.demo.ImgDir.value+"\" );";
	//rtn+= "\nsetSpacer( \""+C_SPACER_IMG+"\" );";
	
	rtn+= "\n";

	rtn+= "\nreorderArgs( \"file\",\"title\",\"description\",\"width\",\"height\",\"date\",\"extra1\",\"extra2\",\"extra3\",\"extra4\" );";
	for (var i=0; i < g_aryImgs.length; i++){
		if ((g_aryImgs[i].width==g_defaults.width) && (g_aryImgs[i].height==g_defaults.height))
			rtn+= "\naddImg( \""+ g_aryImgs[i].src +"\",\""+ g_aryImgs[i].title +"\",\""+ g_aryImgs[i].descr +"\" );";
		else if ((g_aryImgs[i].width==g_defaults.height) && (g_aryImgs[i].height==g_defaults.width))
			rtn+= "\naddImg90( \""+ g_aryImgs[i].src +"\",\""+ g_aryImgs[i].title +"\",\""+ g_aryImgs[i].descr +"\" );";
		else
			rtn+= "\naddImg( \""+ g_aryImgs[i].src +"\",\""+ g_aryImgs[i].title +"\",\""+ g_aryImgs[i].descr +"\", "+g_aryImgs[i].width+", "+g_aryImgs[i].height+");";
		} // for

	document.demo.newout.value=rtn+ "\n\n// --- End of PBTS Gallery Editor code ---\n";
	
} // makeScript


// ==================================================================
// Function: findFn
// Descr:		returns the index to fn (within gv_aryScript), -1 if not found
// ==================================================================
function findFn( p_fn ){
	var i;

	for (i=0; i < gv_aryScript.length; i++){
		if (getIsFn(gv_aryScript[i],p_fn))
			break;
		}// for	

	if (i < gv_aryScript.length)
		return i;
	else
		return -1;

} // findFn

// ==================================================================
// Function: getSetup
// Descr:		this is only run once -- per loading of javascript...
// ==================================================================
function getSetup(){
	var i;

	//	 setImgDir
	i= findFn("setImgDir");
	if (i >= 0){
		if (getStringArgs(gv_aryScript[i]) > 0)
			document.demo.ImgDir.value= gv_aryStrArgs[0];
	} // setImgDir

	//	 setThumbDir
	i= findFn("setThumbDir");
	if (i >= 0){
		if (getStringArgs(gv_aryScript[i]) > 0)
			document.demo.ThumbDir.value= gv_aryStrArgs[0];
	} // setThumbDir

	//	 setSpacer
	//i= findFn("setSpacer");
	//if (i >= 0){
	//	if (getStringArgs(gv_aryScript[i]) > 0)
	//		document.demo.Spacer.value= gv_aryStrArgs[0];
	//} // setSpacer

	//	 setThumbWidth
	i= findFn("setThumbWidth");
	if (i >= 0){
		if (getNumericArgs(gv_aryScript[i]) > 0)
			document.demo.ThumbWidth.value= gv_aryNumArgs[0];
	} // setThumbWidth

	//	 setDefDims 
	i= findFn("setDefDims");
	if (i >= 0){
		if( getNumericArgs(gv_aryScript[i]) > 1){
			document.demo.defWidth.value	= gv_aryNumArgs[0];
			document.demo.defHeight.value = gv_aryNumArgs[1];
			}
	} // setDefDims

} // getSetup
	

// ==================================================================
// Function: FunctionName
// Descr:		Description
// ==================================================================
function resetDefs(){
	g_defaults.descr		= "";
	g_defaults.title		= "";
	g_defaults.date			= "";
	g_defaults.extra1		= "";
	g_defaults.extra2		= "";
	g_defaults.extra3		= "";
	g_defaults.extra4		= "";
	g_defaults.width		= 1;
	g_defaults.height		= 1;
} // resetDefs

// ==================================================================
// Function: FunctionName
// Descr:		Description
// ==================================================================
function parseMyJS(p_id){
	var errs		 = 0;
	
	var myString = new String(document.demo.input.value);
	gv_aryScript	= myString.split(/\n/);

	resetDefs();
	resetImgForm(); // is the need for this just because of my dev environment? 
	resetSetupForm();
	getSetup();
	resetArys();

	document.getElementById(g_EID_Img).src = C_SPACER_IMG;

	execMyJSImgs();
	// need to preload the edit fields, kindofa bug prevention
	if (g_aryImgs.length > 0)
		copyPicAryToFields(0);

	alert( "Done reading your javascript. "+g_aryImgs.length+" images added. Move on to the next step.");

}// parseMyJS


// ==================================================================
// Function: readACDSee
// Descr:		I hate RegExp inconsistencies -- no, i hate how poorly i get 'em
//					 for whatever reason the \w and \d switches weren't cutting it... so am using the 0-9 etc
// ==================================================================
function readACDSee(){
	var i=0;
	var cnt=0;
	var myRE			= new RegExp( "^[\t ]*([a-z0-9_][a-z0-9_ -]*).(jpg|gif)[a-z0-9,_ /:]{30,80} ([0-9]+)x([0-9]+)x", "gi" );

	var myString	= new String(document.demo.input.value);
	var tempStr	 = ""; // 
	var myResults = null; // holds results of RegExp mattch

	myString	= myString.split(/\n/);

	// see parseMyJS for why i'm doing each of these:
	resetDefs();
	// resetSetupForm; don't think I should clear this...
	resetImgForm();
	getSetup();
	resetArys();

	document.getElementById(g_EID_Img).src = C_SPACER_IMG;

	// ok, de it!
	reorderArgs("file","width","height");
	for (i=0;i < myString.length; i++){
		tempStr= new String( myString[i] );
		myResults = tempStr.match(myRE);
		if (myResults != null){
			cnt++;
			addImg( RegExp.$1 +"."+RegExp.$2, RegExp.$3, RegExp.$4 );
			} // if
		} // for

	if (g_aryImgs.length > 0)
		copyPicAryToFields(0);

	alert( cnt+" images read from the ACDSee file listing" );

} // readACDSee


// ==================================================================
// Function: readDir
// Descr:		this is for reading a DOS dir listing
// ==================================================================
function readDir(){
	var i=0;
	var cnt=0;
	var myRE			= new RegExp( "^[\t ]*([A-Za-z0-9_][A-Za-z0-9_ -]*).(jpg|gif)", "gi" );

	var myString	= new String(document.demo.input.value);
	var tempStr	 = ""; // 
	var myResults = null; // holds results of RegExp mattch

	if (!confirm("You are about to load a list of images without dimension information. It is recommended that you set the default width and height (on the Setup tab) before doing this. Click OK to proceed with upload or Cancel to halt"))
		return;

	myString	= myString.split(/\n/);

	// see parseMyJS for why i'm doing each of these:
	// resetDefs();
	// resetSetupForm; don't think I should clear this...
	setDefDims( document.demo.defWidth.value, document.demo.defHeight.value );
	resetImgForm();
	getSetup();
	resetArys();

	document.getElementById(g_EID_Img).src = C_SPACER_IMG;

	// ok, de it!
	reorderArgs("file");
	for (i=0;i < myString.length; i++){
		tempStr= new String( myString[i] );
		myResults = tempStr.match(myRE);
		if (myResults != null){
			cnt++;
			addImg( RegExp.$1 +"."+RegExp.$2 );
			} // if
		} // for

	if (g_aryImgs.length > 0)
		copyPicAryToFields(0);

	alert( cnt+" images read from the file listing. Note: all images are using the default width and height." );

} // readACDSee


// ==================================================================
// Function: Function
// Descr:		Description
// ==================================================================
function copyPicAryToFields(p_idx){
	if ((p_idx < 0) || (p_idx >= g_aryImgs.length))
		return -1;

	document.demo.imgNbr.value	 = p_idx+1;
	document.demo.imgSrc.value	 = g_aryImgs[p_idx].src;
	document.demo.imgDescr.value = getUnsafeQuote(g_aryImgs[p_idx].descr);
	document.demo.imgWidth.value = g_aryImgs[p_idx].width;
	document.demo.imgHeight.value= g_aryImgs[p_idx].height;
	document.demo.imgTitle.value = getUnsafeQuote(g_aryImgs[p_idx].title);
	document.demo.imgExtra1.value= getUnsafeQuote(g_aryImgs[p_idx].extra1);
	document.demo.imgExtra2.value= getUnsafeQuote(g_aryImgs[p_idx].extra2);
	document.demo.imgExtra3.value= getUnsafeQuote(g_aryImgs[p_idx].extra3);
	document.demo.imgExtra4.value= getUnsafeQuote(g_aryImgs[p_idx].extra4);
	document.demo.imgDate.value	= getUnsafeQuote(g_aryImgs[p_idx].date);

	if ((document.demo.imgWidth.value == g_defaults.width) && (document.demo.imgHeight.value = g_defaults.height))
		document.demo.dimensions[0].checked = true;
	else if ((document.demo.imgWidth.value == g_defaults.height) && (document.demo.imgHeight.value = g_defaults.width))
		document.demo.dimensions[1].checked = true;
	else
		document.demo.dimensions[2].checked = true;

} // copyPicAryToFields


// ==================================================================
// Function: Function
// Descr:		Description
// ==================================================================
function resetSetupForm(){
	document.demo.defWidth.value	 = "";
	document.demo.defHeight.value	= "";
	document.demo.ThumbWidth.value = "";
	document.demo.ThumbDir.value	 = "";
	document.demo.ImgDir.value		 = "";
	//document.demo.Spacer.value		 = "";

}

// ==================================================================
// Function: Function
// Descr:		Description
// ==================================================================
function resetImgForm(){
	document.demo.imgNbr.value	 = "";
	document.demo.imgSrc.value	 = "";
	document.demo.imgDescr.value = "";
	document.demo.imgWidth.value = "";
	document.demo.imgHeight.value= "";
	document.demo.imgTitle.value = "";
	document.demo.imgExtra1.value= "";
	document.demo.imgExtra2.value= "";
	document.demo.imgExtra3.value= "";
	document.demo.imgExtra4.value= "";
	document.demo.imgDate.value	= "";

}


// ==================================================================
// Function: insertPic
// Descr:		Description
// ==================================================================
function insertPic(){
	var temp= new pbtsImg;
	temp.copy(g_defaults);
	g_aryImgs.splice( gv_currentIdx, 0, temp );
	copyPicAryToFields(gv_currentIdx);
	writeThmbEdit();
	editPic(gv_currentIdx);

} // insertPic



// ==================================================================
// Function: deletePic
// Descr:		Description
// ==================================================================
function deletePic(){
	if (!confirm("Are you sure you want to delete image "+gv_currentIdx+", \""+g_aryImgs[gv_currentIdx].src+"\"?"))
		return false;

	g_aryImgs.splice( gv_currentIdx, 1 );
	writeThmbEdit();

	if ((gv_currentIdx >= g_aryImgs.length) && (g_aryImgs.length > 0)){
		//alert( "debug: deleting last image" );
		copyPicAryToFields(g_aryImgs.length-1);
		editPic(g_aryImgs.length-1);
	} else if (gv_currentIdx < g_aryImgs.length){
		//alert( "debug: deleting justa middle image" );
		copyPicAryToFields(gv_currentIdx);
		editPic(gv_currentIdx);
	}else if ((gv_currentIdx > 0) && (g_aryImgs.length > 0)){
		//alert( "debug: huh?" );
		copyPicAryToFields(gv_currentIdx-1);
		editPic(gv_currentIdx-1);
		}

} // deletePic


// ==================================================================
// Function: FunctionName
// Descr:		Description
// ==================================================================
function editPic(p_idx){

	if ((p_idx < 0) || (p_idx >= g_aryImgs.length))
		return -1;

	// save current edits
	// --------------------------------
	if (gv_currentIdx >= 0) {
		g_aryImgs[gv_currentIdx].src		= document.demo.imgSrc.value;
		g_aryImgs[gv_currentIdx].descr	= getSafeQuote( document.demo.imgDescr.value);
		g_aryImgs[gv_currentIdx].title	= getSafeQuote( document.demo.imgTitle.value);
		g_aryImgs[gv_currentIdx].extra1	= getSafeQuote( document.demo.imgExtra1.value);
		g_aryImgs[gv_currentIdx].extra2	= getSafeQuote( document.demo.imgExtra2.value);
		g_aryImgs[gv_currentIdx].extra3	= getSafeQuote( document.demo.imgExtra3.value);
		g_aryImgs[gv_currentIdx].extra4	= getSafeQuote( document.demo.imgExtra4.value);
		g_aryImgs[gv_currentIdx].date		= getSafeQuote( document.demo.imgDate.value);

		if (document.demo.dimensions[0].checked){
			g_aryImgs[gv_currentIdx].width	= g_defaults.width;
			g_aryImgs[gv_currentIdx].height	= g_defaults.height;
		} else if (document.demo.dimensions[1].checked){
			g_aryImgs[gv_currentIdx].width	= g_defaults.height;
			g_aryImgs[gv_currentIdx].height	= g_defaults.width;
		} else {
			g_aryImgs[gv_currentIdx].width	= document.demo.imgWidth.value;
			g_aryImgs[gv_currentIdx].height	= document.demo.imgHeight.value;
		}

		document.getElementById(C_THUMBID_Normal+gv_currentIdx).innerHTML= getThumbHTML(gv_currentIdx);

		}

	// load edit fields
	// --------------------------------
	gv_currentIdx=p_idx;
	copyPicAryToFields( gv_currentIdx );

	// draw image
	// --------------------------------
	var pic= document.getElementById(g_EID_Img);
	if (document.demo.prefEdImgSize[0].checked){
		pic.src		= document.demo.prefBaseURL.value+ g_ImgDir + g_aryImgs[p_idx].src;
		pic.width	= g_aryImgs[p_idx].width;
		pic.height = g_aryImgs[p_idx].height;
	} else {
		pic.src		= document.demo.prefBaseURL.value+ g_ThmDir + g_aryImgs[p_idx].src;
		if (g_aryImgs[p_idx].width > g_aryImgs[p_idx].height){
			pic.width= g_ThmWidth;
			pic.height= g_ThmWidth* g_aryImgs[p_idx].height/g_aryImgs[p_idx].width;
		}else{
			pic.height= g_ThmWidth;
			pic.width= g_ThmWidth* g_aryImgs[p_idx].width/g_aryImgs[p_idx].height;
		}
	}
	window.scrollTo(0,110); //pic.top);
		
	// now change the thumbnail, display the selected thumbnail image
	// --------------------------------
	for (var i=0; i < g_aryImgs.length; i++){
		document.getElementById(C_THUMBID_Normal+i).className=C_THUMB_Normal_ClassName;
		} // for
	document.getElementById(C_THUMBID_Normal+p_idx).className=C_THUMB_Selected_ClassName;
	
} // editPic


// ==================================================================
// Function: Function
// Descr:		Description
// ==================================================================
function acceptEdits(){ editPic(gv_currentIdx); }
function cancelEdits(){ copyPicAryToFields(gv_currentIdx) }
function nextImg(){ if ((gv_currentIdx+1) < g_aryImgs.length) editPic(gv_currentIdx+1); else editPic(0); }
function prevImg(){ if ((gv_currentIdx-1) >= 0) editPic(gv_currentIdx-1); else editPic(g_aryImgs.length-1); }


// ==================================================================
// Function: FunctionName
// Descr:		Description
// Returns:	
// ==================================================================
function execMyJSImgs(){
	var errcnt = 0;
	// read in the image list
	// --------------------------------

	for (var i=0; i < gv_aryScript.length; i++){
		try{ eval( gv_aryScript[i] );}
		catch(errid){
			errcnt++; 
			}
		}

	if (errcnt > 0)
		alert( errcnt+" error(s) encountered during conversion:\n\n"+out );

} // execMyJSImgs


// ==================================================================
// Function: FunctionName
// Descr:		Description
// Returns:	the number of statements processed
// ==================================================================
function loadGallery(){
	var out		= "";

	// init the PBTS Photo Gallery 
	// --------------------------------
	setDefDims( document.demo.defWidth.value, document.demo.defHeight.value );

	setThumbWidth( document.demo.ThumbWidth.value );
	setThumbDir( document.demo.ThumbDir.value );
	setImgDir( document.demo.ImgDir.value );
	//setSpacer( document.demo.Spacer.value );

	// draw the thumbnail grid
	// --------------------------------
	pbtsGetFirstIdx();
	writeThmbEdit();
	if (g_aryImgs.length > 0)
		editPic(0);

} // drawThmbs


// ==================================================================
// Function: Function
// Descr:		Description
// ==================================================================
function fileHelp( p_topic ){
	changeTab(7);
	window.location.href="#"+p_topic;
}


// ==================================================================
// Function: Function
// Descr:		Description
// ==================================================================
function changeTab(p_idx){
	for (var i=0; i < 8; i++){
		if (i == p_idx){
			document.getElementById("tab"+p_idx).className="current";
			document.getElementById("page"+p_idx).style.visibility="visible";
		}else{
			document.getElementById("tab"+i).className="";
			document.getElementById("page"+i).style.visibility="hidden";
		}
		}//for

	switch(p_idx){
		case 3: loadGallery(); // editor
						break;
		case 4: sortInit(); //	sort
						break;
		case 5: makeScript(''); // your javasript
						break;
		case 6: makeSearchable();
						break;
		} //switch
}

// ----------------------------------------------------------------------------------------------------------------------
//
//			S O R T		F U N C T I O N S
//
// ----------------------------------------------------------------------------------------------------------------------


// ==================================================================
// FUNCTION: writeThmbEdit
// USE:			thumbnail page
// DOES:		 
// NOTES:		chooses min of width or height
// RETURNS:	none
// PARAMS:	 none or three, your call
// ==================================================================
function sortWriteThumbs() {
	var innerHTML= "";
	var spcHt		= 0;
	var spcWd		= 0;
	var EID			= g_EID_Thms;			 // the thumbnail table element ID
	var idRoot	 = C_THUMBID_Normal; // root for each ell's element ID
	var ThmCols	= 3;

	if (arguments.length > 0){
		// kludge Alert
		EID		= arguments[0];
		idRoot = C_THUMBID_Sort;
		ThmCols= 6;
		gv_onThmbClickFn = "sortAddPic";
	} else {
		gv_onThmbClickFn = "editPic";
	}
	

	// set spacer dimenstions
	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;
	}

	// loop over all images
	for (var i=0; i < g_aryImgs.length; i++ ) {
		if ((i % ThmCols) == 0) 
			innerHTML+=	"<tr><td class='sortTarg'><a class='sortTarg' href=\"#\" onClick='sortSortNow("+i+",\"b\")' title='Place here'>X</a></td>";

		if (i < g_aryImgs.length) {
			innerHTML+=	"<td id=\"" +idRoot+i+"\" valign=top \""+g_ThmClass+"\">" +getThumbHTML(i)+"</td>"+
				"<td class='sortTarg'><a class='sortTarg' href=\"#\" onClick='sortSortNow("+i+",\"a\")' title='Place here'>X</a></td>";
		} else {
			innerHTML+=	"<td valign=top "+g_ThmEmptyClass+" ><img src=\""+g_Spacer+"\" width="+spcWd+" height="+spcHt+"></td>";
		} // if

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

	} // for

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

} // sortWriteThumbs

// ==================================================================
// Function: Function
// Descr:		Description
// ==================================================================
// CRC, start here!!!
function sortSortNow(p_idx, p_dir){ 
	moveToEnd= false;
	// handle "after" placements
	// --------------------------------
	if (p_dir == "a"){
		if (p_idx != (g_aryImgs.length -1))
			p_idx++;
		else
			moveToEnd=true;
		}

	if (gv_arySortSel.length < 1){
		alert( "No images selected." );
		return false;
	} else if (sortGetIsSelected(p_idx) >= 0){
		//	 Overlap
		// --------------------------------
		alert( "Please choose a different Target -- this image is one you've selected to be moved." );
		return false;
	}

	var j=0;
	var i=0;
	var found		= false;
	var lockedIdx= 0;
	var movedIdx = 0;
	var howMany	= gv_arySortSel.length;
	var targetIdx= 0;
	// going to break the master arrays into two sets: one for the selected images to be moved, the other for the static, untouched images
	// that may need to be shifted -- these are the lock images
	var aryMovers		 = new Array(0);
	var aryLocks		 = new Array(0);

	//document.demo.debugsort.value="";
	
	//	B2. sort selections
	// --------------------------------
	gv_arySortSel= gv_arySortSel.sort(arySortNumbers);	 
	
	// B3. store in lock or move array
	// --------------------------------
	for (i=0; i < g_aryImgs.length; i++){
		found= (sortGetIsSelected(i) >= 0);
	
		if (found){
			aryMovers[aryMovers.length]	= g_aryImgs[i].getCopy();
		} else {
			if (i == p_idx)
				targetIdx= aryLocks.length;
	
			aryLocks[aryLocks.length]	= g_aryImgs[i].getCopy();

		} // if
		}//for i
	
	// C3. reload master
	// --------------------------------

	// going to write code to handle the "move to end" case. lame, huh? only because 
	// i continously loose one or two, record keeping is so difficult
	// -------------------------
	if (moveToEnd){
		j=0; 
		for (lockedIdx=0; lockedIdx < aryLocks.length; lockedIdx++){
			g_aryImgs[j].copy(aryLocks[lockedIdx]);
			j++;
			} // for: lockedIdx
		for (movedIdx=0; movedIdx < aryMovers.length; movedIdx++){
			g_aryImgs[j].copy(aryMovers[movedIdx]);
			j++;
			} // for: movedIdx
	} else {
		j=0;
		do {
			if (j == targetIdx){
				for (movedIdx=0; movedIdx < aryMovers.length; movedIdx++){
					g_aryImgs[j].copy(aryMovers[movedIdx]);
					j++;
				} // for
			} else {
				g_aryImgs[j].copy(aryLocks[lockedIdx]);
				lockedIdx++;
				j++;
			} // if
			
			}// do
		while (j < g_aryImgs.length);
	}	// if: move to end?
	
	// --------------------------------
	sortClear();
	sortWriteThumbs("sorttbl");
	
	document.demo.sortStatus.value=" "+howMany+" images were moved";
	document.getElementById("sortStatus").className= "ro";
	
} //sortSortNow

// ==================================================================
// Function: Function
// Descr:		Description
// ==================================================================
function sortInit(){
	gv_arySortSel = new Array(0);
	sortWriteThumbs("sorttbl");
	document.demo.sortStatus.value="";
	document.getElementById("sortStatus").className= "ro";
} //

// ==================================================================
// Function: Function
// Descr:		Description
// ==================================================================
function sortClear(){
	gv_arySortSel = new Array(0);
	for (var i=0; i < g_aryImgs.length; i++){
	 try {
		document.getElementById(C_THUMBID_Sort+i).className=C_THUMB_Normal_ClassName;
			}
		catch (e){ alert(" errored on "+i ); }
		} // for
	sortWriteThumbs("sorttbl");
	document.demo.sortStatus.value=" No images selected";
	document.getElementById("sortStatus").className= "ro";

} // sortClearSels



// ==================================================================
// Function: Function
// Descr:		Description
// ==================================================================
function arySortNumbers(a, b) { return a - b} 

// ==================================================================
// Function: sortGetIsSelected
// Descr:		returns the index within gv_arySortSel of the p_idx, an index to arySrc
// Params:	 p_idx, the index of a valid element in arySrc
// Returns:	-1 if not found, otherwise, the index within gv_arySortSel
// ==================================================================
function sortGetIsSelected(p_idx){
	var found= -1;
	var i=0;

	for (i=0; i < gv_arySortSel.length; i++){
		if (gv_arySortSel[i] == p_idx) {
			found=i;
			break;
			}
		}//for i

	return found;

} // sortGetIsSelected

// ==================================================================
// Function: Function
// Descr:		Description
// ==================================================================
function sortAddPic(p_idx){
	var i= sortGetIsSelected(p_idx);
	if (i >= 0){
		// toggle: remove
		gv_arySortSel.splice( i, 1);
		document.getElementById(C_THUMBID_Sort+p_idx).className=C_THUMB_Normal_ClassName;
	} else {
		// add
		gv_arySortSel[gv_arySortSel.length]= p_idx;
		document.getElementById( C_THUMBID_Sort+p_idx).className= C_THUMB_Selected_ClassName;
	}

	document.demo.sortStatus.value=" "+gv_arySortSel.length+" selected images";
	if (gv_arySortSel.length > 0)
		document.getElementById("sortStatus").className= "loaded";
	else
		document.getElementById("sortStatus").className= "ro";

} // sortAddPic



// ----------------------------------------------------------------------------------------------------------------------

// -------------------------------------------
//						 M A I N
// -------------------------------------------
	var C_THUMB_Normal_ClassName	 = "thumb";
	var C_THUMB_Selected_ClassName = "selthumb";

	var C_THUMBID_Normal					 = "thm";
	var C_THUMBID_Sort						 = "sort";

	var C_SPACER_IMG							 = "images/shim.gif";

	var gv_currentIdx		= -1;
	var gv_onThmbClickFn = "";
	var gv_arySortSel		= new Array(0); // keeps a list of selected images for sorting

	// i only use this to parse the Javascript and load the edit fields
	var gv_aryScript		 = new Array(0);

	// going to utilize as much of the gallery, code and variables, as possible
	// let's initialize stuff
	// --------------------------------

	setThumbGridId( "thmbtbl" );
	setThumbClass( "thumb" );	
	setThumbSpacing( 5 );
	setThumbTitles(true);

	setImageId( "theimage" );

