/*

 Crossroads.js <http://millermedeiros.github.com/crossroads.js>
 Released under the MIT license
 Author: Miller Medeiros
 Version: 0.7.0 - Build: 87 (2011/11/07 03:34 PM)
*/
(function(i){i("crossroads",function(f){function j(a,b){if(a.indexOf)return a.indexOf(b);else{for(var c=a.length;c--;)if(a[c]===b)return c;return-1}}function h(a,b){return"[object "+b+"]"===Object.prototype.toString.call(a)}function i(a){return a===null||a==="null"?null:a==="true"?!0:a==="false"?!1:a===m||a==="undefined"?m:a===""||isNaN(a)?a:parseFloat(a)}function k(){this._routes=[];this.bypassed=new l.Signal;this.routed=new l.Signal}function n(a,b,c,e){var d=h(a,"RegExp");this._router=e;this._pattern=
a;this._paramsIds=d?null:g.getParamIds(this._pattern);this._optionalParamsIds=d?null:g.getOptionalParamsIds(this._pattern);this._matchRegexp=d?a:g.compilePattern(a);this.matched=new l.Signal;b&&this.matched.add(b);this._priority=c||0}var l=f("signals"),g,m;k.prototype={normalizeFn:null,create:function(){return new k},shouldTypecast:!1,addRoute:function(a,b,c){a=new n(a,b,c,this);this._sortedInsert(a);return a},removeRoute:function(a){var b=j(this._routes,a);b!==-1&&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._getMatchedRoutes(a),c=0,e=b.length,d;if(e)for(;c<e;)d=b[c],d.route.matched.dispatch.apply(d.route.matched,d.params),d.isFirst=!c,this.routed.dispatch(a,d),c+=1;else 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)},_getMatchedRoutes:function(a){for(var b=[],c=this._routes,e=c.length,d;d=c[--e];)(!b.length||d.greedy)&&d.match(a)&&b.push({route:d,params:d._getParamsArray(a)});return b},toString:function(){return"[crossroads numRoutes:"+this.getNumRoutes()+"]"}};f=new k;f.VERSION="0.7.0";n.prototype={greedy:!1,rules:void 0,match:function(a){return this._matchRegexp.test(a)&&this._validateParams(a)},_validateParams:function(a){var b=this.rules,c=this._getParamsObject(a),e;for(e in b)if(b.hasOwnProperty(e)&&
!this._isValidParam(a,e,c))return!1;return!0},_isValidParam:function(a,b,c){var e=this.rules[b],d=c[b],f=!1;d==null&&this._optionalParamsIds&&j(this._optionalParamsIds,b)!==-1?f=!0:h(e,"RegExp")?f=e.test(d):h(e,"Array")?f=j(e,d)!==-1:h(e,"Function")&&(f=e(d,a,c));return f},_getParamsObject:function(a){for(var b=this._router.shouldTypecast,c=g.getParamValues(a,this._matchRegexp,b),e={},d=c.length;d--;)e[d]=c[d],this._paramsIds&&(e[this._paramsIds[d]]=c[d]);e.request_=b?i(a):a;e.vals_=c;return e},_getParamsArray:function(a){var b=
this.rules?this.rules.normalize_:null;return(b=b||this._router.normalizeFn)&&h(b,"Function")?b(a,this._getParamsObject(a)):g.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()+"]"}};g=f.patternLexer=function(){function a(a,b){for(var c=
[],d;d=a.exec(b);)c.push(d[1]);return c}var b=/[\\.+*?\^$\[\](){}\/'#]/g,c=/\/$/g,e=/([:}]|\w(?=\/))\/?(:)/g,d=/([:}])\/?(\{)/g,f=/\{([^}]+)\}/g,g=/:([^:]+):/g,j=/(?:\{|:)([^}:]+)(?:\}|:)/g,h=RegExp("__CR_RP__","g"),k=RegExp("__CR_OP__","g"),l=RegExp("__CR_OS__","g"),m=RegExp("__CR_RS__","g");return{getParamIds:function(b){return a(j,b)},getOptionalParamsIds:function(b){return a(g,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]=i(c[a]);a=b}return a},
compilePattern:function(a){if(a=a||"")a=a.replace(c,""),a=a.replace(e,"$1__CR_OS__$2"),a=a.replace(d,"$1__CR_RS__$2"),a=a.replace(g,"__CR_OP__"),a=a.replace(f,"__CR_RP__"),a=a.replace(b,"\\$&"),a=a.replace(l,"\\/?"),a=a.replace(m,"\\/"),a=a.replace(k,"([^\\/]+)?/?"),a=a.replace(h,"([^\\/]+)");return RegExp("^"+a+"/?$")}}}();return f})})(typeof define==="function"&&define.amd?define:function(i,f){typeof module!=="undefined"&&module.exports?module.exports=f(require):window[i]=f(function(f){return window[f]})});
