
var mooSlideShow = new Class({
	options: {
		waiting: 3000,
		durationImg: 500,
		durationTxt: 500,
		startIndex: 0,
		Items: [],
		paObj: null,
		textAnim: false
	},

	Objs:[],
	current: 0,
	timer: false,
	Implements: [Options,Events],
	action: 0,

	initialize: function(pObj, options) {
		this.setOptions(options);
		if($type(pObj) != 'element') pObj = $(pObj);

		this.options.paObj = pObj;
		this.options.Items = pObj.getElements(".item");
		if(this.options.Items.length < 2) return;

		this.createStructur(this.options.Items);
		this.timer = (function(){this.SlideShow()}.bind(this)).delay(this.options.waiting);
	},

	createStructur:function(items) {
		this.lnkBody = new Element('div.mooSlideShowPaging');
		items.fade('hide');
		items.each( function(item, id) {
			item.setStyle('display','block');
			this.Objs[id] = {
				imgObj: item,
				lnkObj: new Element('a.mooSlideShowPagingItem', {'data-indent': '{item:'+id+'}'}).inject(this.lnkBody),
				txtObj: null,
				objEff: new Fx.Tween(item, {link:'cancel',duration: this.options.durationImg, transition: Fx.Transitions.Sine.easeOut, property: 'opacity'}),
				txtEff: null
			};

			if(item.getElement('.caption') && this.options.textAnim) {
				this.Objs[id].txtObj = item.getElement('.caption').fade('hide');
				this.Objs[id].txtEff = new Fx.Morph(this.Objs[id].txtObj, {link:'cancel',duration: this.options.durationTxt, transition: Fx.Transitions.Sine.easeOut});
			}
		}, this);

		this.options.paObj.adopt(this.lnkBody);
		//this.lnkBody.setStyles({'left': "50%", 'marginLeft': "-"+(this.lnkBody.getWidth()/2)+"px"})
		this.lnkBody.getElements('a.mooSlideShowPagingItem').each( function(item, id) {
			item.addEvent('click', function(event){ event.stop(); this.Toggle(item)}.bind(this) );
		}, this);

		(function(){this.FadeIn(this.Objs[0]);}.bind(this)).delay(0);
		this.current = 0;
		return;
	},

	SlideShow: function() {
		var newItem = this.current + 1;
		if(newItem >= this.Objs.length) newItem = 0;
		this.Toggle(this.Objs[newItem].lnkObj);
	},

	Toggle:function(node) {
		$clear(this.timer);
		var val = JSON.decode(node.get('data-indent'));
		if(val.item == this.current || this.action==1) return;
		this.action = 1;

		this.FadeOut(this.Objs[this.current], this.Objs[val.item]);
		this.FadeIn(this.Objs[val.item]);

		this.timer = (function(){this.SlideShow()}.bind(this)).delay(this.options.waiting);
		this.current = val.item;
	},

	FadeOut: function(nodeOld, nodeNew) {
		if(nodeOld.txtObj && this.options.textAnim) nodeOld.txtObj.fade('out');
		nodeOld.objEff.start(0);
		nodeOld.lnkObj.toggleClass('activ');

	},

	FadeIn: function(node) {
		node.objEff.chain(function(){
			if(node.txtObj && this.options.textAnim) this.FadeInText(node);
			this.action = 0;
		}.bind(this));
		node.objEff.start(1);
		node.lnkObj.toggleClass('activ');
	},

	FadeInText: function(node) {
		var cTop = node.txtObj.getStyle('top').toInt();
		node.txtObj.setStyle('top', (cTop+20) +'px');
		node.txtEff.start({'top':cTop, 'opacity':1});
	}
});

