* Davis - http://davisjs.com - JavaScript Routing - 0.8.1
* Copyright (C) 2011 Oliver Nightingale
* MIT Licensed
*/Davis=function(a){var b=new Davis.App;return a&&a.call(b),Davis.$(function(){b.start()}),b},window.jQuery?Davis.$=jQuery:Davis.$=null,Davis.supported=function(){return typeof window.history.pushState=="function"},Davis.noop=function(){},Davis.extend=function(a){a(Davis)},Davis.version="0.8.1",Davis.utils=function(){if(Array.prototype.every)var a=function(a,b){return a.every(b,arguments[2])};else var a=function(a,b){if(a===void 0||a===null)throw new TypeError;var c=Object(a),d=c.length>>>0;if(typeof b!="function")throw new TypeError;var e=arguments[2];for(var f=0;f<d;f++)if(f in c&&!b.call(e,c[f],f,c))return!1;return!0};if(Array.prototype.forEach)var b=function(a,b){return a.forEach(b,arguments[2])};else var b=function(a,b){if(a===void 0||a===null)throw new TypeError;var c=Object(a),d=c.length>>>0;if(typeof b!="function")throw new TypeError;var e=arguments[2];for(var f=0;f<d;f++)f in c&&b.call(e,c[f],f,c)};if(Array.prototype.filter)var c=function(a,b){return a.filter(b,arguments[2])};else var c=function(a,b){if(a===void 0||a===null)throw new TypeError;var c=Object(a),d=c.length>>>0;if(typeof b!="function")throw new TypeError;var e=[],f=arguments[2];for(var g=0;g<d;g++)if(g in c){var h=c[g];b.call(f,h,g,c)&&e.push(h)}return e};var d=function(a,b){var b=b||0;return Array.prototype.slice.call(a,b)};return{every:a,forEach:b,filter:c,toArray:d}}(),Davis.listener=function(){var a={A:function(a){return a.hostname!==window.location.hostname||a.protocol!==window.location.protocol},FORM:function(a){var b=document.createElement("a");return b.href=a.action,this.A(b)}},b=function(b){return a[b.nodeName.toUpperCase()]?a[b.nodeName.toUpperCase()](b):!0},c=function(a){return function(c){if(b(this))return!0;var d=new Davis.Request(a.call(Davis.$(this)));return Davis.location.assign(d),!1}},d=c(function(){var a=this;return{method:"get",fullPath:this.attr("href"),title:this.attr("title"),delegateToServer:function(){window.location.pathname=a.attr("href")}}}),e=c(function(){var a=this;return{method:this.attr("method"),fullPath:decodeURI(this.serialize()?[this.attr("action"),this.serialize()].join("?"):this.attr("action")),title:this.attr("title"),delegateToServer:function(){a.submit()}}});this.listen=function(){Davis.$(document).delegate(this.settings.formSelector,"submit",e),Davis.$(document).delegate(this.settings.linkSelector,"click",d)},this.unlisten=function(){Davis.$(document).undelegate(this.settings.linkSelector,"click",d),Davis.$(document).undelegate(this.settings.formSelector,"submit",e)}},Davis.event=function(){var a={};this.bind=function(b,c){return(a[b]=a[b]||[]).push(c),this},this.trigger=function(b){var c=Davis.utils.toArray(arguments,1),d=a[b];if(!d)return this;for(var e=0,f=d.length;e<f;++e)d[e].apply(this,c);return this}},Davis.logger=function(){function a(){return"["+Date()+"]"}function b(b){var c=Davis.utils.toArray(b);return c.unshift(a()),c.join(" ")}var c=function(a){return function(){window.console&&console[a](b(arguments))}},d=c("error"),e=c("info"),f=c("warn");this.logger={error:d,info:e,warn:f}},Davis.Route=function(){var a=/:([\w\d]+)/g,b="([^/]+)",c=function(c,d,e){var f=function(){if(d instanceof RegExp)return d;var c=d.replace(a,b);return d.lastIndex=0,new RegExp("^"+c+"$","gi")},g=function(){return c instanceof RegExp?c:new RegExp("^"+c+"$","i")},h=function(){var b=[],c;while(c=a.exec(d))b.push(c[1]);return b};this.paramNames=h(),this.path=f(),this.method=g(),this.callback=e};return c.prototype.match=function(a,b){return this.reset(),this.method.test(a)&&this.path.test(b)},c.prototype.reset=function(){this.method.lastIndex=0,this.path.lastIndex=0},c.prototype.run=function(a){this.reset();var b=this.path.exec(a.path);if(b){b.shift();for(var c=0;c<b.length;c++)a.params[this.paramNames[c]]=b[c]}return this.callback.call(a,a)},c.prototype.toString=function(){return[this.method,this.path].join(" ")},c}(),Davis.router=function(){this.route=function(b,c,d){var e=function(c,d){var e=new Davis.Route(b,c,d);return a.push(e),e};return arguments.length==1?e:e(c,d)},this.get=this.route("get"),this.post=this.route("post"),this.put=this.route("put"),this.del=this.route("delete"),this.state=this.route("state"),this.trans=function(a,b){if(b)var c=[a,decodeURIComponent(Davis.$.param(b))].join("?");else var c=a;var d=new Davis.Request({method:"state",fullPath:c,title:""});Davis.location.assign(d)},this.filter=function(a){return function(){var c=/.+/;if(arguments.length==1)var d=/.+/,e=arguments[0];else if(arguments.length==2)var d=arguments[0],e=arguments[1];var f=new Davis.Route(c,d,e);return b[a].push(f),f}},this.lookupFilter=function(a){return function(c,d){return Davis.utils.filter(b[a],function(a){return a.match(c,d)})}},this.before=this.filter("before"),this.after=this.filter("after"),this.lookupBeforeFilter=this.lookupFilter("before"),this.lookupAfterFilter=this.lookupFilter("after");var a=[],b={before:[],after:[]};this.lookupRoute=function(b,c){return Davis.utils.filter(a,function(a){return a.match(b,c)})[0]}},Davis.history=function(){function c(){return"state"in window.history?!0:b}function d(b){a.push(b)}function e(a){window.addEventListener("popstate",a,!0)}function f(a){return function(d){d.state&&d.state._davis?a(new Davis.Request(d.state._davis)):(c()&&a(Davis.Request.forPageLoad()),b=!0)}}function g(a){return{_davis:a}}function h(a){d(a),e(f(a))}function i(b){history.pushState(g(b.toJSON()),b.title,b.location()),Davis.utils.forEach(a,function(a){a(b)})}function j(b){history.replaceState(g(b.toJSON()),b.title,b.location()),Davis.utils.forEach(a,function(a){a(b)})}function k(){return window.location.pathname+(window.location.search?window.location.search:"")}var a=[],b=!1;return{onChange:h,current:k,assign:i,replace:j}}(),Davis.location=function(){function b(b){a=b}function c(){return a.current()}function d(b){a.assign(b)}function e(b){a.replace(b)}function f(b){a.onChange(b)}var a=Davis.history;return{setLocationDelegate:b,current:c,assign:d,replace:e,onChange:f}}(),Davis.Request=function(){var a=function(b){var c=Davis.$.extend({},{title:"",fullPath:"",method:"get"},b),d=this;this.raw=c,this.params={},this.title=c.title,this.queryString=c.fullPath.split("?")[1],this._staleCallback=function(){},this.queryString&&Davis.utils.forEach(this.queryString.split("&"),function(a){var b=a.split("=")[0],c=a.split("=")[1],e=/^(\w+)\[(\w+)?\](\[\])?/,f;if(f=e.exec(b)){var g=f[1],b=f[2],h=!!f[3],i=d.params[g]||{};h?(i[b]=i[b]||[],i[b].push(c),d.params[g]=i):!b&&!h?(i=d.params[g]||[],i.push(c),d.params[g]=i):(i[b]=c,d.params[g]=i)}else d.params[b]=c}),c.fullPath=c.fullPath.replace(/^https?:\/\/.+?\//,"/"),this.method=(this.params._method||c.method).toLowerCase(),this.path=c.fullPath.replace(/\?.+$/,"").replace(/^https?:\/\/[^\/]+/,""),this.fullPath=c.fullPath,this.delegateToServer=c.delegateToServer||Davis.noop,this.isForPageLoad=c.forPageLoad||!1,a.prev&&a.prev.makeStale(this),a.prev=this};return a.prototype.redirect=function(b){Davis.location.replace(new a({method:"get",fullPath:b,title:this.title}))},a.prototype.whenStale=function(a){this._staleCallback=a},a.prototype.makeStale=function(a){this._staleCallback.call(a,a)},a.prototype.location=function(){return this.method==="get"?this.fullPath:""},a.prototype.toString=function(){return[this.method.toUpperCase(),this.path].join(" ")},a.prototype.toJSON=function(){return{title:this.raw.title,fullPath:this.raw.fullPath,method:this.raw.method}},a.forPageLoad=function(){return new this({method:"get",fullPath:Davis.location.current(),title:document.title,forPageLoad:!0})},a.prev=null,a}(),Davis.App=function(){function a(){this.running=!1,this.boundToInternalEvents=!1,this.use(Davis.listener),this.use(Davis.event),this.use(Davis.router),this.use(Davis.logger)}return a.prototype.configure=function(a){a.call(this.settings,this.settings)},a.prototype.use=function(a){a.apply(this,Davis.utils.toArray(arguments,1))},a.prototype.helpers=function(a){for(property in a)a.hasOwnProperty(property)&&(Davis.Request.prototype[property]=a[property])},a.prototype.settings={linkSelector:"a",formSelector:"form",throwErrors:!0,handleRouteNotFound:!1,generateRequestOnPageLoad:!1},a.prototype.start=function(){var a=this;if(this.running)return;if(!Davis.supported()){this.trigger("unsupported");return}var b=function(a){return function(b){var c=b.run(a,a);return typeof c=="undefined"||c}},c=function(c){return Davis.utils.every(a.lookupBeforeFilter(c.method,c.path),b(c))},d=function(d){if(c(d)){a.trigger("lookupRoute",d);var e=a.lookupRoute(d.method,d.path);if(e){a.trigger("runRoute",d,e);try{e.run(d),a.trigger("routeComplete",d,e)}catch(f){a.trigger("routeError",d,e,f)}Davis.utils.every(a.lookupAfterFilter(d.method,d.path),b(d))}else a.trigger("routeNotFound",d)}else a.trigger("requestHalted",d)},e=function(){a.bind("runRoute",function(b){a.logger.info("runRoute: "+b.toString())}).bind("routeNotFound",function(b){!a.settings.handleRouteNotFound&&!b.isForPageLoad&&(a.stop(),b.delegateToServer()),a.logger.warn("routeNotFound: "+b.toString())}).bind("start",function(){a.logger.info("application started")}).bind("stop",function(){a.logger.info("application stopped")}).bind("routeError",function(b,c,d){if(a.settings.throwErrors)throw d;a.logger.error(d.message,d.stack)}),Davis.location.onChange(function(a){d(a)}),a.boundToInternalEvents=!0};this.boundToInternalEvents||e(),this.listen(),this.trigger("start"),this.running=!0,this.settings.generateRequestOnPageLoad&&d(Davis.Request.forPageLoad())},a.prototype.stop=function(){this.unlisten(),this.trigger("stop"),this.running=!1},a}();