// Expects to see an array called "data" with:
// <img1 filename/path>,
// <caption1 text>
// <img2 filename/path>,
// <caption2 text>,
// ...
// if an image name is empty, just the text will be displayed
// along with a "hand" image.
//
// if an image name starts with tn_, thumbs are enabled

// see http://developer.irt.org/script/244.htm
function go(url) {
    if (document.images)
        location.replace(url);
    else
        location.href = url;
}

function fileNameParm(fileIdx)
{
	return filename + '?' + fileIdx + modeParm;
}

function keyHandler(e)
{
    var pressedKey;
    if (document.all)    { e = window.event; }
    if (document.layers) { pressedKey = e.which; }
    if (document.all)    { pressedKey = e.keyCode; }
	if (pressedKey == 32 || pressedKey == 110 || pressedKey == 78)
	{
		// space, n or N = Next
		if (next <= total)
		{
			go(fileNameParm(next));
		}
	}
	else if (pressedKey == 112 || pressedKey == 80)
	{
		// p or P = Prev
		if (prev >= 1)
		{
			go(fileNameParm(prev));
		} 
	}
	else if (pressedKey == 63 && data[index].length != 0)
	{
		// ? show file name
		alert('File="' + data[index] + '"');
	}
}


document.onkeypress = keyHandler;

// based on Nicko's code!

//Get slide number from URL
var url = "" + window.location;
var num = 1;
var mode = 1;
var modeSet = false;
var argstart = url.indexOf("?");
var argend = url.indexOf("&", argstart);
var fileend = argstart;
var filestart = url.lastIndexOf("/") + 1;	// url has forward slashes
if (argstart >= 2)
{
	if (argend > 2)
	{
		num = url.substring(argstart + 1, argend);
		mode = url.substring(argend + 1);
		modeSet = true;
	}
	else
	{
		num = url.substring(argstart + 1);
	}
}
else
{
	fileend = url.length + 1;
}

// work out the name of the html file from the url
var filename = "slidesho.html";	// default
if (filestart != -1)
	filename = url.substring(filestart, fileend);

var allowThumbs = (mode == 1);
var modeParm = "";

if (modeSet)
{
	modeParm = "&" + mode;
}

//Calc other nums
var next = num;
next++;
var prev = num;
prev--;
var total = data.length/2 ;
var index = (num - 1)*2;

//Build the dynamic HTML page
var html = '';
html += '<a href = "../index.html">Up</a> | ';
html += '<a href = "javascript:history.back()">Back</a> | ';
// prev/next links:
if (prev >= 1)
	html += '<a href="javascript:go(\'' + fileNameParm(prev) + '\')"><B>P</B>revious</a> | ';
else
	html += 'Previous | ';
if (next <= total)
	html += '<a href="javascript:go(\'' + fileNameParm(next) + '\')"><B>N</B>ext</a>';
else
	html += 'Next';

// individual page links, 1 | 2 | 3 ...
html += '&nbsp;&nbsp; <FONT SIZE="-1">';
var MaxAltLength = 128;
var anyThumbs = false;
for (var pic = 1; pic <= total; pic++)
{
	var picIdx = (pic -1)*2;
	var isThumb = (data[picIdx].indexOf("tn_") == 0);

	var Alt = data[picIdx + 1];
	Alt = Alt.replace("<B>", "");
	Alt = Alt.replace("</B>", "");
	Alt = Alt.replace("<I>", "");
	Alt = Alt.replace("</I>", "");
	Alt = Alt.replace("<BR>", "");
	Alt = Alt.replace("<P>", "");
	if (Alt.length > MaxAltLength)	// abbreviate
	{
		Alt = Alt.substring(1, MaxAltLength) + "...";
	}
	if (Alt.indexOf("</") != -1)	// found some unknown tags, give up
	{
		Alt = '#' + pic;
	}

	if (isThumb)
	{
		anyThumbs = true;
	}
	if (isThumb && allowThumbs)
	{
		anyThumbs = true;
		if (pic != 1)
			html += '&nbsp;';
		if (pic == num)
			html += '<b>[' + pic + ']:</b>&nbsp;<IMG src="' + data[picIdx] + '"BORDER=3 ALT="' + Alt + '" ALIGN="top" VSPACE=1 HSPACE=1>';
		else
		{
			html += '<a href="javascript:go(\'' + fileNameParm(pic) + '\')">';
			html += '<IMG src="' + data[picIdx] + '" BORDER=1 ALT="' + Alt + '"ALIGN="top" VSPACE=1 HSPACE=1>';
			html += '</a>';
		}
	}
	else
	{
		if (pic != 1)
			html += ' | ';
		if (pic == num)
			html += '<b>[' + pic + ']</b>';
		else
			html += '<a href="javascript:go(\'' + fileNameParm(pic) + '\')"' + 'TITLE="' + Alt + '"' + '>' + pic + '</a>';
	}
}

//html += '</FONT>';
if (anyThumbs)
{
	html += "&nbsp;";
	if (allowThumbs)
	{
		html += '<a href="javascript:go(\'' + filename + '?' + num + '&0' + '\')">Hide Thumbnails</a>';
	}
	else
	{
//		html += ' | <a href="javascript:go(\'' + filename + '?' + num + '&1' + '\')" TITLE="Use thumbnails">Show Thumbnails</a>';
		html += ' | <a href="javascript:go(\'' + filename + '?' + num + '&1' + '\')" TITLE="Show Thumbnails"><IMG src="../thumbs.gif" BORDER=1 ALIGN="top"></a>';
	}
}

// the caption/text:
html += '</FONT><br><br>';
var caption = data[index + 1];
while (caption.indexOf("$mode$") >= 0)
{
	//  is there something like replaceAll()?
	caption = caption.replace("$mode$", modeParm);
}
html += caption + '<p>';
html += '<CENTER>';
if (data[index].length == 0)
{
	// no image, just text, if reqd put up a "next" img (the hand).
	if (next <= total)
		html += '<a href="javascript:go(\'' + fileNameParm(next) + '\')"><IMG src="' + '../slidesho.gif' + '" BORDER=0 ALT="Next"></A><br>';
}
else
{
	var picName = data[index];
	if (picName.indexOf("tn_") == 0)
	{
		picName = picName.substring(3);
	}
	// the picture, make it act like Next:
	if (next <= total)
		html += '<a href="javascript:go(\'' + fileNameParm(next) + '\')"><IMG src="' + picName + '" BORDER=0 ALT="Next"></A><br>';
	else
		html += '<IMG src="' + picName + '" BORDER=0><br>';
}
html += '</CENTER>';
document.write(html);

