var Chains = new Class({      
	Implements: [Chain, Events, Log],
			
	elements: new Hash({}),
	
	effects: [],
	
	complete: function(el){
		if(!this.completed) {
			this.completed = true;
			this.fireEvent("chainready",el);
		}		
    },
	
	isready: function() {
		return this.completed;
	},
		
	initialize: function(elements)
	{
		this.completed = false;
		this.elements = elements;
		
		this.elements.each(function(el){
			el.elements.each(function(item){
				var settings = new Object({});
				
				/* check for title JSON settings */
				if(item.getProperty('title')) {
					settings = JSON.decode(item.getProperty('title'));
					item.removeProperty('title');
				}
				
				/* setup styles */
				if(settings.styles) {
					
					var default_styles = new Object({
						opacity: 0
					});
					
					var styles_obj = $merge(default_styles, settings.styles);
					
					item.store('styles', styles_obj);
					item.setStyles(styles_obj);
					
				}

				/* fx from attribute */
				if(settings.fx) {
					var default_fx = new Object({
						duration: 'normal',
						transition: 'quint:in:out',
						onComplete: function() {
							if(!this.callChain()) {
								this.complete();
							}
						}.bind(this)
					});
					
					var fx_obj = $merge(default_fx, settings.fx);
					
					var item_fx = new Fx.Morph(item, fx_obj);
					item.store('fx', item_fx);
				}
				/* morph from attribute */
				if(settings.morph) {
					var default_morph = new Object({
						opacity: 0
					});
					
					var morph_obj = $merge(default_morph, settings.morph);
					
					item.store('morph', morph_obj);
				}
				
				
				/* slide from attribute */
				if(settings.slide) {
					var default_slide = new Object({
						mode: 'vertical',
						transition: 'quad:in:out',
						duration: 'normal',
						onComplete: function() {
							
							if(!this.callChain()) {
								this.complete();
							}
							
						}.bind(this)
					});
					
					var slide_obj = $merge(default_slide, settings.slide);
					
					var item_slide = new Fx.Slide(item, slide_obj).hide().show().hide();
					item.store('slide', item_slide);
										
				}
			}, this);
			
		}, this);
	},
	
	stop: function() {
		this.clearChain();
	},
	
	pauze: function(parent, item, index, array) {
		this.log('pauze::');
		this.log('item: '+item);
		this.log('index: '+index);
		this.log('array: '+array);
		(function(){
			this.reinit();
		}.bind(this)).delay(10000);
	},
	
	reinit: function() {
		this.callChain();
	},
	
	startup: function() {
		/*
		function () {
			if(parent.limit != undefined && parent.limit > 1) {
				if(iteration % parent.limit == 0) {
					this.pauze(parent, item, index, array);
				}else {
					this.callChain();
				}
				
			}else {
				this.callChain();
			}
		},					
					
		*/
		
		this.elements.each(function(parent){
			parent.elements.each(function(item, index, array){
				var iteration = (index + 1);
				
				this.chain(
					function () {
						
						if((item.retrieve('fx')) && (item.retrieve('morph'))) {
							item.retrieve('fx').start(item.retrieve('morph'));
						}else {
							this.callChain();
						}
						return true;
					},
					function () {
						if(item.retrieve('slide')) {
							item.retrieve('slide').slideIn();
						}else {
							this.callChain();
						}
						return true;
					},
					
					function () {
						if((!item.retrieve('fx')) && (!item.retrieve('morph')) && (!item.retrieve('slide'))) {
							if(!this.callChain()) {
								this.complete();
							}
						}else {
							this.callChain();
						}
						return true;
					},
					function () {
						
						if(item.retrieve('startup')) {
							item.retrieve('startup').startup();
						}
						if(!this.callChain()) {
							this.complete(item);
							
						}
						
					}
				)
			}, this);
		}, this);
		this.callChain();
	},
	
	cancel: function() {
		alert('cancel');
	}
});