// CCC Photo Gallery Definitions
//		Caller (gallery.php) responsible for defining variables imgdir, tndir, initsel, picAry, and pgAry.
//		We assume there is no top row containing prev/back buttons and selbx if only one page is defined.
// 	Examples: imgdir='garden';
//					 picAry[5]=new Array(new Image(335,400), 'garden47.jpg', 'pic caption');
//					 pgAry[2]=new Array('pg title', 'pg text', [3,4,5]);

// global vars
var pgidx=0;
var imgcnt=0;	// on cur pg
var imgidx=0;
var selbx=0;
var bPreload=(document.images)?-1:0;	// -1 => undecided
var bdrNorm='1px #C0C0C0 solid';
var bdrSel='2px #000080 solid';
var bdrHilit='1px #000080 solid';
var bCallTunePg=0;
var intervalElapsed=0;
var intervalMsecs=200;	// how often to check for image load completions
var intervalNxtChk,intervalWait,intervalId;
var instxt,pgcnt;
var tndiv,instdiv,picdiv,capdiv;

function isImgLoaded(imobj) {
 if (imobj) {
	// IE correctly identifies unloaded images as not complete. Others should too.
	// But Gecko-based browsers act like NS4 and incorrectly always return true.
	if (!imobj.complete)
		return false;
	// However, they do have two very useful properties: naturalWidth
	// and naturalHeight. These give the true size of the image. If
	// it failed to load, either of these should be zero.
	if (typeof imobj.naturalWidth != "undefined" && imobj.naturalWidth == 0)
		return false;
 }
 // No other way of checking: assume it's ok.
 return true;
}

function preloadPg(pagix)
{
var curabsidx;
var curpgimgcnt=pgAry[pagix][2].length;
for (var i=0; i<curpgimgcnt; i++) {
	curabsidx=pgAry[pagix][2][i];
	picAry[curabsidx][0].src=imgdir+'/'+picAry[curabsidx][1];
}
}

function waitForPreloads()
{
var curpic,relnum,imgobj;
for (var j=0; j<imgcnt; j++) {
	curpic=pgAry[pgidx][2][j];	// get picAry idx
	imgobj=picAry[curpic][0];
	if (!isImgLoaded(imgobj)) {
		relnum=j+1;  // 1-based
		instdiv.innerHTML='Retrieving pictures ('+relnum+' of '+imgcnt+') ...';
		setTimeout('waitForPreloads()', intervalMsecs);
		return;
	}
}
// all main images on this page are now preloaded
instdiv.innerHTML=instxt;
}

function tunePg() {
	var tdinst='';
	if (bPreload==1)
		instxt='Move your mouse over any image above, or click below.';
	else
		instxt='Click above to select image, or below for next.';

	if (imgcnt>1) {
		if (bPreload==1)
			tdinst='Retrieving pictures (1 of '+imgcnt+') ...';
		else 	// no preloading so clicking required. Also show final msg
			tdinst=instxt;
	}
	instdiv.innerHTML=tdinst;

	if (imgcnt>1 && bPreload==1) {
		preloadPg(pgidx);	// preload big images
		waitForPreloads();	// after done preloading, change "Retrieving..." msg to final msg (instxt)
	}
}

function msOver(relidx) {
	if (bPreload==1)
		selectImg(relidx);
	else
		hiliteImg(relidx,1);
}
function msOut(relidx) {
	if (bPreload==0)
		hiliteImg(relidx,0);
}
function msClk(relidx) {
	if (bPreload==0)
		selectImg(relidx);
}

function hiliteImg(relidx,hion)
{
var bdrsty;
if (relidx!=imgidx) {	// don't chg if cur selected image
	bdrsty=(hion==1)?bdrHilit:bdrNorm;
	document.getElementById('tn'+relidx).style.border=bdrsty;
}
}

function initPg()
{
var tdcont='';
var pidx,aid;
if (imgcnt>1) {
	for (var i=0; i<imgcnt; i++) {
		pidx=pgAry[pgidx][2][i];	// get idx into picAry
		aid='tn'+i;
		if (i>0)
			tdcont=tdcont+'&nbsp;&nbsp; ';	// space thumbnails while allowing browser to break line
		tdcont=tdcont+'<img src="'+imgdir+'/'+tndir+picAry[pidx][1]+'" height="60" id="'+aid+'" onClick="msClk('+i+')" onMouseOver="msOver('+i+')" onMouseOut="msOut('+i+')">';
	}
}
tndiv.innerHTML=tdcont;
}

