// gTheSlides must be defined in the surrounding parent frame which uses this script


function slides() {
//extend( slides );
//this.count = 0;
this.count = 0;
var currentslide = 0;

this.initslides = initSlides;
this.hello = Hello;
this.getslideidbyname = getSlideIdByName;
this.getnextslide = getNextSlide;
this.getprevslide = getPrevSlide;
this.gettheslides = getTheSlides;

//window.alert('slides:constructor');
}


function Hello() {
this.count++;
//return 'hello from slides '+this.count+' . ';
//return 'hello from slides ';
}

function getTheSlides() {
//	window.alert("getTheSlides "+this.slides);
	return this.slides
}

function setslide(id) {
//	this.currentslide = id;
	currentslide = id;
//	window.alert('set to ' + currentslide);
}

function initSlides(aItems) {
//window.alert('initslides');
this.slides = aItems;
this.maxslides = this.slides.length;

/*
if (this.slides == null)
	window.alert('are null');
else
	window.alert('are not null ');
*/

}



function getSlideIdByName(aInfo, aSetIt) {
//window.alert('16='+aInfo);
aret = -1;
// the first one is a special one to set context
apath = this.slides[0][0]+''+this.slides[0][1];
compareslide = 0;

//window.alert('16a='+this);
//window.alert('len='+this.maxslides);
	for (i=1; i<this.maxslides; i++)
	{

		// process context switching
		if (this.slides[i][0] == 'X')
		{	
			apath = this.slides[i][1]+''+this.slides[i][2];
//	window.alert('context =  ' + apath);
			continue;
		}

		// skip unknown
		if ((this.slides[i][0] != 'p') && (this.slides[i][0] != 't') && (this.slides[i][0] != 'd'))
		{
			continue;	
		}

		compareslide++;	// found a real slide
		ckp=apath + '' + this.slides[i][2];
//	window.alert('checking ' + ckp + ' against ' + aInfo);
		if ( aInfo == ckp )
		{
//	window.alert('found it at ' + i);
			aret = i;
			if (aSetIt) setslide(compareslide);
			break;
		}
		else
		{
//	window.alert('next');
		}
	}

//	window.alert('found '+aret);

	return aret;
}

// compareslide is the picture or thumb slide which can be displayed, this
// is used to skip all the non-printable entries in aslides which don't
// constitute a valid slide
function getSlideNameById(aslides, aId, aSetIt) {
//window.alert('17='+aId);

compareslide = 1;
lastlinkid = -1;
sret = '';
//window.alert('17a='+aslides);
maxslides=aslides.length;
//window.alert('17b='+maxslides);
// the first one is a special one to set context
//apath = this.slides[0][0]+''+this.slides[0][1];
apath = aslides[0][0]+''+aslides[0][1];

//window.alert('len='+maxslides);
	for (i=1; i<maxslides; i++)
	{
		// process context switching
		if (aslides[i][0] == 'X')
		{	
			apath = aslides[i][1]+''+aslides[i][2];
//	window.alert('context =  ' + apath);
			continue;
		}

		// process link
		if (aslides[i][0] == 'l')
		{
			lastlinkid = i;
			continue;
		}

		// skip unknown
		if ((aslides[i][0] != 'p') && (aslides[i][0] != 't') && (aslides[i][0] != 'd'))
		{
			continue;	
		}

//	window.alert('checking ' + aId + ' against ' + compareslide);
		if ( aId == compareslide )
		{
//	window.alert('found it at i=' + i);

			if (i == (lastlinkid + 1))
			{
				msg="Found a related topic -*- " + aslides[lastlinkid][1] + " -*- \n\nSelect Ok to see the new set of photos, Cancel to continue with the current set";
				if ( window.confirm(msg) )
				{	
					 
					//parent.frames.slideWindow.location=aslides[lastlinkid][3];
					parent.frames.mainWindow.location=aslides[lastlinkid][3];
					sret=" ";
					break;
				}
			}

			if (aSetIt)
				setslide(compareslide);

			sret =  "img="+apath+''+aslides[i][2];

			if (aslides[i][3] != null)
				sret = sret+"&txt="+escape(aslides[i][3]);
			break;
		}
		else
		{
			compareslide++;
//	window.alert('next');
		}
	}

//	window.alert('found '+sret);

	return sret;
}


function getNextSlide(aslide) {

//	window.alert('0..'+currentslide);
//	window.alert('1..'+this.maxslides);
	if (currentslide < (this.maxslides-1))
	{
		nxs = currentslide+1;
	}

//	window.alert('2..'+nxs);
	rslide = getSlideNameById(aslide, nxs, 1);
//	window.alert('3..'+rslide);
	
	return rslide; 
}

function getPrevSlide(aslide) {

	if (currentslide > 0)
	{
		nxs = currentslide -1;
	}

	rslide = getSlideNameById(aslide, nxs, 1);

	return rslide; 
}
