(function($)
{
  	var methods =
  	{
    	init : function(options, callback)
    	{
			return this.each(function()
			{
				opts = $.extend({}, {
				'index': 0,
				'loop' : false,
				'background': null,
				'width' : 200,
				'timeTable' : [[100,1]]
				}, options);
				$.data(this, 'index', opts['index']);
				$.data(this, 'loop', opts['loop']);
				$.data(this, 'width', opts['width']);
				$.data(this, 'timeTable', opts['timeTable']);
				$.data(this, 'time', 1);
				$(this).css('background',opts['background']+opts['timeTable'][opts['index']][1]*opts['width']+'px 0px');
				methods.clearAnimation(this);
				methods.interval(this,callback);
			});
		},
    	play : function(options,callback)
		{
			return this.each(function()
			{
				opts = $.extend({}, {
				'index': 0,
				'loop' : false
				}, options);
				methods.clearAnimation(this);
				$.data(this, 'time', 1);
				$.data(this, 'index', opts['index']);
				$.data(this, 'loop', opts['loop']);
				var timeTable = $.data(this, 'timeTable');
				var width = $.data(this, 'width');
				$(this).css('background',opts['background']+timeTable[opts['index']][1]*width+'px 0px');
				methods.interval(this,callback);
			});
		},
		stop : function()
		{
			return this.each(function()
			{
				methods.clearAnimation(this);
			});
		},
		resume : function(options,callback)
		{
			return this.each(function()
			{
				methods.clearAnimation(this);
				methods.interval(this,callback);
			});
		},
		playing : function()
		{
			var result = false;
			this.each(function()
			{
				var timer = $.data(this, 'timer');
				if ( timer != null )
				{
					result = true;
					return false;
				}
 			});
			return result;
		},
		finish : function()
		{
			var result = false;
			this.each(function()
			{
				var time = $.data(this, 'time');
				if ( time == 0 )
				{
					result = true;
					return false;
				}
 			});
			return result;
		},
		current : function()
		{
			var time = 0;
			this.each(function()
			{
				time = $.data(this, 'time');
				return false;
 			});
			return time;
			
		},
		clearAnimation : function(self)
		{
			var timer = $.data(self, 'timer');
			if ( timer != null )
			{
				clearInterval(timer);
				$.data(self, 'timer', null);
			}
		},
	  	interval : function(self,callback)
	  	{
			var index = $.data(self, 'index');
			var width = $.data(self, 'width');
			var loop = $.data(self, 'loop');
			var timeTable = $.data(self, 'timeTable');
			var timer = setInterval(function()
			{
				var time = $.data(self, 'time');
				time++;
				if ( time >= timeTable[index].length )
				{
					if ( loop )
					{
						time = 1;
						$.data(self, 'time', 1);
					}
					else
					{
						methods.clearAnimation(self);
						if ( callback != null ) callback.call($(self));
						time = timeTable[index].length - 1;
						$.data(self, 'time', 0);
					}
				}
				else
				{
					$.data(self, 'time', time);
				}
				$(self).css({'background-position': timeTable[index][time]*width + 'px 0px'});
			},timeTable[index][0]);
			$.data(self, 'timer', timer);
		}
	};


  	$.fn.animation = function(method, callback)
   	{
    	if ( methods[method] )
    	{
      		return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ), arguments, callback);
    	}
    	else if ( typeof method === 'object' || ! method )
    	{
			return methods.init.apply( this, arguments, callback);
    	}
    	else
    	{
      		$.error( 'Method ' +  method + ' does not exist on jQuery.animation' );
    	}
    	return $(this);
  	};
})(jQuery)
