//////////////////////////// SLIDE-SHOW SOFTWARE BY DAVID REGIER //////////////////////////
//////////////////////// http://www.creativetouchphoto.com/david //////////////////////////
////////////////////// UNAUTHORIZED USE OF THIS CODE IS PROHIBITED ////////////////////////

// All outside funtion calls or refernces to outside variables must be defined thru accessors on "thumbnails.html"...


ProgressBar[ "hideOnSuccess" ] = 1; //set to 0 for testing
var IMG_BORD_WID_TN = "1";
var nImagesTN = -1;
var tnRemainingTN = 0;
var dataFile = null;

// Search "prvWedHmPg" to mark customization for ctp private wedding homepages...
var prvWedHmPg = false;

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

var theStyles = "" +

	"<STYLE>" +

		".vSpacer10 { line-height:10px; } " +

		".borderImage{ border:1px solid " + staticColorTN + "; } " +

	"</STYLE>";

document.write( theStyles );

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


function notifyUserTN() { // onUnload

	if( IV.lgPhotoShowing ) alert( "PROBLEM: Clicking your Browser's Back button will send you away from the thumbnails page altogether and back to the last page you were on. This is probably not your intention?\n\nSOLUTION: Next time simply click the \"Back\" link below the full-size image (or the image itself)." );

}
//--------------------------------------------------------------------------------------

function getDataErrorsTN() {

	var dataError = false;

	if( local ) {

		var N_FIELDS = 6;

		// Check for possible errors
		for( i = 0; i < nImagesTN; i++ ) {

			var nFields = arrImageInfo[ i ].length;

			if( N_FIELDS != nFields ) {

				dataError = true;

				alert( "Record #" + ( i + 1 ) + " in \"" + dataFile + "\" has " + nFields + " fields. It should have exactly " + N_FIELDS + " fields. \n\nBAD RECORD: " + arrImageInfo[ i ] );

				break;
			}
		}
	}
	return dataError;
}
//--------------------------------------------------------------------------------------

function showNumImagesTN() {
	if( nImagesTN > -1 ) {
		if( ProgressBar.hideOnSuccess )
			document.getElementById( "consoleTN" ).style.visibility = "hidden";
		setTimeout( 'self.status = nImagesTN + " Thumbnails loaded..."', 500 );
	}
}
//--------------------------------------------------------------------------------------

function writeConsoleTN() {
	document.write( "<DIV ID='consoleTN' STYLE='line-height:14px;' ALIGN='center'><DIV>&nbsp; </DIV><BR></DIV>" );
}
//--------------------------------------------------------------------------------------

function tnStatusTN() {

	var Console = document.getElementById( "consoleTN" );

	if( document.body && eval( "typeof Console" ) == "object" ) {
		if( tnRemainingTN >= 0 ) {
			var dBarWid = ( nImagesTN - tnRemainingTN ) * ( ProgressBar.width / nImagesTN );
			var percentLoaded = parseInt( ( ( nImagesTN - tnRemainingTN ) / nImagesTN ) * 100 ) + "%";

			var pBar = "" +

			"<DIV STYLE='width:" + ProgressBar.width + "px; background-color:" + ProgressBar.bgColor + "; text-align:left; z-index:0;'>" +
				"<DIV STYLE='width:" + dBarWid + "px; background-color:" + ProgressBar.progressColor + ";'>" +
					"&nbsp;" +
				"</DIV>" +
			"</DIV>" +

			"<DIV STYLE='position:relative; top:-14px; left:0px; width:" + ProgressBar.width + "px; text-align:center; z-index:1; font-family:arial,helvetica; font-size:8pt; color:" + ProgressBar.fontColor + "; font-weight:bold'>" +
				"Loading Thumbnails " + percentLoaded +
			"</DIV>";
			Console.innerHTML = pBar;
			if( tnRemainingTN > 0 ) setTimeout( "tnStatusTN()", 250 );
			else if( ProgressBar.hideOnSuccess ) Console.style.visibility = "hidden";
		}
	}
	else setTimeout( "tnStatusTN()", 250 );
}
//--------------------------------------------------------------------------------------

