/*
*
*	Seppukoo  Animation JavaScript Engine
*	By Les Liens Invisibles (2009)
*	
*	Requiores: 
*		- mootools-core.js	
*		- Asset class in mootools-more.js
*
*
*	Usage:
*	
*	new SeppukooAnimation(element:Element, sourceObject:Object, options:Object)
*	
*	sourceObject = [
*	
*		{
*		
*			frames: Array(frame1:String ,frame2:Object {url:String, option:Object})	
*			loop:Bool
*			onComplete:Function	//trigger the end of the sequence
*
*		}
*	
*	]
*
*/

var SeppukooAnimationFrame = new Class({
	Implements: [Events, Options],	
	
	options: {
	},
	
	initialize: function(url,options) {				
		this.setOptions(options);		
		this.url = url;		
		return this;
	},
	
	complete:function() {
		this.fireEvent('complete');
	}

});

var SeppukooAnimationSequence = new Class({

	Implements: [Events, Options],	
	
	options: {
		name:false,
		loop:false,
		frames:new Array()
	},
	
	initialize: function(options) {				
		this.setOptions(options);
		this.name = this.options.name;
		this.loop = this.options.loop;
		this.frames = this.options.frames;
	},
	
	complete:function() {
		this.fireEvent('complete');
	}
	

})
	
var SeppukooAnimation = new Class({
	Implements: [Events, Options],
	
	options: {
		url_suffix:'css/images/test',
		fps: 12,
		onPlay:function() {this.isPlaying = true;},
		onStop:function() {this.isPlaying = false;}
	},
	
	sequences:new Array(),
	isPlaying:false,
	
	initialize: function(element, source, options) {
		if ($(element)) {
			this.setOptions(options);

			this.e = $(element);
			this.period_time = 1000/this.options.fps;
			
			this.currentSequence = 0;
			this.currentFrame = 0;
			
			assets2load = new Array();
			
			source.each((function(seq,index) {
				
				this.sequences[index] = new SeppukooAnimationSequence({
											name:(($defined(seq.name)) ? seq.name : null ),
											loop:(($defined(seq.loop)) ? seq.loop : false),
											onComplete:(($defined(seq.onComplete)) ? seq.onComplete : $empty),
											frames:new Array()
										});
				
				seq.frames.each((function(f,frameIndex) {
				
					if ($type(f) == 'string') {
						this.sequences[index].frames[frameIndex] = new SeppukooAnimationFrame(f);
						assets2load.include(this.options.url_suffix+'/'+f);
						
					} else {
						this.sequences[index].frames[frameIndex] = new SeppukooAnimationFrame(f.url,f.options);
						assets2load.include(this.options.url_suffix+'/'+f.url);
					}
					
					
				}).bind(this));
			}).bind(this))
			
			new Asset.images(assets2load,{
				onComplete:(function() {
					this.fireEvent('load')
				}).bind(this)
			});
			
		}
	},
	
	getCurrentSequence: function() {
		return this.sequences[this.currentSequence]
	},

	setCurrentSequence: function(num) {
		if (num<=this.sequences.length-1) {
			this.currentSequence = num; 
			return true;
		} else {
			return false;
		}
		
	},


	getCurrentFrame: function() {
		return this.sequences[this.currentSequence].frames[this.currentFrame]
	},
	
	setNewImage:function() {
		this.e.set('src',this.options.url_suffix+"/"+this.getCurrentFrame().url);
		this.getCurrentFrame().complete();
	},
	anim: function() {
		
		if (this.currentFrame<this.getCurrentSequence().frames.length-1) {
			this.currentFrame +=1;
			this.setNewImage();
			if (this.currentFrame == this.getCurrentSequence().frames.length-1) this.getCurrentSequence().complete();
		} else {
			this.currentFrame = 0;
			if (this.currentSequence < this.sequences.length-1) {
				if (!this.sequences[this.currentSequence].loop) this.currentSequence +=1;				
				this.setNewImage();
			} else {
				if (!this.sequences[this.currentSequence].loop) this.stop();
			}
		}
		
		
	},
	breakLoop: function(seq) {
		if (!$defined(seq)) seq = [this.currentSequence];
		if (($type(seq) == 'string') || ($type(seq) == 'number')) seq = [seq];

		
		seq.each((function(s) {
			if (this.sequences[s].loop) this.sequences[s].loop = false;		
		}).bind(this));
	},
	
	
	gotoAndPlay:function(seq) {
	
		if (this.setCurrentSequence(seq)) {
			this.currentFrame = 0;
			if (!this.isPlaying) this.play();
		}
	
	},
	
	play: function() {
		if (!this.isPlaying) {
			this.animation = this.anim.periodical(this.period_time,this);
			this.fireEvent('play');
		} else this.stop();
	},
	stop:function() {
		$clear(this.animation);
		this.fireEvent('stop');
	}
	
})