function waitForFirstLoad() {
	var loaded=(isImgLoaded(document.mainimage))?1:0;
	intervalElapsed = intervalElapsed+intervalNxtChk;	// msecs elapsed so far
	var msecsleft=intervalWait-intervalElapsed;
	if (loaded==1 || msecsleft<=0) {	// decision time
		clearInterval(intervalId);
		bPreload=loaded;	// 0 => slow connection
		if (bCallTunePg==1) {
			bCallTunePg=0;
			tunePg();
		}
	}
	else if (msecsleft<intervalNxtChk)	// getting close - no use waiting full interval
		intervalNxtChk=msecsleft;
}

function selectImg(relidx)
{
imgidx = relidx;	// gbl page-relative index
var pidx=pgAry[pgidx][2][imgidx];	// idx into picAry of current selection
var pnam=picAry[pidx][1];
document.mainimage.src = imgdir + '/' + pnam;
// if undecided about preloading, set bPreload=1 if fast connection, else 0
// 	method: fast connection if loaded after x msecs, where x=msecs needed to load pic at 57K bits/sec
if (bPreload<0) {	// undecided
	if (firstfsiz>0 && firstfnam==pnam) {
		intervalWait=Math.floor( ((1000*firstfsiz*8)/1024*0.93)/57 );	// .93 is approx IP packet overhead (7%)
		// Check every intervalMsecs up to max of intervalWait msecs, but stop if image load is found to be complete
		intervalNxtChk=intervalMsecs;
		intervalElapsed=0;
		intervalId=setInterval("waitForFirstLoad()", intervalNxtChk);
	}
	else	// missing size, or name fails sanity check
		bPreload=0;
}
picdiv.innerHTML=picAry[pidx][2];	// caption text
// hilite matching thumbnail (imgidx)
if (imgcnt>1) {
	var bdrsty;
	for (var i=0; i<imgcnt; i++) {
		bdrsty=(i==imgidx)?bdrSel:bdrNorm;
		document.getElementById('tn'+i).style.border=bdrsty;
	}
}
}

function nextImg()
{
if (imgidx < imgcnt-1) 	// same page
	selectImg(imgidx+1);
else if (pgcnt>1) // go to next page
	nextPg();
else	// this is only page
	selectImg(0);
}

function changePg(newpgx)	// newpgx=-1 => use selbx selection
{
if (bCallTunePg==1 && bPreload<0)	// shouldn't happen, but maybe caller clicked 'Next' during first img timing loop
	bPreload=0;	// so forget preloading altogether
bCallTunePg=0;
pgidx = (newpgx<0) ? selbx.selectedIndex : newpgx;
imgcnt=pgAry[pgidx][2].length; // count of pics on this page (each value in array is an index into picAry)
initPg();	// init page with thumbnails (no decisions based on preloading)
selectImg(0);  // select the first thumbnail and decide whether to do preloading
capdiv.innerHTML=pgAry[pgidx][1];
if (pgcnt>1) {
	document.getElementById('bkbtn').disabled=(pgidx==0);
	document.getElementById('nxbtn').disabled=(pgidx>=pgcnt-1);
}
if (bPreload>=0)	// we've decided whether to preload
	tunePg();	// write instruction line and initiate preloads, if required
else	// tell waitForFirstLoad() to call tunePg() when it decides whether to do preloads
	bCallTunePg=1;
}

function prevPg()
{
if (pgcnt>1) {
	selbx.selectedIndex=(pgidx==0)?pgcnt-1:pgidx-1;
	changePg(-1);
}
}
function nextPg()
{
if (pgcnt>1) {
	selbx.selectedIndex=(pgidx<pgcnt-1)?pgidx+1:0;
	changePg(-1);
}
}

function initGallery()
{
tndiv=document.getElementById('thumbrow');
instdiv=document.getElementById('instrow');
picdiv=document.getElementById('pictxt');
capdiv=document.getElementById('txtcaption');
pgcnt=pgAry.length; // count of pages in gallery
if (pgcnt>1) {
	selbx=document.getElementById('selbox');
	for (var i=0; i<pgcnt; i++) {
		selbx.options[i] = new Option(pgAry[i][0],i,(i==initsel));
	} 
}
changePg(initsel);
}
