/*! scorpion 0.1.0 */var Scorpion=function(){var a=function(){var a=function(a,b){setTimeout(function(){a(b)})},b=function(a,b){return function(c){try{var d=a(c);d&&d.then?d.then(function(a){b.resolve(a)},function(a){b.reject(a)}):b.resolve(d)}catch(e){b.reject(e)}}},c=function(){this.resolved=!1,this.rejected=!1,this.onSuccess=[],this.onError=[]};c.prototype.then=function(c,e){c=c||function(a){return a},e=e||function(a){throw a};var f=new d,g=b(c,f),h=b(e,f);return this.resolved?a(g,this.value):this.rejected?a(h,this.value):(null!=this.onSuccess&&this.onSuccess.push(g),null!=this.onError&&this.onError.push(h)),f.promise};var d=function(){this.promise=new c};return d.prototype.resolve=function(a){if(this.promise.resolved)throw new Error("Cannot re-resolve already resolved promise");if(this.promise.rejected)throw new Error("Cannot resolve a rejected promise");this.promise.resolved=!0,this.promise.value=a;var b=this.promise.onSuccess;this.promise.onSuccess=null,this.promise.onError=null,setTimeout(function(){b.forEach(function(b){b(a)})})},d.prototype.reject=function(a){if(this.promise.resolved)throw new Error("Cannot reject an already resolved promise");if(this.promise.rejected)throw new Error("Cannot re-reject a rejected promise");this.promise.rejected=!0,this.promise.value=a;var b=this.promise.onError;this.promise.onSuccess=null,this.promise.onError=null,setTimeout(function(){b.forEach(function(b){b(a)})})},d.waitForAll=function(a){var b=new d,c=0,e=0,f={},g={},h=a.length;a.forEach(function(a,b){a.then(function(a){g[b]=a,c++,i()},function(a){f[b]=a,e++,i()})});var i=function(){if(c==h){for(var a=[],d=0,i=h;i>d;++d)a.push(g[d]);b.resolve(a)}else c+e==h&&b.reject({errors:f,values:g})};return i(),b.promise},d.collapseErrorObject=function(a){var b=[];for(var c in a.errors)b=b.concat(a.errors[c]);throw b},d}(),b=function(){var a=/^function[^(]*\(([^)]*)\)/,b=function(b){var c=a.exec(b.toString().replace(/\s+/g,""));if(null==c)throw new Error("Unable to parse fn definition");return c[1]?c[1].split(/,/):[]},c=function(a){var c,d;return"function"==typeof a||a instanceof Function?(d=b(a),c=a):(c=a[a.length-1],d=a.slice(0,a.length-1)),[c,d]},d=function(a){var b=a.split("!");if(0==b.length)throw new Error("Invalid dependency: "+a);return 1==b.length?{prefix:"",name:b[0]}:{prefix:b[0],name:b.slice(1).join("!")}};return{spec:c,dep:d}}(),c=function(a,b){var c=function(a){this.plugins=a};c.rejected=function(a,b){var c=this.defer();return c.reject(this.error(a,b)),c.promise},c.prototype.rejected=c.rejected,c.resolved=function(a){var b=this.defer();return b.resolve(a),b.promise},c.prototype.resolved=c.resolved,c.prototype.get=function(a,b){if(b=[a].concat(b||[]),b&&0!=b.lastIndexOf(a))return this.rejected("Cyclic dependency: "+b.join(" <- "));for(var d=0,e=this.plugins.length;e>d;++d){var f=this.plugins[d].get(this,a,b);if(f)return f.destroy||(f.destroy=function(){return c.resolved(!0)}),f}return this.rejected("Unknown dependency: "+b.join(" <- "))},c.prototype.register=function(a,b){if(("function"==typeof a||a instanceof Function)&&(b=a,a=b.name),"string"==typeof a||a instanceof String){for(var c=0,d=this.plugins.length;d>c;++c)if(this.plugins[c].register(this,a,b))return this;throw this.error("No plugin handled registration of: "+a)}for(var e in a)this.register(e,a[e]);return this},c.prototype.invoke=function(b){var d=this,e=this.parseSpec(b),f=e[0],g=e[1],h=g.map(function(a){return d.get(a,[])}),i=this.waitForAll(h),j=i.then(function(a){return f.apply(d,a)},a.collapseErrorObject);return j.destroy=function(){return c.waitForAll(h.map(function(a){return a.destroy()})).then(function(){return!0},a.collapseErrorObject)},j},c.error=function(a,b){return new d(a,b)},c.prototype.error=c.error,c.defer=function(){return new a},c.prototype.defer=c.defer,c.parseSpec=b.spec,c.prototype.parseSpec=b.spec,c.waitForAll=a.waitForAll,c.prototype.waitForAll=a.waitForAll;var d=function(a,b){this.name="ScorpionError",this.message=a,this.cause=b};return d.prototype=new Error,d.prototype.constructor=d,d.prototype.toString=function(){return"ScorpionError: "+this.message+(this.cause?" [caused by "+this.cause+"]":"")},c}(a,b);return c.prefixPlugin=function(a,b){return{register:function(c,d,e){return 0==d.indexOf(a)?b.register(c,d.substr(a.length),e):!1},get:function(c,d,e){return 0==d.indexOf(a)?b.get(c,d.substr(a.length),e):null}}},c.DOMPlugin=function(){var a=function(){this.aliases={}};return a.prototype.register=function(a,b,c){return this.aliases[b]=c,!0},a.prototype.get=function(a,b,c){var d=a.defer();if("undefined"!=typeof $&&$){var e=function(){var a=$(this.aliases[b]||b);a.length&&(clearInterval(f),d.resolve(a))}.bind(this),f=setInterval(e,100);e()}else d.reject(new Error("jQuery was not found "+c.join(" <- ")));return d.promise},a}(),c.HTTPPlugin=function(){var a=function(){this.aliases={}};return a.prototype.register=function(a,b,c){return this.aliases[b]=c,!0},a.prototype.get=function(a,b,d){var e=a.defer();return"undefined"!=typeof $&&$?($.ajax(this.aliases[b]||b).then(function(a){e.resolve(a)},e.reject.bind(e)),e.promise.destroy=function(){return c.resolved(!0)}):e.reject(new Error("jQuery was not found "+d.join(" <- "))),e.promise},a}(),c.ValuePlugin=function(){var b=function(){this.specs={},this.values={}};return b.prototype.register=function(a,b,c){return this.specs[b]=c,!0},b.prototype.get=function(b,c,d){if(c in this.values)return this.values[c].references++,this.values[c];if(c in this.specs){var e=this.specs[c],f=b.parseSpec(e),g=f[0],h=f[1],i=!1,j=h.map(function(a){return"__requestor__"==a?(i=!0,b.resolved(d[1])):b.get(a,d)}),k=b.waitForAll(j),l=null,m=b.defer(),n=m.promise;k.then(function(a){var c=Object.create(b);try{m.resolve(g.apply(c,a))}catch(d){m.reject(d)}finally{l=c.onDestroy,n.references<=0&&l&&l()}},function(b){try{a.collapseErrorObject(b)}catch(c){m.reject(c)}}),n.references=1;var o=this.values;return n.destroy=function(){return this.references--,this.references<=0?(l&&l(),delete o[c],n.resolved||n.rejected||m.reject(b.error("Promise destroyed before value completed construction")),b.waitForAll(j.map(function(a){return a.destroy()})).then(function(){return!0},a.collapseErrorObject)):b.resolved(!0)},i?n:this.values[c]=n}return null},b}(),c}();"undefined"!=typeof module&&(module.exports=Scorpion);