

function tryGalleryInit()
{
	if (!document.getElementById("footer"))
	{
		setTimeout("tryGalleryInit()",50)
	}
	else
	{
		initGallery();
	}
}
setTimeout("tryGalleryInit()",0)

var galleryLargeImages
var galleryThumbnails = []

var galleryViewport
var galleryContainer

function initGallery()
{

	galleryContainer = document.getElementById("gallery")	
	galleryViewport = document.getElementById("images")
	if (galleryViewport)
	{
		galleryLargeImages = galleryViewport.getElementsByTagName("IMG")
		
		galleryViewport.style.height = GALLERY_IMAGE_HEIGHT + "px"
		galleryViewport.style.width = GALLERY_IMAGE_WIDTH + "px"

		initGalleryThumbs();
		showImage(0);
	}
}


function initGalleryThumbs()
{

	// Build Thumbs
	var newUL = document.createElement("UL")
	var newLI
	newUL.id = "thumbnails"
	for (i=0; i<galleryLargeImages.length; i++)
	{
		// Create The LI Tag
		newLI = document.createElement("LI")

		// Create The Thumbnail
		newImg = document.createElement("IMG")
		newImg.src = galleryLargeImages[i].src.replace( /&h=\d+&w=\d+/,"&h=" + GALLERY_THUMBNAIL_HEIGHT + "&w=" + GALLERY_THUMBNAIL_WIDTH )
		newImg.alt = galleryLargeImages[i].alt
		newLI.index = i
		newLI.onclick = changeImage

		galleryThumbnails.push(newImg)

		// Append IMG to LI
		newLI.appendChild(newImg)	
		
		// Append LI to UL
		newUL.appendChild(newLI)
	}
	
	// Append to Page
	galleryContainer.appendChild(newUL)
}


function changeImage()
{
 
	showImage(this.index)
}


function showImage( index )
{	
	
	for (var c=0; c<galleryLargeImages.length;c++)
	{
		galleryLargeImages[c].style.display="none";
		galleryThumbnails[c].parentNode.className=""
	
	}
	
	var image = galleryLargeImages[index]
	//opera.postError( image.src )
	if (image.complete)
	{	
		//alert("hello")
		galleryThumbnails[index].parentNode.className="active"
		image.style.display = "block";
		image.style.marginTop = (parseInt(galleryViewport.style.height)/2 - image.height/2) + "px"
		image.style.marginLeft = (parseInt(galleryViewport.style.width)/2 - image.width/2) + "px"
	}
	else
	{
		setTimeout("showImage(" + index + ")",500)	
	}
	
	
}



