/*
Script: Awf.pngAnimation.js
	Animate your png images

License:
	MIT-style license.

Authors:
	Arian Stolwijk
*/

var Awf = $merge({
	pngAnimation: new Class({
	    version: 1.1,
		
		Implements: Options, 
		
		options: {
			width: 16,
			height: 16,
			rows: 1,
			cols: 7,
			fps: 24
		},
		
		spinner: null,
		col: 1,
		row: 1,
		
		initialize: function(url,options){
			if(!url){
				return false;
			}
			this.setOptions(options);
			
			this.spinner = new Element('div',{
				'styles': {
					width: this.options.width,
					height: this.options.height,
					'background-image': 'url('+url+')',
					'background-position': 'top left'
				},
				text:''
			});
			this.start();
		},
		
		start:function(){
			this.timer = this.animate.periodical(1000/this.options.fps,this);
		},
		
		stop: function(){
			$clear(this.timer);
		},
		
		animate: function(){
			this.spinner.setStyle(
				'background-position',
				(-(this.col-1)*this.options.width)+'px '+(-(this.row-1)*this.options.height)+'px'
			);
	 
			this.col++;			 
			if(this.col > this.options.cols){
				this.row++;
				if(this.row > this.options.rows){
					this.row = 1;
				}
				this.col = 1;
			}
		},
		
		toElement: function(){
			return this.spinner;
		}
	})
}, Awf || {});

