var Actus = new Class ({
	Implements: Options,
	size:{
	},
	elements:null,
	container:null,
	active:null,
	transitionTime:1,
	displayTime:1,
	initialize:function(options)
	{
		this.setOptions(options);
		var that = this;
		this.c = options.container;
		this.c.addClass('scripted');
		this.e = options.elements;
		this.imgClass=options.imgClass;
		this.descClass=options.descClass;
		this.e.each(function(el,index)
		{
			
			var img = el.getChildren('.img')[0];
			var desc = el.getChildren('.actu-text')[0];
			var bg = el.getChildren('.actu-text-bg')[0];
			(el.getChildren('.actu-text')[0].getChildren('h2')[0].get('text').clean() =='' && el.getChildren('.actu-text')[0].getChildren('.actu-desc')[0].get('text').clean() =='') ? showDesc = false : showDesc = true ;//desc = bg = false;
			(el.hasClass('nodesc') && showDesc == true) ? showDesc = false : showDesc = true ;
			el.stuff = { img:img, desc:desc, bg:bg, showDesc:showDesc };
			img.set('opacity',0);
			el.setStyle('display','none');
			img.setStyle('display','none');
			desc.setStyle('bottom',-100);
			bg.setStyle('bottom',-100);
		});
		this.change(0);
		if(this.e.length >1)
		{
			this.timer = this.autoChange.periodical(7000, this);
		}
	},
	autoChange:function()
	{
		//alert('oi ' + this.active);
		if(this.active==this.e.length-1)
		{
			this.change(0);
		}else{
			this.change(this.active+1);
		}
	},
	change:function(id)
	{
		var that = this;
		if(!this.stuck)
		{
			this.stuck = true;
			var newPic = this.e[id].stuff.img;
			var newDesc = this.e[id].stuff.desc;
			var newBg = this.e[id].stuff.bg;
			var showDesc = this.e[id].stuff.showDesc;
			that.e[id].setStyle('display','block');
			newPic.setStyle('display','block');
			if(this.active!= null)
			{
				var oldPic = this.e[this.active].stuff.img;
				var oldDesc = this.e[this.active].stuff.desc;
				var oldBg = this.e[this.active].stuff.bg;
				oldBg.tween('bottom', -100);
				if(showDesc)
				{
					var oldTw = new Fx.Tween(oldDesc).start('bottom', -100).chain(function()
					{
						oldPic.tween('opacity', 0);
						new Fx.Tween(newPic).start('opacity', 1).chain(function()
						{
							newBg.tween('bottom', 0);
							var twb = new Fx.Tween(newDesc).start('bottom', 0).chain(function()
							{
								if(that.active!= null){
									that.e[that.active].setStyle('display','none');
								}
								that.active = id;
								that.stuck = false;
							});
						});
					});
				}else{
					var oldTw = new Fx.Tween(oldDesc).start('bottom', -100).chain(function()
					{
						oldPic.tween('opacity', 0);
						new Fx.Tween(newPic).start('opacity', 1).chain(function()
						{
							//newBg.tween('bottom', 0);
							if(that.active!= null){
								that.e[that.active].setStyle('display','none');
							}
							that.active = id;
							that.stuck = false;
							/*
							var twb = new Fx.Tween(newDesc).start('bottom', 0).chain(function()
							{
							});
							*/
						});
					});
				}
			}else{
				newPic.tween('opacity', 1);
				if(showDesc)
				{
					var tw = new Fx.Tween(newBg).start('bottom', 0);
					var twb = new Fx.Tween(newDesc).start('bottom', 0).chain(function()
					{
						that.active = id;
						that.stuck = false;
					});
				}else{
					that.active = id;
					that.stuck = false;
				}
			}
		}
	}
});
var Change = new Class ({
	Implements: Options,
	size:{
	},
	elements:null,
	container:null,
	active:null,
	displayTime:5000,
	direction:'fade',
	initialize:function(options)
	{
		this.setOptions(options);
		var that = this;
		this.c = options.container;
		this.c.addClass('scripted');
		this.e = options.elements;
		if(options.displayTime) this.displayTime = options.displayTime;
		if(options.direction) this.direction = options.direction;
		if(options.distance)  this.distance = options.distance;
		if(this.e.length <1) return false;
		this.e.each(function(el,index)
		{
			switch(that.direction)
			{
				case 'fade' :
					el.set('opacity',0);
					el.setStyle('display','none');
					break;
				case 'left' :
				case 'right' :
				case 'top' :
				case 'bottom' :
					el.setStyle(that.direction, -that.distance);
					break;
			}
		});
		this.change(0);
		if(this.e.length >1)
		{
			this.timer = this.autoChange.periodical(this.displayTime, this);
		}
	},
	autoChange:function()
	{
		//alert('oi ' + this.active);
		if(this.active==this.e.length-1)
		{
			this.change(0);
		}else{
			this.change(this.active+1);
		}
	},
	change:function(id)
	{
		var that = this;
		if(!this.stuck)
		{
			this.stuck = true;
			var newPic = this.e[id];
			newPic.setStyle('display','block');
			if(this.active!= null)
			{
				var oldPic = this.e[this.active];
				var oldPicFx = new Fx.Tween(oldPic);
				var newPicFx = new Fx.Tween(newPic);
				switch(this.direction)
				{
					case 'fade' :
						newPicFx.start('opacity', 1);
						oldPicFx.start('opacity', 0).chain(function()
						{
							oldPic.setStyle('display','none');
							that.active = id;
							that.stuck = false;
						});
						break;
					case 'left' :
					case 'right' :
					case 'top' :
					case 'bottom' :
						newPicFx.start(this.direction, 0);
						oldPicFx.start(this.direction, this.distance).chain(function()
						{
							oldPic.setStyle(that.direction,-that.distance);
							that.active = id;
							that.stuck = false;
						});
						break;
				}
			}else{
				var newPic = this.e[id];
				var newPicFx = new Fx.Tween(newPic);
				switch(this.direction)
				{
					case 'fade' :
						newPicFx.start('opacity', 1).chain(function()
						{
							that.active = id;
							that.stuck = false;
						});
						break;
					case 'left' :
					case 'right' :
					case 'top' :
					case 'bottom' :
						newPicFx.start(this.direction, 0).chain(function()
						{
							that.active = id;
							that.stuck = false;
						});
						break;
				}
			}
		}
	}
});