// photoengine.js

var gIndex = 0;
var pIndex = 0;
var photojs;

function removeChildren(el) {
	while (el.childNodes.length > 0) el.removeChild(el.lastChild);
}

function setText(elname, text) {
	var el = document.getElementById(elname);
	if (el) {
		removeChildren(el);
		el.appendChild(document.createTextNode(text));
	}
}

function fillCombo(elname, items, fn) {
	var el = document.getElementById(elname);
	el.options.length = items.length;
	for (var i = 0; i < items.length; i++) {
		var text = items[i];
		if (fn) text = fn(text, i);
		el.options[i].text = text;
		el.options[i].value = i;
	}	
}

function setComboIndex(elname, index) {
	var el = document.getElementById(elname);
	el.selectedIndex = index;
}

function setGalleryName(gname, index) {	
	//setText("gname", gname);
	//setText("pcount", gname);
	setComboIndex("gname", index);
}

function setPhotoName(pname, index) {
	//setText("pname", pname); // for text field	
	setComboIndex("pname", index);
}

function setPhotoUrl(psrc) {
	document.photo.src = psrc;
}

function setComment(comment) {
	var el = document.getElementById("comment");
	if (!comment) comment = "";
	el.innerHTML = comment;
}

function setCredit(credit) {
	var el = document.getElementById("credit");
	if (credit) {
		el.style.visibility = "visible";
		el.innerHTML = "<small><em>Photo Credit: "+credit+"</em></small>";
	}
	else {
		el.style.visibility = "hidden";
	}	
}

function getPhotoCount() {
	for (var i = 0; i < galleries.length; i++) {
		count += photos[i].length;
	}
}

function getGalleryCount() {
	return galleries.length;

}

function dump(obj) {
	var str = "";
	for (key in obj) { str += key+"="+obj[key]+"  "; }
	return str;
}

function dumpkeys(obj) {
	var str = "";
	for (key in obj) { str += key+" "; }
	return str;
}

function getGalleryIndex(key) {
	//
	var n = (parseInt(key) - 1);
	if (isNaN(n) || n < 0 || n >= galleries.length) n = 0;
	return n;
}

function getGalleryIndexFromKey(key) {
	for (var i = 0; i < galleries.length; i++) {
		if (galleries[i].key == key) return i;
	}
	return 0;
}

function getPhotoIndex(key) {
	var n = (parseInt(key) - 1);
	if (isNaN(n) || n < 0) n = 0;
	return n;
}

function init() {	
	if (window.gparam) {		
		gIndex = getGalleryIndexFromKey(gparam);
		// if gparam is preset, disable the gallery selection
		document.getElementById("gntable").style.display = "none";
		document.getElementById("gntitle").innerHTML = galleries[gIndex].name;
	}
	else {
		gIndex = getGalleryIndex(getParameter("gallery"));
	}
	
	pIndex = getPhotoIndex(getParameter("photo"));
	fillCombo("gname", galleries, function(g, i) { 
		return (i+1)+". "+g.name; 
	} );	
	setGalleryName(null, gIndex);
	document.getElementById("gname").onchange = function() {
		window.location.href = "?gallery=" + (this.selectedIndex+1);
	};
	photojs = galleries[gIndex].url;
}

function getParameter(name) {
	var arr = window.location.search.substring(1).split("&");
	for (var i = 0; i < arr.length; i++) {
		var nv = arr[i].split("=");
		var n = unescape(nv[0]);
		if (n == name) return unescape(nv[1]);
	}	
}

function updateGalleryInfo(gname, photos) {
	setGalleryName(gname, gIndex);
	fillCombo("pname", photos, function(p, i) { 
		return (i+1)+". "+getPhotoName(p); 
	} );
	document.getElementById("pname").onchange = function() {
		pIndex = this.selectedIndex;
		update();
	};	
}

function getPhotoName(photo) {
	var psrc = photo.url;
	var pname = photo.name;
	if (!pname) {
		var n = psrc.lastIndexOf("/");
		pname = psrc.substring(n + 1);
	}	
	return pname;
}

function updatePhotoInfo(photo, index, count) {
	if (index >= 0 && index < count) {
		setPhotoUrl(photo.url);
		var pname = getPhotoName(photo);
		pname = pname + " (" + (index + 1) + " of " + count + ")";
		setPhotoName(pname, index);
		setComment(photo.comment);
		setCredit(photo.credit);

		updatePreviewPhoto(document.thumb8, photos, index, -2);	
		updatePreviewPhoto(document.thumb9, photos, index, -1);
		updatePreviewPhoto(document.thumb0, photos, index, 0);
		updatePreviewPhoto(document.thumb1, photos, index, 1);
		updatePreviewPhoto(document.thumb2, photos, index, 2);
	}	
	else {
		setPhotoUrl("");
		setPhotoName("");
		setComment("");
		setCredit("");
	}
}

function updatePreviewPhoto(el, photos, index, offset) {
	var n = index + offset;
	if (n >= 0 && n < photos.length) {
		//var cls = photos[n].portrait ? "p thumb" : "l thumb";
		var cls = photos[n].portrait ? "thumb" : "thumb";
		if (offset == 0) cls += " sel";
		var a0 = 4, a1 = 3; // assume 4x3 aspect unless other specified
		if (photos[n].portrait) {
			a0 = 3;
			a1 = 4;
		}
		if (photos[n].aspect) {
			a0 = photos[n].aspect[0];
			a1 = photos[n].aspect[1];
		}
		var a = (a1 > a0) ? a1 : a0;
		var max = 104;
		var w = Math.floor(max * a0 / a), h = Math.floor(max * a1 / a);
		el.onload = function() { 
			el.className = cls; 
			el.style.width = w; 
			el.style.height = h; 
			el.style.marginLeft = (h > w) ? Math.floor((h - w) / 2) : 0;
		};
		el.title = photos[n].name;
		el.src = photos[n].url;
		el.style.visibility = "visible";
		el.onclick = function() { pIndex = n; update(); };
	}
	else {
		el.style.visibility = "hidden";
		el.onclick = null;
	}
}

function update() {
	var str = galleries[gIndex].name;	
	var gal = photos;
	updateGalleryInfo(str, gal);
	updatePhotoInfo(gal[pIndex], pIndex, gal.length);
}

function navigate(which, type) {
	var value = which ? gIndex : pIndex;
	var max = which ? galleries.length - 1 : photos.length - 1;
	if (type == 1) value = 0;
	else if (type == 2) value--;
	else if (type == 3) value++;
	else if (type == 4) value = max;
	if (value < 0) value = 0;
	if (value > max ) value = max;
	if (which) window.location.href = "?gallery="+(1+value);  
	else pIndex = value;
	update();
}


