if(typeof(TMAT) == "undefined")
	var TMAT = { $:function(id) { return document.getElementById(id); } };
// Rotator Class
TMAT.Rotator = Class.create(
	{
		// Depends: Protoype, Scriptaculous
		// Assume you start in the first div
		initialize: function(obj)
		{
			obj = obj ? obj : {};
			this.img_id_prefix = typeof(obj.img_id_prefix) != "undefined" ? obj.img_id_prefix : '';
			this.total = typeof(obj.total) != "undefined" ? obj.total : 1;
			this.div_num = typeof(obj.div_num) != "undefined" ? obj.div_num : 0;
			this.cycle = typeof(obj.cycle) != "undefined" ? obj.cycle : true;
			this.effect = typeof(obj.effect) != "undefined" ? obj.effect : 'fade';
			this.duration = typeof(obj.duration) != "undefined" ? obj.duration : 1.0;
			this.seconds = typeof(obj.seconds) != "undefined" ? obj.seconds : 1;
			this.width = typeof(obj.width) != "undefined" ? obj.width : 800;
			this.height = typeof(obj.height) != "undefined" ? obj.height : 600;
			this.onChange = typeof(obj.onChange) != "undefined" ? obj.onChange : null;
			this.timer = null;
			this.rotateState = null;
			this.active = false;
		},
		next: function()
		{
			if(this.active || (!this.cycle && this.div_num+1 >= this.total))
				return false;
			clearTimeout(this.timer);
			var $from = this.div_num;
			var $to = this.div_num+1 >= this.total ? 0 : this.div_num+1;
			this.active = true;
			this._onChange($to);
			eval('this._'+this.effect+'('+$from+','+$to+');');
		},
		prev: function()
		{
			if(this.active || (!this.cycle && this.div_num-1 < 0))
				return false;
			clearTimeout(this.timer);
			var $from = this.div_num;
			var $to = this.div_num-1 < 0 ? this.total-1 : this.div_num-1;
			this.active = true;
			this._onChange($to);
			eval('this._'+this._getPrevEffect()+'('+$from+','+$to+');');
		},
		switchTo: function($to)
		{
			if(this.active || $to >= this.total || $to < 0 || this.div_num == $to)
				return false;
			clearTimeout(this.timer);
			var $from = this.div_num;
			this.active = true;
			this._onChange($to);
			var $effect = ($to < $from ? this._getPrevEffect() : this.effect);
			eval('this._'+$effect+'('+$from+','+$to+');');
		},
		rotateForward: function()
		{
			this.rotateState = 'rotateForward';
			that = this;
			this.timer = setTimeout("that.next();", this.seconds*1000);
		},
		_getPrevEffect: function()
		{
			switch(this.effect)
			{
				case 'fade':
					return 'fade';
				case 'random':
					return 'random';
				case 'slideLeft':
					return 'slideRight';
				case 'slideRight':
					return 'slideLeft';
				case 'slideUp':
					return 'slideDown';
				case 'slideDown':
					return 'slideUp';
			}
		},
		_onChange: function($to)
		{
			if(this.onChange)
				eval(this.onChange+'('+$to+');');
		},
		_fade: function($from,$to)
		{
			this.div_num = $to;
			$(this.img_id_prefix+$to).style.display='none';
			$(this.img_id_prefix+$to).style.left='0px';
			$(this.img_id_prefix+$to).style.top='0px';
			new Effect.Parallel([ 
							   new Effect.Fade(this.img_id_prefix+$from, { sync: true }),
							   new Effect.Appear(this.img_id_prefix+$to, { sync: true })
								 ], { duration: this.duration, transition:  Effect.Transitions.sinoidal, that: this, afterFinish: function() { this.that.active = false; if(this.that.rotateState) eval('this.that.'+this.that.rotateState+'();'); } });
		},
		_slideLeft: function($from,$to)
		{
			this.div_num = $to;
			$(this.img_id_prefix+$to).style.left=this.width+'px';
			$(this.img_id_prefix+$to).style.top='0px';
			$(this.img_id_prefix+$to).style.display='';
			new Effect.Parallel([ 
							   new Effect.Move(this.img_id_prefix+$from, { x: -this.width, sync: true }),
							   new Effect.Move(this.img_id_prefix+$to, { x: -this.width, sync: true })
								 ], { duration: this.duration, transition:  Effect.Transitions.sinoidal, that: this, afterFinish: function() { this.that.active = false; if(this.that.rotateState) eval('this.that.'+this.that.rotateState+'();'); } });
		},
		_slideRight: function($from,$to)
		{
			this.div_num = $to;
			$(this.img_id_prefix+$to).style.left=-this.width+'px';
			$(this.img_id_prefix+$to).style.top='0px';
			$(this.img_id_prefix+$to).style.display='';
			new Effect.Parallel([ 
							   new Effect.Move(this.img_id_prefix+$from, { x: this.width, sync: true }),
							   new Effect.Move(this.img_id_prefix+$to, { x: this.width, sync: true })
								 ], { duration: this.duration, transition:  Effect.Transitions.sinoidal, that: this, afterFinish: function() { this.that.active = false; if(this.that.rotateState) eval('this.that.'+this.that.rotateState+'();'); } });
		},
		_slideUp: function($from,$to)
		{
			this.div_num = $to;
			$(this.img_id_prefix+$to).style.top=this.height+'px';
			$(this.img_id_prefix+$to).style.left='0px';
			$(this.img_id_prefix+$to).style.display='';
			new Effect.Parallel([ 
							   new Effect.Move(this.img_id_prefix+$from, { y: -this.height, sync: true }),
							   new Effect.Move(this.img_id_prefix+$to, { y: -this.height, sync: true })
								 ], { duration: this.duration, transition:  Effect.Transitions.sinoidal, that: this, afterFinish: function() { this.that.active = false; if(this.that.rotateState) eval('this.that.'+this.that.rotateState+'();'); } });
		},
		_slideDown: function($from,$to)
		{
			this.div_num = $to;
			$(this.img_id_prefix+$to).style.top=-this.height+'px';
			$(this.img_id_prefix+$to).style.left='0px';
			$(this.img_id_prefix+$to).style.display='';
			new Effect.Parallel([ 
							   new Effect.Move(this.img_id_prefix+$from, { y: this.height, sync: true }),
							   new Effect.Move(this.img_id_prefix+$to, { y: this.height, sync: true })
								 ], { duration: this.duration, transition:  Effect.Transitions.sinoidal, that: this, afterFinish: function() { this.that.active = false; if(this.that.rotateState) eval('this.that.'+this.that.rotateState+'();'); } });
		},
		_random: function($from,$to)
		{
			var $total_effects = 3;
			var $rand = Math.floor(Math.random()*$total_effects);
			
			switch($rand)
			{
				case 0:
					return this._fade($from,$to);
				case 1:
					return this._slideLeft($from,$to);
				case 2:
					return this._slideUp($from,$to);
			}
		}
	}
);
