var thumbs = [];

window.onload = function () {
	var body = document.getElementsByTagName ('body')[0];
	
	for (var i = 0; i < galleryList.length; i++) {
		thumbs[i] = new Thumbnail (galleryList[i].fileName, galleryList[i].caption);
		var div = thumbs[i].div;
		div.style.left = (96 * i) + 'px';
		body.appendChild (thumbs[i].div);
	}
	
	Thumbnail.imageViewer = parent.document.getElementById ('picture-element');
	Thumbnail.captionViewer = parent.document.getElementById ('picture-caption');
	
	thumbs[Math.round ((thumbs.length - 1) * Math.random ())].view ();
}

function Thumbnail (fileName, caption) {
	this.fileName = fileName;
	this.caption = caption? caption: fileName;
	var o = this;
	
	this.div = document.createElement ('div');
	this.div.className = 'thumbnail';
	this.div.style.position = 'absolute';
	
	this.img = document.createElement ('img');
	this.img.style.border = '0';
	this.img.src = cmsUrlRoot + 'dynamic_img/gallery_thumbnail.php?fileName=' + encodeURIComponent (this.fileName);
	this.img.alt = caption;
	this.img.width = 88;
	this.img.height = 66;
	this.img.style.cursor = 'pointer';
	
	var onclick = function (e) {
		return o._onclick (this, e);
	}
	if (this.img.addEventListener) {
		this.img.addEventListener ('click', onclick, false);
	}
	else {
		this.img.onclick = onclick;
	}
	
	this.div.appendChild (this.img);
}

Thumbnail.prototype._onclick = function (target, e) {
	if (!e) e = window.event;
	
	this.view ();
}

Thumbnail.prototype.view = function () {
	Thumbnail.imageViewer.src = cmsUrlRoot + 'dynamic_img/gallery_view.php?fileName=' + encodeURIComponent (this.fileName);
	Thumbnail.imageViewer.alt = this.caption;
	Thumbnail.captionViewer.innerHTML = this.caption;
}
	
// Thanks ppk (quirksmode.org)

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

