function DOMCall(name) {
	if (document.layers) 
		return document.layers[name];
	else if (document.all)
		return document.all[name];
	else if (document.getElementById)
		return document.getElementById(name);
}

function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

// CSS GALLERY - OO-Style
// UA Jan 2008

// Contructor
function gallery(css_container,css_placeholder,css_caption)
{
	// Fields
	this.container = css_container;
	this.placeholder = css_placeholder;
	this.caption = css_caption;
	this.currentPos = 0;
	this.currentKey = 0;
	this.maxheight = 0;
	// Functions
	this.galleryInit = galleryInit;
	this.printStuff = printStuff;
	this.nextImage = nextImage;
	this.lastImage = lastImage;
	this.showPic = showPic;
	this.switchThumbs = switchThumbs;
	// always return true
	return true;
}

// Initiate the gallery
function galleryInit()
{
	// Get the pictures
	var d=DOMCall(this.container);
	if(d) {
		this.pics=d.getElementsByTagName('a');
		this.showPic(this.pics[2]);
		} else alert('Error: Container not found...');

	return true;
}

// Print something -- used for testing purposes
function printStuff() {

	for(var i=0;i<this.pics.length;i++){
		document.writeln(this.pics[i]+'\n');
		}

	return true;
}

// Show a given image
function showPic(whichpic) {
	
	// Fix page height
	var picheight1 = document.getElementById(this.placeholder).clientHeight;
	if(picheight1>this.maxheight) this.maxheight = picheight1;
	
	DOMCall(this.placeholder).src = whichpic.href;
	if (whichpic.title) {
		DOMCall(this.caption).innerHTML = whichpic.title;
		DOMCall(this.caption).style.display = "";
	} else {
		DOMCall(this.caption).style.display = "none";
	}

	// Update the thumbssection
	this.switchThumbs(whichpic);

	// Fix page height
	// Calls setNewHeight - if nothing is be done please remove content of this function
	var picheight2 = document.getElementById(this.placeholder).clientHeight;
	var contentheight = document.getElementById('content').clientHeight;
	
	if(picheight2>this.maxheight) {
		var newHeight = contentheight+(picheight2-this.maxheight);
		document.getElementById('content').style.height =  newHeight + 'px';
	}

 	return false;
}

// Jump to the next image
function nextImage() 
{
	if(this.pics[this.currentKey+1].href != ''){
		this.showPic(this.pics[this.currentKey+1]);
		}
		else this.showPic(this.pics[2]);

	return false;
}

// Jump to the preceeding image
function lastImage() 
{
	if(this.pics[this.currentKey-1].href != ''){
		this.showPic(this.pics[this.currentKey-1]);
		}
		else this.showPic(this.pics[this.pics.length-3]);

	return false;
}

// Update the style on thumbs
function switchThumbs(whichpic) {
	for(var i=0;i<this.pics.length;i++)
	{
		if(whichpic==this.pics[i]){
			this.pics[i].parentNode.style.background="gray";
			this.currentKey=i;
		}
		else this.pics[i].parentNode.style.background="";
	}

	for(var i=0;i<this.pics.length;i++)
	{
		if(i<(this.currentKey-2) || i>(this.currentKey+2)) this.pics[i].parentNode.style.display="none";
		else {
			this.pics[i].parentNode.style.display="";
			// preload next 2 images
			if(this.pics[i].href != '') newImage(this.pics[i].href);
		}
	}

}

/* Slideshow stuff */
function startSlideshow() {
	var t=setTimeout("doSlide();",3000);
}
function doSlide() {
	nextImage();
	var t=setTimeout("doSlide();",3000);
}