// JavaScript Document
var is_gallery = 1;

// Sequencing functions
var imgIndex = 0;
gNumberOfImages = gPhotos.length;

imgs = new Array();

// anchor - index+1
function selImg(anchor) {
	imgIndex = anchor - 1;
	i = document.getElementById('mainPic');
	i.src = 'images/' + gPhotos[imgIndex].filename;
	var w = img_files[gPhotos[imgIndex].filename].w;
	var h = img_files[gPhotos[imgIndex].filename].h;
// Menu h = 36, caption h = 22, activeHeight = pic h + 58, top/bot margin min 20px each
//alert(windowWidth + 'x' + windowHeight + '\n' + w + 'x' + h);
// imgHeight set in jsfunc
//	if (h > imgHeight) {
		i.height = imgHeight;		// always resize, incr/reduce
		i.width = Math.round((i.height * w) / h);
/*
	} else {
		i.width = w;
		i.height = h;
	}
*/
	var newSpan = document.createElement("span");
	var caption = gPhotos[imgIndex].meta["description"];
	if (!caption)
		captxt = '  ';
	else
		captxt = unescape(caption);
	caps = captxt.split('\^');
	if (caps.length == 1) {
		cap1 ='';
		cap2 = '';
		cap0 = caps[0];
	} else {
		cap0 = '';
		cap1 = caps[0];
		cap2 = caps[1];
	}
	putcap('L',cap1);
	putcap('C',cap0);
	putcap('R',cap2);
}

function nextImg() {
	if (imgIndex < (gNumberOfImages - 1)) {
		selImg(imgIndex + 2);
	} else {
		selImg(1);
	}
}
function prevImg() {
	if (imgIndex > 0) {
		selImg(imgIndex);
	} else {
		selImg(gNumberOfImages);
	}
}
function docinit() {	// Preload images
	for (var ix = 0; ix < gNumberOfImages; ix++) {
		imgs[ix] = new Image();
		imgs[ix].src = 'images/' + gPhotos[ix].filename;
	}
}
function putcap (which, txt) {
	switch (which) {
		case 'L':
			var	imgcap_id = 'l_imgcap';
			var	captxt_id = 'l_captxt';
			break;
		case 'C':
			var	imgcap_id = 'c_imgcap';
			var	captxt_id = 'c_captxt';
			break;
		case 'R':
			var	imgcap_id = 'r_imgcap';
			var	captxt_id = 'r_captxt';
			break;
	}
	var newSpan = document.createElement("span");
	var newText = document.createTextNode(txt);
	newSpan.appendChild(newText);
	var capdiv = document.getElementById(imgcap_id);
	var spanElm = document.getElementById(captxt_id);
	capdiv.replaceChild(newSpan,spanElm);
	newSpan.id = captxt_id;
}

