// 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;
// }

var pix, img1, img2, caption, playInterval;
var curIndex = 0, nextIndex = 1, playRandom = 0, maxWidth = 0, maxHeight = 0;

function showpictures(p, i1, c1)
{
    pix = p;
    curIndex = -1;
    img1 = $(i1);
    caption = $(c1);
	nextIndex = getNextIndex();
    redisplay();
}

function chopup(s)
{
	var paths = s.match(/.*[/](\d{4})[a-c]?(-\d+)*-([^/]*)[/][^/]*$/);
	return RegExp.$1 + " " + RegExp.$3.replace(/-/g, ' ');
}

function reassign()
{
	resizeImage(img2);
	img1.src = img2.src;
	img1.width = img2.width;
	img1.height = img2.height;
	delete img2;
	if (caption) {
		caption.innerHTML = chopup(img1.src);
	}
}

function setMaxWidth(w)
{
	if (w)
		maxWidth = w;
	else
		maxWidth = document.width;
}

function redisplay()
{
	curIndex = nextIndex;
	img2 = new Image();
	img2.onload = reassign;
	img2.src = pix[nextIndex];
//	if (caption) caption.inn
//	  alert("caption=" + img2.src);
    nextIndex = getNextIndex();
}

function resizeImage(img)
{
    if ((maxWidth > 0) && (maxHeight > 0)) {
    	var curWidth = img.width;
    	var curHeight = img.height;
		var curRatio = img.height / img.width;
		var wdtRatio = curWidth / maxWidth;
		var hgtRatio = curHeight / maxHeight;
		if (wdtRatio > hgtRatio) {
			if (curWidth > maxWidth) {
				img.width = maxWidth;
				img.height = maxWidth * curRatio;
			}
		} else {
			if (curHeight > maxHeight) {
				img.height = maxHeight;
				img.width = maxHeight / curRatio;
			}
		}

    } else if (maxWidth > 0) {
		var curWidth = img2.width;
    	if (curWidth > maxWidth) {
			var curHeight = img.height;
			var curRatio = img.height / img.width;
    		img.width = maxWidth;
    		curHeight = maxWidth * curRatio;
    		img.height = curHeight;
    	}

    } else if (maxHeight > 0) {
		var curHeight = img.height;
    	if (curHeight > maxHeight) {
			var curWidth = img.width;
			var curRatio = img.height / img.width;
    		img.height = maxHeight;
    		img.width = maxHeight / curRatio;
    	}
	}
}

function getNextIndex()
{
    if (playRandom) return parseInt(Math.random() * pix.length);
    else return (curIndex + 1) % pix.length;
}

function next()
{
    redisplay();
}

function prev()
{
    if (playRandom) nextIndex = parseInt(Math.random() * pix.length);
    else nextIndex = (pix.length + curIndex - 1) % pix.length;
    redisplay();
}

function play(howfast)
{
    clearInterval(playInterval);
    playInterval = setInterval(next, howfast * 1000);
}

function stop()
{
    clearInterval(playInterval);
}

function randomize(r, list)
{
    // playRandom = r;
    if (r) randomizelist(list);
    else list.sort();
}

function randomizelist(list)
{
	for (var i = 0; i < list.length; i++) {
		var j = parseInt(Math.random() * list.length);
		var t = list[j];
		list[j] = list[i];
		list[i] = t;
	}
}
