/* usage example: 
 
$('a').asyncEach( 
 function(){ $(this).fadeOut(); }, //each callback, return false to stop it 
 function(i){ return 5*i; },       //duration of the next delay, this can a number too 
 function(){ alert('finished'); }  //complete callback 
) 
 
*/ 
 
//the plugin: 
 
;(function($){ 
 
function asyncEach() { } 
  
asyncEach.prototype = { 
    init: function( object, each, delay, complete ) { 
        this.object = object; 
        this.each = each; 
        this.complete = complete; 
        this.i = -1; 
        this.length = object.length; 
 
        switch (typeof delay){ 
            case "function":  
                this.delayFn = delay;  
                this.isFn=true;  
                break; 
            case "number": ; 
            case "string":  
                this.delay = +delay;  
                break; 
            default: this.delay = 0; 
        } 
         
        this.next(); 
        return object; 
    }, 
     
    next: function() { 
        var that = this; 
        if ( this.i == this.length )  
            return this.complete.call( this.object, this.object, this ); 
        this.i++; 
         
        if ( this.isFn ) 
            this.delay = +this.delayFn( this.i ); 
         
        var value = this.object[ this.i ]; 
         
        if ( this.each.call( value, this.i, value, this ) !== false ) 
            setTimeout( function() { that.next(); }, this.delay ); 
         
    }, 
     
} 
 
$.asyncEach = function() { 
    var inst = new asyncEach(); 
    return inst.init.apply(inst, arguments); 
} 
 
$.fn.asyncEach = function( each, delay, complete ) { 
    return jQuery.asyncEach( this, each, delay, complete ); 
} 
 
})(jQuery);