Request.Queue=new Class({Implements:[Options,Events],Binds:["attach","request","complete","cancel","success","failure","exception"],options:{stopOnFailure:true,autoAdvance:true,concurrent:1,requests:{}},initialize:function(a){var b;if(a){b=a.requests;delete a.requests}this.setOptions(a);this.requests={};this.queue=[];this.reqBinders={};if(b){this.addRequests(b)}},addRequest:function(a,b){this.requests[a]=b;this.attach(a,b);return this},addRequests:function(a){Object.each(a,function(c,b){this.addRequest(b,c)},this);return this},getName:function(a){return Object.keyOf(this.requests,a)},attach:function(a,b){if(b._groupSend){return this}["request","complete","cancel","success","failure","exception"].each(function(c){if(!this.reqBinders[a]){this.reqBinders[a]={}}this.reqBinders[a][c]=function(){this["on"+c.capitalize()].apply(this,[a,b].append(arguments))}.bind(this);b.addEvent(c,this.reqBinders[a][c])},this);b._groupSend=b.send;b.send=function(c){this.send(a,c);return b}.bind(this);return this},removeRequest:function(b){var a=typeOf(b)=="object"?this.getName(b):b;if(!a&&typeOf(a)!="string"){return this}b=this.requests[a];if(!b){return this}["request","complete","cancel","success","failure","exception"].each(function(c){b.removeEvent(c,this.reqBinders[a][c])},this);b.send=b._groupSend;delete b._groupSend;return this},getRunning:function(){return Object.filter(this.requests,function(a){return a.running})},isRunning:function(){return !!(Object.keys(this.getRunning()).length)},send:function(b,a){var c=function(){this.requests[b]._groupSend(a);this.queue.erase(c)}.bind(this);c.name=b;if(Object.keys(this.getRunning()).length>=this.options.concurrent||(this.error&&this.options.stopOnFailure)){this.queue.push(c)}else{c()}return this},hasNext:function(a){return(!a)?!!this.queue.length:!!this.queue.filter(function(b){return b.name==a}).length},resume:function(){this.error=false;(this.options.concurrent-Object.keys(this.getRunning()).length).times(this.runNext,this);return this},runNext:function(a){if(!this.queue.length){return this}if(!a){this.queue[0]()}else{var b;this.queue.each(function(c){if(!b&&c.name==a){b=true;c()}})}return this},runAll:function(){this.queue.each(function(a){a()});return this},clear:function(a){if(!a){this.queue.empty()}else{this.queue=this.queue.map(function(b){if(b.name!=a){return b}else{return false}}).filter(function(b){return b})}return this},cancel:function(a){this.requests[a].cancel();return this},onRequest:function(){this.fireEvent("request",arguments)},onComplete:function(){this.fireEvent("complete",arguments);if(!this.queue.length){this.fireEvent("end")}},onCancel:function(){if(this.options.autoAdvance&&!this.error){this.runNext()}this.fireEvent("cancel",arguments)},onSuccess:function(){if(this.options.autoAdvance&&!this.error){this.runNext()}this.fireEvent("success",arguments)},onFailure:function(){this.error=true;if(!this.options.stopOnFailure&&this.options.autoAdvance){this.runNext()}this.fireEvent("failure",arguments)},onException:function(){this.error=true;if(!this.options.stopOnFailure&&this.options.autoAdvance){this.runNext()}this.fireEvent("exception",arguments)}});