/*
	jAni jQuery Plugin
	� 2009 ajaxBlender.com
	For any questions please visit www.ajaxblender.com 
	or email us at support@ajaxblender.com
*/

;(function($){

    var defaults = {
                    frameWidth:      100,
                    frameHeight:     100,
                    speed:           100,
                    totalFrames:     0,
                    loop:            true
                };

 
    var methods = {
            init : function(options){
                        
                        var item = $(this);
                        item.data('settings', $.extend({}, defaults, options));

			item.width($(this).data('settings').frameWidth);
			item.height($(this).data('settings').frameHeight);
			item.css('background-position', '0 0');
                        
                    },

            pause : function(){
                            var item = $(this);
                            if(item.data('tm')){ clearTimeout(item.data('tm')); }
                            item.data('tm', null); },

            stop : function(){
                    var item = $(this);
                    if(item.data('tm')){ clearTimeout(item.data('tm')); }
                    item.data('tm', null);
                    item.data('currFrame', 0);
                    item.css('background-position', '0 0');
},
	    reset : function() {
	    var item = $(this);
	     item.data('currFrame', 0);
	     $(item).css('background-position', '0 0');                                	    
	},

            play : function(reverse, myFunc){                
                var item = $(this);

                if(item.data('tm')){ clearTimeout(item.data('tm')); }                

		if(item.data('settings').totalFrames <= 0 || !item || !item.length){ return; }

		function _animate(){                        
			var tmFn = function(){ _animate(); };
			var bgPos = $(item).css('background-position');                
			var ie = true;

			if(bgPos == 'undefined' || bgPos == null){
				bgPos = parseInt($(item).css('background-position-y'));
			} else {
                            bgPos = bgPos.split(' ');
                            bgPos = parseInt(bgPos[1]);
                            ie = false;
			}

                        if(reverse){ bgPos += item.data('settings').frameHeight; item.data('currFrame', item.data('currFrame') -1 );}
                        else { bgPos -= item.data('settings').frameHeight; item.data('currFrame', item.data('currFrame') + 1);}

			if(ie){ $(item).css('background-position-y', bgPos + 'px'); }
			else { $(item).css('background-position', ('0px ' + bgPos + 'px')); }


			if(!item.data('settings').loop && (!reverse && item.data('currFrame') >= (item.data('settings').totalFrames - 1) || (reverse && item.data('currFrame') <= 0))){
			    if(myFunc) {myFunc();}
			    return;
			}

			if(item.data('settings').loop && (!reverse && item.data('currFrame') >= (item.data('settings').totalFrames) || (reverse && item.data('currFrame') < 0))){
			    item.data('currFrame', 0);
			    $(item).css('background-position', '0 0');                                			    
			}

			item.data('tm', setTimeout(tmFn, item.data('settings').speed));
		}
                
		if(!item.data('tm')){ item.jani('stop'); }
		_animate();
                
                
            }
	};


	$.fn.jani = function(method){                                             
        
        if ( methods[method] ) {
            return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } else if ( typeof method === 'object' || ! method ) {
            return methods.init.apply( this, arguments );
        } else {
            $.error( 'Method ' +  method + ' does not exist on jQuery.tooltip' );
        }                                		
    };

    
    
		
})(jQuery);

