

var imageGallery = new Array();
var imageSlots = new Array();
var galleryObjects = new Array();

var workingDom = false;
var ieDom = false;

var imageLoopInterval = -1;
var fadeLoopInterval = -1;

if( document.implementation )
{
	workingDom = true;
}
else if( document.all )
{
	ieDom = true;
}



function TransImage( imageUrl )
{
	this.imageUrl = imageUrl;
	this.isDisplayed = false;
}

function ImageSlot( image, div )
{
	this.image = image;
	this.div = div;
	this.transImageIndex = -1;
}

function ImageGallery( galleryId )
{
	this.slots = new Array();
	var galleryDiv = getImageElement( galleryId );
	potentialSlots = galleryDiv.childNodes;
	for( slotIndex in potentialSlots )
	{
		var slot = potentialSlots[ slotIndex ];
		if( slot.childNodes && slot.childNodes.length > 0 )
		{
			index = this.slots.length;
			imageSlot =  new ImageSlot( slot.childNodes[0].childNodes[0], slot.childNodes[0] );
			imageSlot.index = index;
			this.slots[ index ] = imageSlot;
			
		}
	}
}

function ImageGallery_addImage( imageUrl )
{
	this.images[ this.images.length ] = new TransImage( imageUrl );
	
	// Preload the image
	var imgPreload = new Image();
	imgPreload.src = imageUrl;
}

function ImageGallery_transitionImage( slot )
{
	var n = Math.floor(Math.random() * (this.images.length - this.gallery.slots.length));
	index=0;
	imageRef = -1;

	// Keep looping until we've chosen an image	
	while( imageRef == -1 )
	{
		// Is this image currently not displayed?
		if( this.images[ index ].isDisplayed == false )
		{
			// Have we reached the n'th image?
			if( n == 0 )
			{
				imageRef = index;
			}
			else
			{
				n--;
			}
		}
		
		index = (index + 1);
		// Shouldn't happen - we shouldn't be able to get all through
		// the array and have no hidden images
		if( index == this.images.length )
		{
			break;
		}
	}
	
	// If we've found an image to display
	if( imageRef != -1 )
	{
		// Remember the current image
		var previousImageRef = slot.transImageIndex;
		// Get the URL of the new image
		imageUrl = this.images[imageRef].imageUrl;
		
		// Mark the new image as displayed
		this.images[imageRef].isDisplayed = true;
		// Record the index of the new image
		slot.transImageIndex = imageRef;
		
		// Was there an image in the slot previously?
		if( previousImageRef != -1 )
		{
			// Mark it as no longer displayed
			this.images[previousImageRef].isDisplayed = false;

			// Copy it into the div as  background image
			slot.div.style.backgroundImage = "url('"+imageUrl+"')";
			// Set the new image as completely opaque
			slot.image.style.opacity = 1;
			// Set the fade animation going
			fadeLoopInterval = setTimeout("fadeImage("+this.index+","+slot.index+",'"+ imageUrl  +"')", 20);
		}
		else
		{
			// Just put the image in place.
			slot.image.src = imageUrl;
		}
	}
}

function ImageGallery_setGallery( galleryId )
{
	var newGallery = new ImageGallery( galleryId );

	this.gallery = newGallery;
	
	// Initialise the gallery
	for( slot in this.gallery.slots )
	{
		this.TransitionImage( this.gallery.slots[slot] );
	}
}

function ImageGallery_transitionSlot()
{
	var n = Math.floor(Math.random() * this.gallery.slots.length);
	this.TransitionImage( this.gallery.slots[n] );
}

function ImageGalleryManager()
{
	// Attributes
	this.images = new Array();
	this.gallery = null;
	
	// Methods
	this.AddImage = ImageGallery_addImage;
	this.SetGallery = ImageGallery_setGallery;
	this.TransitionImage = ImageGallery_transitionImage;
	this.TransitionSlot = ImageGallery_transitionSlot;
}


// Function: getImageElement
// Description: Wrapper function to get an element using either the DOM or
//	legacy methods.
// In:
//	id : the id of the element to fetch.
// Out: The element, or null if not found.
//
function getImageElement( id )
{
	var elem = null;
	
	if( workingDom == true )
	{
		elem = document.getElementById( id );
	}
	else if( ieDom == true )
	{
		elem = eval("document.all."+id);
	}
	else
	{
		elem = eval("document."+id);
	}
	
	return elem;	
}


// Function: fadeImage
// Description: Called by setTimeout, fades the top image out to reveal the
//	div background image, then copies the div background into the top image.
//
function fadeImage( managerIndex, slotIndex, imageUrl )
{
	var slot = galleryObjects[ managerIndex ].gallery.slots[ slotIndex ];
	var slotImage = slot.image;
	var slotDiv = slot.div;
	var opacity = slotImage.style.opacity;
	
	if( opacity < 0.1 )
	{
		slotImage.style.opacity = 0;
		slotImage.style.filter = "alpha(opacity=0)";
		
		// Workaround IE bug; IE always displays the old image before the 
		// new, causing a flicker; so set it once, pause, set it again, and
		// make it opaque.
		slotImage.src = imageUrl;
		setTimeout("setTransitionImage("+managerIndex+","+slotIndex+",'"+imageUrl+"')", 250);
	}
	else
	{
		opacity -= 0.1;
		slotImage.style.opacity = opacity;
		slotImage.style.filter = "alpha(opacity="+opacity*100+")";
		setTimeout("fadeImage("+managerIndex+","+slotIndex+",'"+ imageUrl  +"')", 20);
	}
}

function setTransitionImage(managerIndex, slotIndex, imageUrl)
{
	var slot = galleryObjects[ managerIndex ].gallery.slots[ slotIndex ];
	var slotImage = slot.image;
	slotImage.src = imageUrl;
	slotImage.style.opacity = 1;
	slotImage.style.filter = "alpha(opacity=100)";
}


function registerGalleryManager( manager )
{
	manager.index = galleryObjects.length;
	galleryObjects[ galleryObjects.length ] = manager;
}

function imageLoop( delay )
{
	for( i=0; i < galleryObjects.length; i++ )
	{
		realDelay = delay + Math.floor( Math.random() * 5 ) * 1000;
		setInterval( "galleryObjects["+i+"].TransitionSlot()",  realDelay );
	}
}
