// JavaScript Document

var SlideShows = new Array;
var TickRate = 100;
var CurrentTick = 0;

function SlideObject(SlideName, SlideTitle, SlideText)
{
	this.SlideName = SlideName;
	this.SlideTitle = SlideTitle;
	this.SlideText = SlideText;
}


function SlideShowObject(SlideNames, SlideToChange, SlideTitles, SlideTitleToChange, SlideText, SlideTextToChange, Frequency, InitialTimer)
{
	var i;

	// Methods
	this.AdvanceShow = function()
	{
		if (this.SlideShowStarted==0)
			return 1;
		var NextImage = (this.CurImage+1) % this.SlideImages.length;
		if (this.SlideIsComplete(NextImage)) {
			if (CurrentTick > this.LastTick + this.Frequency) {
				this.NextSlide();
			}
			return 1;
			}
		return 0;
	};
	
	this.NextSlide = function()
	{
		var NextImage = (this.CurImage+1) % this.SlideImages.length;
		this.LastTick = CurrentTick;
		this.CurImage=NextImage;
		this.SetSlideObject();
		this.LoadSlide(this.CurImage+1);
	}
	
	this.PreviousSlide = function()
	{
		var NextImage = (this.CurImage-1 + this.SlideImages.length) % this.SlideImages.length;
		this.LastTick = CurrentTick;
		this.CurImage=NextImage;
		this.SetSlideObject();
	};
	
	this.SlideIsComplete = function(index)
	{
		if (this.SlideImages[index]==null)
			return false;
		else
			return this.SlideImages[index].complete		
	}
	
	this.LoadSlide = function(index)
	{
		if (index>=this.SlideImages.length)
			return;
		if (this.SlideImages[index]==null) {
			this.SlideImages[index] = new Image;
			this.SlideImages[index].src=this.SlideNames[index];
		}
	}

	this.SetSlideObject = function()
	{
		this.LoadSlide(this.CurImage);
		if (this.SlideText!=null) {
			document.getElementById(this.SlideTextToChange).innerHTML = this.SlideText[this.CurImage];
		}
		if (this.SlideTitles!=null) {
			document.getElementById(this.SlideTitleToChange).innerHTML = this.SlideTitles[this.CurImage];
		}
		this.SlideToChange.src=this.SlideImages[this.CurImage].src;
	}
	
	
	this.ToggleSlideShow = function()
	{
		if (this.SlideShowStarted == 0) {
			this.ResetShow();
			this.SlideButton.src = this.SlideStopImage;
		} else {
			this.StopSlideShow();
			this.SlideButton.src = this.SlideStartImage;
		}
	}

	this.SlideButtonOver = function()
	{
		if (this.SlideShowStarted == 1) {
			this.SlideButton.src = this.SlideStopHighlightImage;
		} else {
			this.SlideButton.src = this.SlideStartHighlightImage;
		}
	}

	this.SlideButtonOut = function()
	{
		if (this.SlideShowStarted == 1) {
			this.SlideButton.src = this.SlideStopImage;
		} else {
			this.SlideButton.src = this.SlideStartImage;
		}
	}

	this.StopSlideShow = function()
	{
		this.SlideShowStarted = 0;
		this.SlideButton.src = this.SlideStartImage;
	}

	this.ResetShow = function()
	{
		if (this.SlideNames.length>1) {
			this.SlideShowStarted = 1;
			this.SetSlideObject();
			this.TimerCount=CurrentTick + this.InitialTimer;
			this.LoadSlide(this.CurImage+1); // preload the first two (if they exist)
			this.LoadSlide(this.CurImage+2);
		}
	}
	
	this.ShowSlide = function(index)
	{
		this.StopSlideShow();
		this.CurImage = index;
		this.SetSlideObject();
	}
	
	this.SetStartStopButtons = function(SlideButton, SlideStartImage, SlideStartHighlightImage, SlideStopImage, SlideStopHighlightImage)
	{
		this.SlideButton = SlideButton;
		this.SlideStartImage = SlideStartImage;
		this.SlideStartHighlightImage = SlideStartHighlightImage;
		this.SlideStopImage = SlideStopImage;
		this.SlideStopHighlightImage = SlideStopHighlightImage;
	}

	this.SlideNames = SlideNames;
	this.Frequency = Frequency; // in ms
	this.SlideImages = new Array(SlideNames.length);
	for (i=0; i< SlideNames.length; i++)
		this.SlideImages[i] = null;
	this.LoadSlide(0); // preload the first two
	this.LoadSlide(1);
	this.InitialTimer = InitialTimer;
	this.CurImage = 0;
	this.TimerCount = 0;
	this.SlideShowStarted = 1;
	this.LastTick = CurrentTick + InitialTimer;
	this.SlideIndex = SlideShows.length;
	this.SlideToChange = SlideToChange;
	this.SlideTextToChange = SlideTextToChange;
	if (SlideText!=null) {
		this.SlideText = new Array(SlideText.length);
		for (i=0; i< SlideText.length; i++) {
			this.SlideText[i] = SlideText[i];
		}
	} else {
		this.SlideText = null;
	}
	
	this.SlideTitleToChange = SlideTitleToChange;
	if (SlideTitles!=null) {
		this.SlideTitles = new Array(SlideTitles.length);
		for (i=0; i< SlideTitles.length; i++) {
			this.SlideTitles[i] = SlideTitles[i];
		}
	} else {
		this.SlideTitles = null;
	}
	
	SlideShows[SlideShows.length] = this; // add to global array
}

// Public entry points... shouldn't modify, but these should be deprecated
function SetStartStopButtons(SlideObject, SlideButton, SlideStartImage, SlideStartHighlightImage, SlideStopImage, SlideStopHighlightImage)
{
	SlideObject.SetStartStopButtons(SlideButton, SlideStartImage, SlideStartHighlightImage, SlideStopImage, SlideStopHighlightImage);
}

function ToggleSlideShow(SlideObject)
{
	SlideObject.ToggleSlideShow();
}

function SlideButtonOver(SlideObject)
{
	SlideObject.SlideButtonOver();
}

function SlideButtonOut(SlideObject)
{
	SlideObject.SlideButtonOut();
}

function StopSlideShow(SlideObject)
{
	SlideObject.StopSlideShow();
}

function ResetShow(SlideObject)
{
	SlideObject.ResetShow();
}

function AutoSlideShow()
{
	var i;
	var good = 0;
	for (i=0; i<SlideShows.length; i++) {
		if (SlideShows[i].AdvanceShow()==1)
			good++;
	}
	if (good==SlideShows.length)
		CurrentTick+=100;
	var delay = setTimeout("AutoSlideShow()",TickRate);
}

function StartSlideShows()
{
	var i;
	for (i=0; i<SlideShows.length; i++) {
		SlideShows[i].ResetShow();
	}
	var delay = setTimeout("AutoSlideShow()",TickRate);
}
