
// --------------------------------------------------<  Slide Show Ojbect  >------------------- //


// Initialize the slide show object
SlideShowController = function(InstanceName){
	this.InstanceName = InstanceName;
	this.Images = new Array();
	this.Index = 0;
	this.IsRunning = false;
	this.Interval = null;
	this.Slide = null;
}


// Adds an image to the slide show
SlideShowController.prototype.AddImage = function(strSrc, strKey){
	// Create a new image
	this.Images[ this.Images.length ] = new SlideShowImage(strSrc, strKey);
}


// This goes to the next image in the slide show
SlideShowController.prototype.Next = function(){
	
	// Clear the interval, if it's in use
	clearInterval(this.Interval);
	
	// Check to see if the slide show is running
	if (this.IsRunning){
	
		// Increment the index
		this.Index++;
		
		// Check for bounds
		if (this.Index > (this.Images.length - 1)){
			this.Index = 0;
		}
		
		this.ShowSlideByIndex();
	}
}


// Shows the slide at the given index
SlideShowController.prototype.ShowSlideByIndex = function(intIndex){
	
	// Clear the interval, if it's in use
	clearInterval(this.Interval);
	
	// Check to see if the index is sent, otherwise, use the local index
	if (intIndex != null){
		this.Index = intIndex;
	}
	
	// Load the image object
	this.Images[this.Index].Load();

	// Check to see if the current image is loaded
	if (this.Images[this.Index].IsLoaded()){
							
		// Apply the filter
		if (this.Slide.filters){
			this.Slide.filters[0].apply();					
		}
		
		// Set the new image
		this.Slide.src = this.Images[this.Index].Src;
		
		// Play the filter
		if (this.Slide.filters){
			this.Slide.filters[0].play();
		}			
		
		// Check to see if the slide show is running
		if (this.IsRunning){
			// Set the interval for updating image
			this.Interval = setInterval(this.InstanceName + ".Next()", 3500);		
		}
		
		// Load the next image if it is available
		if (this.Index < (this.Images.length - 1)){
			this.Images[this.Index + 1].Load();
		}
		
	} else {
		
		// The image has not loaded yet, so call this function again 
		this.Interval = setInterval(this.InstanceName + ".ShowSlideByIndex()", 50);
		
	}
}


// This forces the next image to be the one at the given key
SlideShowController.prototype.ShowSlideByKey = function(strKey){
	var intIndex = 0;
	var objSlide = null;
	
	// Loop over slides to find the one at the given key
	for (var intIndex = 0 ; intIndex < this.Images.length ; intIndex++){
		if (this.Images[intIndex].Key == strKey){
			objSlide = this.Images[intIndex];
			break;
		}
	}
	
	// Check to see if we found an object
	if (objSlide){
		this.ShowSlideByIndex(intIndex);
	}
	
	return;
}


// This randomly sorts the images
SlideShowController.prototype.Resort = function(){
	var objTemp = null;
	var intRandIndex = 0;
	
	// Loop over and sort
	for (var i = 0 ; i < this.Images.length ; i++){
		intRandIndex = Math.floor((Math.random() * (this.Images.length - 1)));
		
		// Get rand item
		objTemp = this.Images[intRandIndex];
		
		// Swap current images
		this.Images[intRandIndex] = this.Images[i];
		this.Images[i] = objTemp;
	}
}


// This sets the slide image reference
SlideShowController.prototype.SetSlideImage = function(ImageName){
	this.Slide = document.images[ImageName];
	
	// If the image exists in the document, then add the style
	if (this.Slide){
		this.Slide.style.filter = "blendTrans(duration=1)";
	}
}


// Starts the slide show
SlideShowController.prototype.Start = function(){
	// Check to make sure that there is more than one image
	if (this.Images.length > 1){
		// Set the boolean to say we are running
		this.IsRunning = true;
		
		// Call next image
		this.ShowSlideByIndex();
	}
}


// Stops the current slide show
SlideShowController.prototype.Stop = function(){
	// Set the boolean for running
	this.IsRunning = false;
}


// --------------------------------------------------<  Slide Show Slide  >-------------------- //


// Creates a slide show image object
SlideShowImage = function(Src, Key){
	this.Src = Src;
	this.Img = new Image();
	this.Key = Key;
}


// This loads the image into the image object
SlideShowImage.prototype.Load = function(){
	if (this.Img.src.length == 0){
		this.Img.src = this.Src;
	}
}


// This checks to see if the image is loaded
SlideShowImage.prototype.IsLoaded = function(){
	return(this.Img.complete);
}


// --------------------------------------------------<  Additional Code  >--------------------- //


// Create the slide show object
var SlideShow = new SlideShowController("SlideShow");
