/**
 * japing
 * jquery animated png
 * version 0.2 beta
 * author geiregat jonas
 */
(function($){
	$.fn.japing = function(customOptions) {
	
		// init options
		// ============
		var options = {
			id: "japing",
			className: "japing",
			width: -1,
			height: -1,
			frames: -1,
			speed: 50
		};
		
		var japings = [];
		var currentFrame = -1;
		var interval;
		
		if (customOptions) $.extend(options, customOptions);
		
		
		// private methods
		// ===============
		var update = function() {
			for (var i = 0; i < japings.length; i++)
			{
				japings[i].css({
					'background-position': -1 * currentFrame * options.width
				});
			}
			
			currentFrame = ((currentFrame + 1) == options.frames) ? 0 : currentFrame + 1;
		}
				
		var create = function(parent) {
			// create element
			var japing = $('<div />');
			japing.attr({
				'id': options.id,
				'class': options.className
			});
			
			// set width and height
			japing.width(options.width);
			japing.height(options.height);
			
			// set css
			japing.css({
				'background': 'url(' + options.spritePath + ') no-repeat'
			});
			
			parent.append(japing);			
			japings.push(japing);
		}
		
		
		// public methods
		// ==============
		this.destroy = function() {
			clearInterval(interval);
			for (var i = 0; i < japings.length; i++) {
				$(japings[i]).remove();			
			}						
		}
		
		this.each(function() {
			if (this != null) {
				var $this = $(this);
				create($this);
			}			
		});
		
		
		// init
		// ====
		currentFrame = 0;
		interval = setInterval(update, options.speed);
		
		return this;
	}
})(jQuery);
