(function($)
{
	$._spritehover_thread = function (options)
	{
		this._tickDelegate = Function.CreateDelegate(this, this.tick);
		this.first=options.first;
		this.width=options.width;
		this.total=options.total;
		this.wait=options.wait;
		this.cycle=options.cycle;
		this.pause=options.pause;
	};

	$._spritehover_thread.prototype = {
		tick: function (){
			if(this.cycle==1)
			{
				if(this.first>this.total)
				{
					this.first=0;

					if(this.pause>0)
					{
						setTimeout(this._tickDelegate,this.pause);
						return;
					};
				};
			} else {
				if(this.first>this.total)
					return;
				if(this.first<0)
					return;
			};

			this.obj.css("background-position","-"+this.width*this.first+"px 0px")
			this.first+=this.dir;
			setTimeout(this._tickDelegate,this.wait);
		}
	};

	$.fn.spritehover = function (options)
	{
		var options = $.extend({
			first: 1,
			width: 28,
			total: 5,
			wait: 100,
			cycle: 0,
			pause: 0
		},options);

		options.first--;
		options.total--;

		return this.each(function() {
			$.fn.spritehover.sys.arr[$.fn.spritehover.sys.cnt]=new $._spritehover_thread(options);
			var sa=$.fn.spritehover.sys.arr[$.fn.spritehover.sys.cnt];
			$.fn.spritehover.sys.cnt++;
		
			sa.obj=$(this);

			if(options.cycle==1)
			{
				sa.dir=1;
				setTimeout(sa._tickDelegate, options.wait);
			} else {
				$(this).hover(
					function(){
						if(sa.first<0)
							sa.first=0;
						sa.dir=1;
						setTimeout(sa._tickDelegate, options.wait);
					},
					function(){
						if(sa.first>sa.total)
							sa.first=sa.total;
						sa.dir=-1;
						setTimeout(sa._tickDelegate, options.wait);
					}
				);
			};
		});
	};
	if (!Function.CreateDelegate) {
		Function.CreateDelegate = function(instance, method) {
			return function() {
				method.apply(instance, arguments);
			}
		}
	}
	$.fn.spritehover.sys = {
		arr: {},
		cnt: 0
        };
})(jQuery);