var gallery;

function galleryLoadCheck() {
	if(gallery.imageLoaded(gallery.next_full))
		gallery.completeSetImg();
	else
		setTimeout('galleryLoadCheck()',300);
	return;
}

function Gallery(active_full,active_thumb,overlay_id,fade_time,body_id,main_img_class) {
	this.active_full = active_full;
	this.active_thumb = active_thumb;
	this.full_images = new Hash();
	this.working = false;
	this.overlay_id = overlay_id;
	this.next_src = false;
	this.next_thumb = false;
	this.next_full = false;
	this.fade_time = fade_time;
	this.body_id = body_id;
	this.main_img_class = main_img_class;
}
Gallery.prototype.thumbOver = function(link_id) {
	var children = $(link_id).immediateDescendants();
	children[0].setStyle({borderColor: '#004c27'});
}
Gallery.prototype.thumbOff = function(link_id) {
	if(link_id == this.active_thumb)
		return;
	var children = $(link_id).immediateDescendants();
	children[0].setStyle({borderColor: '#7a887a'});
}
Gallery.prototype.imageLoaded = function(img_id) {
	return this.full_images[img_id].complete;
}
Gallery.prototype.preloadImage = function(img_id,img_src) {
	this.full_images[img_id] = new Image();
	this.full_images[img_id].src = img_src;
}
Gallery.prototype.completeSetImg = function() {
	var fade_time_secs = this.fade_time / 500;
	new Effect.Parallel(
		[
			new Effect.Opacity(this.active_full,{sync: true, from: 1.0, to: 0.00001}),
			new Effect.Opacity(this.next_full,{sync: true, from: 0.00001, to: 1.0})
		],
		{duration: fade_time_secs});
	this.active_full = this.next_full;
	var old_active_thumb = this.active_thumb;
	this.active_thumb = this.next_thumb;
	this.thumbOff(old_active_thumb);
	this.thumbOver(this.active_thumb);
	this.working = false;
}
Gallery.prototype.setImg = function(new_src,new_thumb,new_full) {
	if(this.working)
		return;
	this.working = true;
	this.next_src = new_src;
	this.next_full = new_full;
	this.next_thumb = new_thumb;
	if(!this.imageLoaded(new_full)) {
		setTimeout('galleryLoadCheck()',200);
		return;
	} else
		this.completeSetImg();
}
Gallery.prototype.preloadFullsize = function() {
	var elems = $(this.body_id).getElementsByClassName(this.main_img_class);
	for(var i = 0; i < elems.length; i++)
		this.preloadImage(elems[i].id,elems[i].src);
}
gallery = new Gallery('full_img_0','thumbnail_0','overlay',700,'page_body','main_img');