function jsDoSlideTn(obj, offset)
{
	var imgname = obj.name;
	var maxImg = obj.nimgs;
	
	var imgIdx = imgname+"_pic";
	var imgElem = document.images[imgIdx];
	var currSrc = imgElem.src;
	var imgNum = jsGetSlideNum(currSrc);

	var nextState = 0;

	var newSrc = '';
	if(offset == 0)
	{
		if(imgNum == 0)	// Thumbnail
		{
			jsSetImgSize(imgIdx, false, obj.wide);
			newSrc = jsMakeImgFilename(imgname, 1);
		}
		else
		{
			jsSetImgSize(imgIdx, true, obj.wide);
			newSrc = jsMakeImgFilename(imgname, 0);
		}
	}
	else
	{
		var newImgNum = imgNum + offset;
		imgNum += offset;
		if((imgNum<1) || (imgNum>maxImg))
			return false;

		newSrc = jsMakeImgFilename(imgname, imgNum);
	}

	// Show disabled buttons until img-load
	jsDoSlideBtn(imgname, 0, 'd');
	var btnElem = jsGetSlideBtnElem(imgname,  0);
	
	jsDoSlideBtn(imgname, 1, 'd');
	var btnElem = jsGetSlideBtnElem(imgname,  1);
	btnElem.style.cursor = 'wait';

	var altNum = jsGetSlideNum(imgElem.src);
	if(altNum < maxImg)
		document.images[imgname+"_extra"].src = "../../art/empty.gif";

	imgElem.alt = "Loading...";
	imgElem.src = newSrc;
	imgElem.style.cursor = 'wait';
		
    return false;
}
function jsOnLoadSlide(obj)
{
	var imgname = obj.name;
	var maxImg = obj.nimgs;

	var imgIdx = imgname+"_pic";
	var imgElem = document.images[imgIdx];
	var currSrc = imgElem.src;
	var imgNum = jsGetSlideNum(currSrc);
	
	var backState = 'x';
	var nextState = 'x';

	var alt = "Click to shrink";
	
	if(imgNum == 0)	// Thumbnail
	{
		backState = 'h';
		nextState = 'h';
		alt = "Click to enlarge";
	}
	else
	{
		if(imgNum > 1)
		{
			if(imgNum < maxImg)
				nextState = 'e';
			backState = 'e';
		}
		else
		{
			nextState = 'e';
		}
	}
	
	imgElem.alt = alt;
	imgElem.style.cursor = 'default';
	jsDoSlideBtn(imgname, 0, backState);
	jsDoSlideBtn(imgname, 1, nextState);	
	
	return true;
}
function jsSetImgSize(imgIdx, thumb, wide)
{
	var imgElem = document.images[imgIdx];

	if(thumb)
	{
		if(wide)
		{
			imgElem.height = 100;
			imgElem.width = 133;
		}
		else
		{
			imgElem.height = 133;
			imgElem.width = 100;
		}
	}
	else
	{
		if(wide)
		{
			imgElem.height = 480;
			imgElem.width = 640;
		}
		else
		{
			imgElem.height = 640;
			imgElem.width = 480;
		}
	}
}
function jsGetSlideNum(fname)
{
	var imgNum = -1;
	var zz = fname.lastIndexOf("_tn.");
	if(zz > 0)
		imgNum = 0;
	else
	{
		var imgNumStr = "";
		zz = fname.lastIndexOf(".");
		var nc = 0;
		while(zz > 0)
		{
			zz--;
			var yy = fname.substr(zz, 1);
			var n = yy.charCodeAt(0);
			if((n>=48) && (n<=57))
				nc++;
			else
				break;
		}

		if(nc > 0)
		{
			var imgStr = fname.substr(zz+1, nc);
			imgNum = parseInt(imgStr, 10)
		}
	}
	return imgNum;
}

// state: h:hide, d:disabled, e:enabled, x:no next/prev slide
function jsDoSlideBtn(baseName, next, state)
{
	var imgFile = "../../art/empty.gif";
	var width = 0;
	var alt = "";

	switch(state)
	{
		case 'h':
			// width = 0;
			break;
		case 'd':
			width = 25;
			if(next)
			{
				imgFile = "../../art/nextd.gif";
				alt = "Loading image...";
			}
			else
			{
				imgFile = "../../art/backd.gif";
				alt = "Loading image...";
			}
			break;
		case 'e':
			width = 25;
			if(next)
			{
				imgFile = "../../art/next.gif";
				alt = "Click here to see the next slide";
			}
			else
			{
				imgFile = "../../art/back.gif";
				alt = "Click here to see the previous slide";
			}
			break;
		case 'x':
			width = 25;
			if(next)
			{
				imgFile = "../../art/nextx.gif";
				alt = "(this is the last slide)";
			}
			else
			{
				imgFile = "../../art/backx.gif";
				alt = "(this is the first slide)";
			}
			
			break;
	}
	
	var btnElem = jsGetSlideBtnElem(baseName, next);
	btnElem.width = width;
	btnElem.src = imgFile;
	btnElem.alt = alt;
	btnElem.style.cursor = 'default';
}

function jsGetSlideBtnElem(baseName, next)
{
	var imgIdx = baseName+"_back";
	if(next)
		imgIdx = baseName+"_next";
	return document.images[imgIdx];
}

function jsMakeImgFilename(imgname, imgNum)
{
	var numstr = imgNum;
	if(imgNum < 10)
	{
		if(imgNum < 1)
			numstr = "tn";
		else
			numstr = "0" + imgNum;
	}
	var ret = imgname+"_"+numstr+".jpg";
	return ret;
}

function jsShowSlide(ss, n)
{
	var imgname = ss.name;
	var imgIdx = imgname+"_pic";

	jsSetImgSize(imgIdx, false, ss.wide);
	document.images[imgIdx].src = jsMakeImgFilename(imgname, n);

	jsDoSlideBtn(imgname, 0, (n>1)?'e':'x');
	jsDoSlideBtn(imgname, 1, ((n<ss.nimgs)?'e':'x'));
}