function writeDBfileTN() {


	self.status = "Opening connection to Data-Base - please wait...";

	var qStr = unescape( document.location.search ).substring( 1 );

	dataFile = qStr.getVal( dataKeyTN );

	if( dataFile ) {
		document.write("<SCR" + "IPT LANGUAGE='JavaScript1.2' SRC='" + dataFile + "'><\/SCR" + "IPT>");
		prvWedHmPg = ( dataFile.indexOf( "client/" ) >= 0 && dataFile.indexOf( "client.js" ) >= 0 ) ? true : false;
	}

	else
		document.location.replace( "index.html" );

}

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

function writePrvWedHmPgDataConverter() {

	self.status = "Reformating private wedding homepage data, please wait...";

	document.write("<SCR" + "IPT LANGUAGE='JavaScript1.2' SRC='prvWedHmPgDataConverter.js'><\/SCR" + "IPT>");

}
//--------------------------------------------------------------------------------------

function writeHtmlTN() {

	self.status = "Reading information in data-base...";

	if( eval( "typeof arrImageInfo" ) != "object" ) {
		alert( "WEBMASTER: A fatal error has been detected. The image data file is malformed or non-existant. Specifically, \"arrImageInfo\" is not an array object data type. Here are some possible causes...\n\n1) The system is looking for the data file \"" + dataFile + "\". Does it exist? If so continue to #2...\n2) Each image record must begin with 1 open bracket '[' and end with 1 closing bracket ']'\n3) Double quote characters may be missing or in the wrong place. There should be exactly 6 double quote characters per record." );
		return;
	}

	// Evaluate & set ss global stmts from all members in globalInfo array
	for( i = 0; i < arrGlobalInfo.length - 1; i++ ) eval( arrGlobalInfo[ i ] );

	nImagesTN = arrImageInfo.length - 1;
	arrImageInfo.length = nImagesTN;
	for( member in arrImageInfo ) {
		var imgTitle = arrImageInfo[ member ][ 3 ];
		if( eval( "typeof imgTitle" ) != "string" ) {
			alert( "WEBMASTER: A fatal error has been detected. The image data file is malformed. Specifically, an image title is represented as a \"non-string\" data type. Here are some possible causes...\n\n1) Make sure each record ends with a comma like this: ...\"b\",1], <== Note that all records must end with trailing comma." );
			return;
		}
		else arrImageInfo[ member ][ 3 ] = imgTitle.replaceLiteral( "'", "^^" );

	}

	var dataError = getDataErrorsTN();

	if( dataFile ) {

		tnRemainingTN = nImagesTN;

		self.status = "Rendering data as HTML...";

		var tnHTML = "<DIV ID='thumbnails0'>";

		if( dataError )
			tnHTML += "<DIV CLASS='lgTN'>DATA ERROR: Unable to show Thumbnail Page</DIV>";

		else {


	//  "1-7.jpg|280|280|Grand Wailea Hotel|null|0,0,0|1",
	//      0      1   2          3           4    5   6

			// Gets image title from data base and appends it onto the end of p.NAME...
			// Formats image name as TN from large version name
			for( var i = 0; i < nImagesTN; i++ ) {
				var thisImg = arrImageInfo[i];
				var fileExt = "TN.jpg";
				if( prvWedHmPg ) {
					thisImg[ 0 ] = thisImg[ 0 ].replaceLiteral( "+", "" );
					fileExt = ".jpg";
				}
				thisImg[ 0 ] = thisImg[ 0 ].split('.')[ 0 ] + fileExt;
				thisImg = thisImg.join('|');
				arrImageInfo[i] = thisImg;
			}

			var tnNum = 0;

			thePageTitle = PAGE_TITLE;
			thePageTitle = thePageTitle.replaceLiteral( '^^', '’' );
			thePageTitle = thePageTitle.replaceLiteral( '^', '&#34;' );

			if( WRITE_TN_PAGE_TITLE ) tnHTML += "<DIV CLASS='lgTN' ALIGN='center'>" + thePageTitle + "</DIV><DIV CLASS='vSpacer10'>&nbsp; </DIV>";

			tnHTML += "<TABLE ALIGN='center' BORDER=0 CELLPADDING=0 CELLSPACING=0>";

			var nRows = Math.floor( nImagesTN / nImagesPerRowTN ) + 1;

			///////////////// START ROWS /////////////////
			for( var row = 0; row < nRows; row++ ) {

				tnHTML += "<TR ALIGN='center' VALIGN='middle'><TD ALIGN=center NOWRAP>";

				///////////////// START TN's /////////////////
				for( var i = 0; i < nImagesPerRowTN; i++ ) {

					var thisTnImgData = '';
					var writeTN = ( tnNum < nImagesTN ) ? true : false;

					if( writeTN ) {
						thisTnImgData = arrImageInfo[ tnNum ];
						var arrThisTnImg = thisTnImgData.split('|');
						var thisTnImg = arrThisTnImg[0];
						var lgWid = arrThisTnImg[1];
						var lgHgt = arrThisTnImg[2];
						var imgTitle = arrThisTnImg[3];

						if( imgTitle == "*" )
							imgTitle = thePageTitle;
						else if( imgTitle.indexOf( "*+" ) >= 0 )
							imgTitle = thePageTitle + imgTitle.split( "+" )[ 1 ];
						else if( imgTitle.indexOf( "+*" ) >= 0 )
							imgTitle = imgTitle.split( "+" )[ 0 ] + thePageTitle;

						var toolTip = ( imgTitle == "&nbsp;" || imgTitle == "-" ) ? "" : imgTitle.removeHtmlTags();
						if( !WRITE_TN_IMAGE_TITLE ) toolTip = "";
						if( imgTitle == "&nbsp;" || imgTitle == "-" ) imgTitle = "";
						toolTip = toolTip.replaceLiteral( '^^', '’' );
						toolTip = toolTip.replaceLiteral( '^', '&#34;' );

						var lgBorderCol = arrThisTnImg[ 4 ]; // RGB Triplet
						var lgBorderWid = ( arrThisTnImg[ 5 ] == "*" ) ? IMG_BORD_WID_TN : arrThisTnImg[ 5 ];

						var tn = IMAGE_FOLDER + thisTnImg;
						tnNum++;
					}

					var imageNum = row * nImagesPerRowTN + i + 1;

// [ 'images', 'princeWFReid.jpg', 347, 351, 'Maui Prince', 'b', 1, LinkObj, sectName ]
// IV.show( ImgInfoArr )

					if( writeTN ) {

						var lgImgName = thisTnImg.replaceLiteral( "TN.", "." );
						if( prvWedHmPg ) lgImgName  = thisTnImg.replaceLiteral( ".", "+." );

						var toolTipColon = ( toolTip ) ? ": " : "";
						toolTip = imageNum + "/" + nImagesTN + toolTipColon + toolTip;

						tnHTML += "<A HREF='javascript:void( 0 );' onMouseOver='self.status=\""+ imageNum + " of " + nImagesTN + "; click to view full-size\"; return true;' onMouseOut='self.status=\"\"; return true;' TITLE=\""+ toolTip +"\" onClick='return IV.show( [ \"" + IMAGE_FOLDER + "\", \"" + lgImgName + "\", " + lgWid + ", " + lgHgt + ", \"" + imgTitle + "\", \"" + lgBorderCol + "\", " + lgBorderWid + ", this, \"thumbnails\" ] );'><IMG SRC='" + tn + "' BORDER='0' HSPACE='2' VSPACE='2' WIDTH='" + widthTN + "' HEIGHT='" + heightTN + "' ALT=\""+ toolTip +"\" class='borderImage' onMouseover='borderIm(this,activeColorTN)' onMouseout='borderIm(this,staticColorTN)' onAbort='this.src=this.src;' onLoad='tnRemainingTN--;' onError='tnRemainingTN--;'></A>";

					}

				}
				///////////////// END TN"s /////////////////

				tnHTML += "</TD></TR>";

			///////////////// END ROWS /////////////////

			}

			tnHTML += "</TABLE><BR></DIV><SPAN ID='thumbnails' CLASS='off'></SPAN>";

			document.write( tnHTML );

			self.status = "";

		}
	}
}
