// Loads a set of images, one at a time.
// Externals
//					ImageSet.ShowPreviousOrNext()
//					ImageSet.ShowFullSizeJimg()
//					ImageClass.getImageId()
//					ImageClass.getImagePath()


function _ImageLoader()
{
	this.Running = false
	this.getImageId = getImageId
	this.InitSequence = InitSequence
	this.PreLoadImage = PreLoadImage
	this.GetNextImage = GetNextImage
	this.InitImageLoad = InitImageLoad

	this.WaitForImageComplete = WaitForImageComplete
	this.RequestedLoadedOK = RequestedLoadedOK
	this.RequestedLoadedBad = RequestedLoadedBad

	function getImageId(ImageName)	{ return this.ImageClass.getImageId(ImageName) }

	function InitSequence(ImageNames, ImageClass, StartIndex)
	{
		this.ImageNames = ImageNames	      				// Set of images to be loaded and added to the document
		this.ImageClass = ImageClass        				// Class that defines where the images are stored
		this.ImgFolder = ImageClass.getImagePath()	// Path for all the images
		this.PreLoadIndex = StartIndex							// Current Image to get (or waiting for)

		this.PreLoadJimg = null  										// The image being pre-loaded
		this.RequestedJimg = null										// The image that is requested by the user
		this.TimeEvent = null												// Waits for an image to complete loading

		if( 0 == StartIndex && ! this.Running )
			this.PreLoadImage()
	}

	function PreLoadImage()
	{
		// Find the next Image that is not already loaded
		for( ; this.PreLoadIndex < this.ImageNames.length; this.PreLoadIndex++ )
		{
			// If this Image is not already in the document, load it
			var Jimg = $('#' + this.getImageId(this.PreLoadIndex))
			if( Jimg.length == 0 )
			{
				this.PreLoadJimg = this.InitImageLoad( this.PreLoadIndex ).bind('load', ImageLoader.GetNextImage)
																																	.bind('error', ImageLoader.GetNextImage)
				this.Running = true
				return
			}
		}
		// All the images are loaded
		this.Running = false
	}

	function GetNextImage()
	{
		// Get the next Image
		this.PreLoadIndex++
		setTimeout('ImageLoader.PreLoadImage()', 10)
	}

	function InitImageLoad(idx)
	{
		var Jimg = $( new Image() ).attr({src : this.ImgFolder + this.ImageNames[idx],
																			 id : this.getImageId(idx)})
															 .addClass('photo')
															 .css({display:'none',
																		 position:'absolute',
																		 top:0,
																		 left:0,
																		 border:0,
																		 opacity:'hide'
																		})
															 .bind('click', ImageSet.ShowPreviousOrNext)
		$('#imgAreaId').append( Jimg )
		return Jimg
	}



	// Load and show the requested image ASAP
	// ======================================

	// Can be called by a Thumbnail click, Previous/Next buttons or 1st image in a Category
	this.LoadRequestedImage = function( idx )
	{
		// Cancel any repeat request. The user is making a new request
		if( this.TimeEvent )
		{
			clearTimeout( this.TimeEvent )
			this.TimeEvent = null
		}

		var ImgId = this.getImageId(idx)
		var Jimg = $( '#' + ImgId )
		if( Jimg.length != 0 )
		{
			// NOTE the pre-loader may have just added this image and it's not yet complete
			if( ! Jimg[0].complete )
			{
				this.TimeEvent = setTimeout('ImageLoader.WaitForImageComplete("' + ImgId + '")', 1000)
				return
			}
			ImageSet.ShowFullSizeJimg( Jimg )
		}
		else
		{
			this.RequestedName = this.ImageNames[idx]
			this.RequestedJimg = this.InitImageLoad( idx ).bind('load', ImageLoader.RequestedLoadedOK)
																										.bind('error', ImageLoader.RequestedLoadedBad)
		}
	}

	function RequestedLoadedOK( )
	{
		ImageSet.ShowFullSizeJimg( ImageLoader.RequestedJimg )
	}

	function RequestedLoadedBad( )
	{
		window.status = 'Cannot Load ' + ImageLoader.RequestedName
	}

	function WaitForImageComplete( ImageName )
	{
		this.TimeEvent = null
		var ImageId = this.getImageId(ImageName)
		var ImgObj = document.getElementById( ImageId )
		if( ImgObj )
		{
			if( ! ImgObj.complete )
			{
				Debug.raise('Still Incomplete ' + ImageName)
				this.TimeEvent = setTimeout('ImageLoader.WaitForImageComplete("' + ImageName + '")', 1000)
				return
			}
			ImageSet.ShowFullSizeJimg( ImgObj )
		}
	}
}

ImageLoader = new _ImageLoader()

