if(! TMAT)
	var TMAT = { $: function(id) { return document.getElementById(id); } };
TMAT.TemploGallery = {};
TMAT.TemploGallery.gallery = Class.create(
{
	// Depends: Protoype, Scriptaculous
	initialize: function(obj)
	{
		this.img_id_prefix = obj.img_id_prefix ? obj.img_id_prefix : '';
		this.thumb_id_prefix = obj.thumb_id_prefix ? obj.thumb_id_prefix : '';
		this.effect = obj.effect ? obj.effect : 'fade';
		this.seconds = obj.seconds ? obj.seconds : 3;
		this.total_images = obj.total_images ? obj.total_images : 0;
		this.current_img_index = obj.current_img_index ? obj.current_img_index : 0;
		this.cycle_images = obj.cycle_images ? obj.cycle_images : true;
		this.hidden_id = obj.hidden_id ? obj.hidden_id : '';
		this.active = false;
		this.timer = null;
	},	
	switchToImage: function($new_id)
	{
		if(this.active || ! $(this.thumb_id_prefix+$new_id) || ! $(this.thumb_id_prefix+this.current_img_index))
			return;
		this.active = true;
		// Remove background from old thumb
		$(this.thumb_id_prefix+this.current_img_index).style.background = '';
		// Add background to new thumb
		$(this.thumb_id_prefix+$new_id).style.background = this.thumb_background;
		eval('this._'+this.effect+'('+this.current_img_index+', '+$new_id+');');
		this.current_img_index = $new_id;
		$(this.hidden_id).value = this.current_img_index+1;
	},
	next: function()
	{
		if(this.active && (this.cycle_images || this.current_img_index+1 >= this.total_images))
			return;
		this.active = true;
		var $from = this.current_img_index;
		this.current_img_index++;
		if(this.current_img_index >= this.total_images)
			this.current_img_index = 0;
		eval('this._'+this.effect+'('+$from+', '+this.current_img_index+');');
		$(this.hidden_id).value = this.current_img_index+1;
	},
	prev: function()
	{
		if(this.active && (this.cycle_images || this.current_img_index <= 0))
			return;
		this.active = true;
		var $from = this.current_img_index;
		this.current_img_index--;
		if(this.current_img_index < 0)
			this.current_img_index = this.total_images-1;
		eval('this._'+this.effect+'('+$from+', '+this.current_img_index+');');
		$(this.hidden_id).value = this.current_img_index+1;
	},
	startRotate: function()
	{
		var self = this;
		this.timer = setTimeout(function() { self._rotate(); }, this.seconds*1000);
	},
	_rotate: function()
	{
		if(this.active)
			return;
		this.active = true;
		var $next = this.current_img_index+1;
		if($next >= this.total_images)
			$next = 0;
		eval('this._'+this.effect+'('+(this.current_img_index)+', '+$next+');');
		this.current_img_index = $next;
		this.startRotate();
	},
	_fade: function($old_id, $new_id)
	{
		new Effect.Parallel([
				new Effect.Fade(this.img_id_prefix+$old_id, { sync: true, from: 1, to: 0 }),
				new Effect.Appear(this.img_id_prefix+$new_id, { sync: true, from: 0, to: 1 }),
				new Effect.Fade(this.thumb_id_prefix+$old_id, { sync: true, from: 1, to: 0.5 }),
				new Effect.Appear(this.thumb_id_prefix+$new_id, { sync: true, from: 0.5, to: 1 })
							], { duration: 1.0, gallery: this, afterFinish: function() { this.gallery.active = false; $(this.gallery.thumb_id_prefix+$new_id).className=''; } });
	}
}
);