var pix, img1, img2, caption, playInterval;
var curIndex = 0, nextIndex = 1, playRandom = 0, maxWidth = 0, maxHeight = 0;

function $(e)
{
    if (typeof(e)!='string') return e;
    if (document.getElementById) e=document.getElementById(e);
    else if (document.all) e=document.all[e];
    else if (document.layers) e=document.layers[e];
    else e=null;
    return e;
}

function setMaxWidth(w)
{
	if (w)
		maxWidth = w;
	else
		maxWidth = document.width;
}

function chopup(s)
{
	var paths = s.match(/.*[/]([^/]*).jpg$/);
	var ret = RegExp.$1.replace(/[-_]/g, ' ');
	return ret;
}

var picnum = 0;
function showPictures(maxcnt)
{
	for (var cnt = 0; cnt < maxcnt; cnt++) {
		var showpic = (picnum+cnt < pictures.length);
		var picdiv = (picnum+cnt)%pictures.length;
		var caption = showpic ? chopup(pictures[picdiv]) : "";

		// blank out the image
		var bimgref = $("img" + cnt);
		if (bimgref) {
		    bimgref.src = "images/blank-pixel.gif";
		}

		var imgref = $("img" + cnt);
		if (imgref) {
		    imgref.src = showpic ? ("pictures/" + pictures[picdiv]) : "images/blank-pixel.gif";
		    imgref.alt = caption;
		    if (showpic) Element.show(imgref);
		    else Element.hide(imgref);
		}

		// update the <a> href
		var aref = $("a" + cnt);
		if (aref) {
			aref.href = showpic ? ("pictures/" + pictures[picdiv]) : null;
		}

		// update the caption
		var captionref = $("caption" + cnt);
		if (captionref) {
			captionref.innerHTML = showpic ? caption : "";
		}

		// update the description
		var iframeref = $("if" + cnt);
		if (iframeref) {
			iframeref.src = "/php/show-my-gallery-description.php?d=" + pictures[picdiv];
		}

		// show or hide the description frame
		var oframeref = $("oframe" + cnt);
		if (oframeref) {
			if (showpic) Element.show(oframeref);
			else Element.hide(oframeref);
		}
		
		// show or hide the info button
		var inforef = $("info" + cnt);
		if (inforef) {
			if (showpic) Element.show(inforef);
			else Element.hide(inforef);
		}
	}

	var numsref = $("nums");
	if (numsref) {
		numsref.innerHTML = // picnum + " " + pictures.length + " " +
				 "Screen " + (1+Math.floor((picnum)/maxcnt)) + " of " +
				 (Math.floor((pictures.length+maxcnt-1)/maxcnt));
	}
}

function nextPictures(maxcnt)
{
	picnum += maxcnt;
	if (picnum >= pictures.length) picnum = 0;
	showPictures(maxcnt);
}

function previousPictures(maxcnt)
{
	picnum -= maxcnt;
	if (picnum < 0) picnum += maxcnt*(1+Math.floor((pictures.length-1)/maxcnt));
	showPictures(maxcnt);
}
