var ImagesLoaded = false;
var BarPos=0;
var ImageCache;
var NumLoadedImages = 0;	//Note LOADED images. This is set after all image pre-load code has completed.
var MaxWidth = 0;
var MaxHeight = 0;

function ImagePreloader(images, callback)
{
// store the call-back
this.callback = callback; 

// initialize internal state.
this.Loaded = 0;
this.Processed = 0;
this.aImages = new Array;

// record the number of images.
this.nImages = images.length;

// for each image, call preload()
for (var i=0; i < images.length; i++)
	{
	this.preload(images[i]);
	}
}
 
ImagePreloader.prototype.preload = function(image)
{
   // create new Image object and add to array
   var oImage = new Image;
   this.aImages.push(oImage);

   // set up event handlers for the Image object
   oImage.onload = ImagePreloader.prototype.onload;
   oImage.onerror = ImagePreloader.prototype.onerror;
   oImage.onabort = ImagePreloader.prototype.onabort;   

   // assign pointer back to this.
   oImage.oImagePreloader = this;
   oImage.Loaded = false;

   // assign the .src property of the Image object
   oImage.src = image;
} 

ImagePreloader.prototype.onComplete = function()
{
this.Processed++;
if (this.Processed == this.nImages)
	{
	this.callback(this.aImages, this.Loaded);
	}
}

ImagePreloader.prototype.onload = function()
{
   this.Loaded = true;
   this.oImagePreloader.Loaded++;
   this.oImagePreloader.onComplete();
}

ImagePreloader.prototype.onerror = function()
{
   this.bError = true;
   this.oImagePreloader.onComplete();
}

ImagePreloader.prototype.onabort = function()
{
   this.bAbort = true;
   this.oImagePreloader.onComplete();
}

function DisplayImages(LoadedImages, NumImages)
{
ImagesLoaded = true;
NumLoadedImages = NumImages;	//NumLoadedImages is global.

for (a=0; a<NumImages; a++)
	{
	FileList[a] = LoadedImages[a];	//FileLists elements are here converted from string data type containing
									//just the filename to Image object data type containing also the image size.
									
	if (LoadedImages[a].width > MaxWidth) { MaxWidth = LoadedImages[a].width; }
	if (LoadedImages[a].height > MaxHeight) { MaxHeight = LoadedImages[a].height; }
	}
	
if (NumImages==0)
	{
	getElementById_s("GalleryMain").innerHTML = "<p>Sorry, there has been a problem loading the gallery images.</p>";
	}
else
	{	
	if (NumImages==1)
		{
		ThisCode = "\n\t<div id=\"gallerynavbar\" class=\"GalleryNavigation\"></div>";
		ThisCode += "\n\t<div id=\"GalleryImageWindow\">";
		ThisCode += "\n\t\t<img src=\"" + FileList[0].src + "\"></div>";
		ThisCode += "\n\t<div id=\"gallerynavbar2\" class=\"GalleryNavigation\"></div>";
		getElementById_s("GalleryMain").innerHTML = ThisCode;
		}
	else
		{
		ThisCode = "\n\t<div id=\"gallerynavbar\" class=\"GalleryNavigation\"></div>";
		ThisCode += "\n\t<div id=\"GalleryImageWindow\">";
		ThisCode += "\n\t\t<img name=\"galleryimg\" id=\"galleryimg\" alt=\"Gallery Image\"/></div>";
		ThisCode += "\n\t<div id=\"gallerynavbar2\" class=\"GalleryNavigation\"></div>";
		getElementById_s("GalleryMain").innerHTML = ThisCode;
		DispPic(0);
		}
		
	getElementById_s("GalleryImageWindow").style.width = (MaxWidth + 15) + "px";
	getElementById_s("GalleryImageWindow").style.height = (MaxHeight + 15) + "px";
	}
}

function BeginImageFade(FadeToIndex)
{
ImageFadeTransition(FadeToIndex, false, 9);
}

function ImageFadeTransition(FadeToIndex, FadingIn, Opacity)
{
ThisPicture = getElementById_s("galleryimg");
ThisPicture.style.opacity = (Opacity/10);

if (FadingIn)
	{
	//Fading In
	Opacity+=1;
	if (Opacity!=10)
		{
		//Not finished yet
		setTimeout("ImageFadeTransition(" + FadeToIndex + ", true, " + Opacity + ");", 18);
		}
	}
else
	{
	//Fading Out
	Opacity-=1;
	if (Opacity!=0)
		{
		//Not finished fading out yet
		setTimeout("ImageFadeTransition(" + FadeToIndex + ", false, " + Opacity + ");", 18);
		}
	else
		{
		//Finished fading out. Switch image and start fading in....		
		ThisPicture.src = FileList[FadeToIndex].src;
	
		ThisPicture.style.marginTop = parseInt(((MaxHeight - FileList[FadeToIndex].height) / 2) + 5) + "px";
		ThisPicture.style.marginBottom = parseInt(((MaxHeight - FileList[FadeToIndex].height) / 2) + 5) + "px";

		ThisPicture.style.marginLeft = parseInt(((MaxWidth - FileList[FadeToIndex].width) / 2) + 5) + "px";
		ThisPicture.style.marginRight = parseInt(((MaxWidth - FileList[FadeToIndex].width) / 2) + 5) + "px";

		setTimeout("ImageFadeTransition(" + FadeToIndex + ", true, " + Opacity + ");", 18);
	
		}
	}
}

function DispPic(index)
{
var a=0;
PictureIndex = index;

BeginImageFade(index);

//ThisPicture = getElementById_s("galleryimg");
//ThisPicture.src = FileList[index].src;
//ThisPicture.style.marginTop = parseInt(((MaxHeight - FileList[index].height) / 2) + 5) + "px";
//ThisPicture.style.marginBottom = parseInt(((MaxHeight - FileList[index].height) / 2) + 5) + "px";
//ThisPicture.style.marginLeft = parseInt(((MaxWidth - FileList[index].width) / 2) + 5) + "px";
//ThisPicture.style.marginRight = parseInt(((MaxWidth - FileList[index].width) / 2) + 5) + "px";

if (index > 0)
	{
	tempstr = "<a class=\"GalleryLink\" href=\"javascript: DispPic(--PictureIndex);\">Previous</a> ";
	}
else
	{
	tempstr = "<span class=\"GalleryLink\">Previous</span> ";
	}

for (a=0; a<NumLoadedImages; a++)
	{
	if (a==index)
		{
		tempstr += "<span class=\"GalleryLink\" style=\"font-weight: bold;\">" + (a + 1) + "</span> ";		
		}
	else
		{
		tempstr += "<a class=\"GalleryLink\" href=\"javascript: DispPic(" + a + ");\">" + (a + 1) + "</a> ";
		}
	}

if (index < (NumLoadedImages - 1))
	{
	tempstr += "<a class=\"GalleryLink\" href=\"javascript: DispPic(++PictureIndex);\">Next</a>";
	}
else
	{
	tempstr += "<span class=\"GalleryLink\">Next</span>";
	}

getElementById_s("gallerynavbar").innerHTML = tempstr;
getElementById_s("gallerynavbar2").innerHTML = tempstr;
}

function GalleryOnLoadEvents()
{
setTimeout ("ScrollLoadingBar()", 100);
ImageCache = new ImagePreloader(FileList, DisplayImages);
}

function ScrollLoadingBar()
{
if (!ImagesLoaded)
	{
	BarPos += 5;
	if (BarPos>=200) BarPos=-40;
	getElementById_s("BarElement").style.left = (BarPos / 10) + "em";
	setTimeout ("ScrollLoadingBar()", 100);
	}
}
