/*

 Crossroads.js <http://millermedeiros.github.com/crossroads.js>
 Released under the MIT license
 Author: Miller Medeiros
 Version: 0.6.0 - Build: 77 (2011/08/31 11:12 PM)
*/
(function(h){h(["signals"],function(f){function h(a,b){for(var c=a.length;c--;)if(a[c]===b)return c;return-1}function j(a,b){return"[object "+a+"]"===Object.prototype.toString.call(b)}function m(a){return a===null?a:o.test(a)?a.toLowerCase()==="true":a===""||isNaN(a)?a:parseFloat(a)}function l(){this._routes=[];this.bypassed=new f.Signal;this.routed=new f.Signal}function n(a,b,c,d){var g=j("RegExp",a);this._router=d;this._pattern=a;this._paramsIds=g?null:i.getParamIds(this._pattern);this._optionalParamsIds=
g?null:i.getOptionalParamsIds(this._pattern);this._matchRegexp=g?a:i.compilePattern(a);this.matched=new f.Signal;b&&this.matched.add(b);this._priority=c||0}var k,i,o=/^(true|false)$/i;l.prototype={create:function(){return new l},shouldTypecast:!1,addRoute:function(a,b,c){a=new n(a,b,c,this);this._sortedInsert(a);return a},removeRoute:function(a){var b=h(this._routes,a);b>=0&&this._routes.splice(b,1);a._destroy()},removeAllRoutes:function(){for(var a=this.getNumRoutes();a--;)this._routes[a]._destroy();
this._routes.length=0},parse:function(a){var a=a||"",b=this._getMatchedRoute(a),c=b?b._getParamsArray(a):null;b?(c?b.matched.dispatch.apply(b.matched,c):b.matched.dispatch(),this.routed.dispatch(a,b,c)):this.bypassed.dispatch(a)},getNumRoutes:function(){return this._routes.length},_sortedInsert:function(a){var b=this._routes,c=b.length;do--c;while(b[c]&&a._priority<=b[c]._priority);b.splice(c+1,0,a)},_getMatchedRoute:function(a){for(var b=this._routes,c=b.length,d;d=b[--c];)if(d.match(a))return d;
return null},toString:function(){return"[crossroads numRoutes:"+this.getNumRoutes()+"]"}};k=new l;k.VERSION="0.6.0";n.prototype={rules:void 0,match:function(a){return this._matchRegexp.test(a)&&this._validateParams(a)},_validateParams:function(a){var b=this.rules,c=this._getParamValuesObject(a),d;for(d in b)if(b.hasOwnProperty(d)&&!this._isValidParam(a,d,c))return!1;return!0},_isValidParam:function(a,b,c){var d=this.rules[b],g=c[b],f;g==null&&this._optionalParamsIds&&h(this._optionalParamsIds,b)!==
-1?f=!0:j("RegExp",d)?f=d.test(g):j("Array",d)?f=h(d,g||"")!==-1:j("Function",d)&&(f=d(g,a,c));return f||!1},_getParamValuesObject:function(a){for(var b=this._router.shouldTypecast,c=i.getParamValues(a,this._matchRegexp,b),d={},g=c.length;g--;)d[g]=c[g],this._paramsIds&&(d[this._paramsIds[g]]=c[g]);d.request_=b?m(a):a;return d},_getParamsArray:function(a){var b=this._getParamValuesObject(a),c=this.rules?this.rules.normalize_:null;return j("Function",c)?c(a,b):i.getParamValues(a,this._matchRegexp,
this._router.shouldTypecast)},dispose:function(){this._router.removeRoute(this)},_destroy:function(){this.matched.dispose();this.matched=this._pattern=this._matchRegexp=null},toString:function(){return'[Route pattern:"'+this._pattern+'", numListeners:'+this.matched.getNumListeners()+"]"}};i=k.patternLexer=function(){var a=/[\\.+*?\^$\[\](){}\/'#]/g,b=/\/$/g,c=/([:}]|\w(?=\/))\/?(:)/g,d=/([:}])\/?(\{)/g,g=/\{([^}]+)\}/g,f=/:([^:]+):/g,h=/(?:\{|:)([^}:]+)(?:\}|:)/g,i=RegExp("___CR_REQ___","g"),j=RegExp("___CR_OPT___",
"g"),k=RegExp("___CR_OPT_SLASH___","g"),l=RegExp("___CR_REQ_SLASH___","g");return{getParamIds:function(a){for(var b=[],c;c=h.exec(a);)b.push(c[1]);return b},getOptionalParamsIds:function(a){for(var b=[],c;c=f.exec(a);)b.push(c[1]);return b},getParamValues:function(a,b,c){if(a=b.exec(a))if(a.shift(),c){c=a;a=c.length;for(b=[];a--;)b[a]=m(c[a]);a=b}return a},compilePattern:function(e){if(e=e||"")e=e.replace(b,""),e=e.replace(c,"$1___CR_OPT_SLASH___$2"),e=e.replace(d,"$1___CR_REQ_SLASH___$2"),e=e.replace(f,
"___CR_OPT___"),e=e.replace(g,"___CR_REQ___"),e=e.replace(a,"\\$&"),e=e.replace(k,"\\/?"),e=e.replace(l,"\\/"),e=e.replace(j,"([^\\/]+)?/?"),e=e.replace(i,"([^\\/]+)");return RegExp("^"+e+"/?$")}}}();return k})})(typeof require==="undefined"?function(h,f){this.crossroads=f(signals)}:typeof exports==="undefined"?function(h,f){define("crossroads",h,f)}:function(h,f){module.exports=f.apply(this,h.map(require))});
