/*
	Class:    	cvElementAppear
	Author:   	Crispijn Verkade
	Website:    http://crispijnverkade.nl
	Version:  	1.0
	Date:     	24/03/2009
	Built For:  MooTools 1.2.0
*/

var cvElementAppear = new Class({
	
	Implements: [Options,Events,Chain],

	options: {
		elements: 'img',
		endwidth: 150,
		endheight: 150,
		duration: 1000,
		transition: Fx.Transitions.Elastic.easeIn,
		onComplete: null
	},
	
	initialize: function(el,options) {
		//set options
		this.el = $(el);
		this.setOptions(options);
		
		this.items = this.el.getElements(this.options.elements);
		
		var self = this;
		
		this.items.each(function(e){
			e.setStyles({
				'width': 0,
				'height': 0,
				'margin-top': (self.options.endheight/2).round(),
				'margin-left': (self.options.endwidth/2).round()
			});
		});
		
		this.current = 0;
		this.next();
	},
		
	next: function(){
		var pos = this.items[this.current];
		
		if(this.current < this.items.length){
		
			this.fx = new Fx.Morph(pos,{
				duration: this.options.duration,
				transition: this.options.transition
			});
		
			this.fx.start({
				'width': this.options.endwidth,
				'height': this.options.endheight,
				'margin-top': 0,
				'margin-left': 0
			});

			this.current++;
			this.next.bind(this).delay(this.options.duration);
		}else{
			this.onComplete();
		}
	},
	
	onComplete: function(){
		if(this.options.onComplete != null){
			this.options.onComplete();
		}
	}
});
