var W3C = (document.getElementById) ? 1 : 0;
var IE = (document.all) ? 1 : 0;
var NS4 = (document.layers) ? 1 : 0;

function OpenSlideURL (destwindow)
{
	if (this.slidesloaded)
	{
		var dest = this.slides[this.currentslide].slideurl;
		if(destwindow && !destwindow.closed && dest)
		{
			destwindow.location = dest;
			destwindow.focus();
			return true;
		}
		else if(dest)
		{
			var targetwindow = window.open(dest, "slide");
			targetwindow.focus();
			return true;
		}
	}
	return false;
}

function AddTag( thename, thevalue )
{
	this.tags[this.totaltags] = new Object();
	this.tags[this.totaltags].name = thename;
	this.tags[this.totaltags].value = thevalue;
	return this.tags[this.totaltags++];
}

function LoadSlideImage()
{
	this.image.src = this.imageurl;
	this.imageloaded = 1;
}

function Slide(imageurl, slideurl)
{
	this.image = new Image();
	this.imageurl = imageurl;
	this.imageloaded = 0;
	this.slideurl = slideurl;
	this.totaltags = 0;
	this.tags = new Array();
	this.AddTag = AddTag;
	this.LoadImage = LoadSlideImage;
}

function AddSlide(imageurl, slideurl)
{
	this.slides[this.totalslides] = new Slide(imageurl, slideurl);
	return this.slides[this.totalslides++];
}

function LoadSlideShow ()
{
	if (this.totalslides)
	{
		this.slides[0].image.src = this.slides[0].imageurl;
		this.slidesloaded = 1;
		this.currentslide = -1;
		return 1;
	}
	return 0;
}

function NextSlide ()
{
	if (!this.totalslides)
		return;
	this.currentslide++;
	if (this.currentslide >= this.totalslides)
		this.currentslide = 0;
	slide = this.slides[this.currentslide];
	if (!slide.imageloaded)
		slide.LoadImage();

	if (W3C || IE || NS4)
	{
		var theimage = document.images[this.imagename];
		if (IE && !this.paused && this.blendDuration)
		{
			theimage.style.filter="blendTrans(duration="+this.blendDuration+")";
			theimage.style.filter="blendTrans(duration=crossFadeDuration)";
			theimage.filters.blendTrans.Apply();
		}
		if (this.updateTags)
		{
			for (i = 0; i < slide.totaltags; i++)
			{
				var thetag = slide.tags[i].name;
				if (W3C)
					document.getElementById(thetag).innerHTML = slide.tags[i].value;
				else if (IE)
					document.all.item(thetag).innerHTML = slide.tags[i].value;
				else if (NS4)
				{
					var theparent = document.layers['PNS'+thetag];
					if (theparent)
					{
						var thechild = theparent.layers['NS'+thetag];
						if (thechild)
						{
							thechild.document.write("<DIV ID='"+thetag+"' STYLE='font-color: black;'>"+slide.tags[i].value+"</DIV>");
							thechild.document.close();
						}
						else
						{
							alert("Missing layer: NS" + thetag);
						}
					}
					else
					{
						alert("Missing layer: PNS" + thetag);
					}
				}
			}
		}
	}

	theimage.src = slide.image.src;
	if (IE && !this.paused && this.blendDuration)
		theimage.filters.blendTrans.Play();

	var nextslide = this.currentslide + 1;
	if (nextslide >= this.totalslides)
		nextslide = 0;
	slide = this.slides[nextslide];
	if (!slide.imageloaded)
		slide.LoadImage();

	if (!this.paused)
		this.timer = setTimeout(this.name + ".Next()", this.switchspeed * 1000);
}

function PrevSlide ()
{
	this.currentslide -= 2;
	if (this.currentslide < -1)
		this.currentslide = this.totalslides - 2;
	this.Next();
}

function RunSlideShow ()
{
	if (this.paused)
		this.paused = 0;
	else
		clearTimeout(this.timer);

	if (this.playimage.pause)
		UpdateImage(this.playimage.name, this.playimage.pause);
	if (this.stopimage.pause)
		UpdateImage(this.stopimage.name, this.stopimage.pause);

	this.Next();
}

function StopSlideShow ()
{
	if (this.paused)
		return;

	if (this.playimage.play)
		UpdateImage(this.playimage.name, this.playimage.play);
	if (this.stopimage.play)
		UpdateImage(this.stopimage.name, this.stopimage.play);

	clearTimeout(this.timer);
	this.paused = 1;
}

function SetPlayImage(name, playsrc, pausesrc)
{
	this.playimage.name = name;
	if (playsrc)
	{
		this.playimage.play = new Image();
		this.playimage.play.src = playsrc;
	}
	else
		this.playimage.play = 0;
	if (pausesrc)
	{
		this.playimage.pause = new Image();
		this.playimage.pause.src = pausesrc;
	}
	else
		this.playimage.pause = 0;
}

function SetStopImage(name, stopsrc, pausesrc)
{
	this.stopimage.name = name;
	if (stopsrc)
	{
		this.stopimage.stop = new Image();
		this.stopimage.stop.src = stopsrc;
	}
	else
		this.stopimage.play = 0;
	if (pausesrc)
	{
		this.stopimage.pause = new Image();
		this.stopimage.pause.src = pausesrc;
	}
	else
		this.stopimage.pause = 0;
}

function UpdateImage(name, newimage)
{
	document.images[name].src = newimage.src;
}

function SlideShow (name, imagename)
{
	this.name = name;
	this.totalslides = 0;
	this.currentslide = 0;
	this.switchspeed = 5;
	this.blendDuration = 1;
	this.imagename = imagename;
	this.imageWidth = 640;
	this.imageHeight = this.imageWidth / 4 * 3;
	this.imageBorder = 0;
	this.showNoPhoto = 0;
	this.showNav = 0;
	this.updateTags = 0;
	this.slidesloaded = 0;
	this.currentslide = 0;
	this.paused = 1;
	this.timer;
	this.slides = new Array();

	this.OpenSlideURL = OpenSlideURL;
	this.AddSlide = AddSlide;
	this.Load = LoadSlideShow;
	this.Next = NextSlide;
	this.Prev = PrevSlide;
	this.Run = RunSlideShow;
	this.Stop = StopSlideShow;

	this.playimage = new Object();
	this.stopimage = new Object();

	this.SetPlayImage = SetPlayImage;
	this.SetStopImage = SetStopImage;
}
