// -- Sammy --
// http://code.quirkey.com/sammy
// Version: 0.2.0
// Built: Wed Jun 10 10:42:20 -0400 2009
(function(){var a=false,b=/xyz/.test(function(){xyz})?/\b_super\b/:/.*/;this.Class=function(){};Class.extend=function(g){var f=this.prototype;a=true;var e=new this();a=false;for(var d in g){e[d]=typeof g[d]=="function"&&typeof f[d]=="function"&&b.test(g[d])?(function(h,i){return function(){var k=this._super;this._super=f[h];var j=i.apply(this,arguments);this._super=k;return j}})(d,g[d]):g[d]}function c(){if(!a&&this.init){this.init.apply(this,arguments)}}c.prototype=e;c.constructor=c;c.extend=arguments.callee;return c}})();(function(a){a.srender=function(b,c,d){a.srender.cache=a.srender.cache||{};if(a.srender.cache[b]){fn=a.srender.cache[b]}else{if(typeof c=="undefined"){return false}fn=a.srender.cache[b]=new Function("obj",'var p=[],print=function(){p.push.apply(p,arguments);};with(obj){p.push("'+c.replace(/[\r\t\n]/g," ").replace(/\"/g,'\\"').split("<%").join("\t").replace(/((^|%>)[^\t]*)/g,"$1\r").replace(/\t=(.*?)%>/g,'",$1,"').split("\t").join('");').split("%>").join('p.push("').split("\r").join("")+"\");}return p.join('');")}if(typeof d!="undefined"){return fn(d)}else{return fn}}})(jQuery);(function(c){var a="([^/]+)";var b=/:([\w\d]+)/g;Sammy={};Sammy.VERSION="0.2.0";Sammy.Object=Class.extend({init:function(d){c.extend(this,d)},clone:function(d){if(typeof d=="undefined"){d=this}return c.extend({},d)},toHash:function(){var d={};this.each(function(f,e){if(!c.isFunction(e)){d[f]=e}});return d},toHTML:function(){var d="";this.each(function(f,e){if(!c.isFunction(e)){d+="<strong>"+f+"</strong> "+e+"<br />"}});return d},uuid:function(){if(typeof this._uuid=="undefined"||!this._uuid){this._uuid=(new Date()).getTime()+"-"+parseInt(Math.random()*1000)}return this._uuid},each:function(){var f,e,g,d;f=this;if(typeof arguments[0]!="function"){e=arguments[0];g=arguments[1]}else{e=this;g=arguments[0]}d=function(){return g.apply(f,arguments)};c.each(e,d)},log:function(){var d=[].slice.call(arguments);d.unshift("["+Date()+"]");if(typeof window.console!="undefined"){window.console.log.apply(window.console,d)}else{if(typeof console!="undefined"){console.log.apply(this,d)}else{}}}});Sammy.Application=Sammy.Object.extend({ROUTE_VERBS:["get","post","put","delete"],APP_EVENTS:["run","unload","lookup-route","run-route","route-found","event-context-before","event-context-after","changed","error-404","check-form-submission","redirect"],_last_route:null,_running:false,data_store_name:"sammy-app",element_selector:"body",debug:false,silence_404:true,run_interval_every:50,init:function(d){var e=this;this.routes={};this.listeners={};this.befores=[];this.namespace=this.uuid();this.context_prototype=Sammy.EventContext.extend({});this.each(this.ROUTE_VERBS,function(f,g){this._defineRouteShortcut(g)});d.apply(this);if(this.debug){this.addLogger(function(g,f){e.log(e.toString(),g.cleaned_type,f||{})})}},toString:function(){return"Sammy.Application:"+this.element_selector},$element:function(){return c(this.element_selector)},route:function(h,f,j){var g=this;var i=[];if(f.constructor==String){while((path_match=b.exec(f))!=null){i.push(path_match[1])}f=new RegExp(f.replace(b,a)+"$")}var d={verb:h,path:f,callback:j,param_names:i};if(typeof this.routes[h]=="undefined"||this.routes[h].length==0){this.routes[h]=[d]}else{var e=false;this.each(this.routes[h],function(l,k){if(f.toString().length>=k.path.toString().length){this.routes[h].splice(l,0,d);e=true;return false}});if(!e){this.routes[h].push(d)}}return d},eventNamespace:function(){return this.data_store_name+"-"+this.namespace+"-"},bind:function(d,f,g){if(typeof g=="undefined"){g=f}var e=function(){var j,h,i;j=arguments[0];h=arguments[1];i=arguments[2];j.cleaned_type=j.type.replace(h.eventNamespace(),"");g.apply(h,[j,i])};if(!this.listeners[d]){this.listeners[d]=[]}this.listeners[d].push(e);if(this.isRunning()){return this._listen(d,e)}},trigger:function(d,f,e){if(typeof e=="undefined"){e=new this.context_prototype(this,"bind",d,f)}return this.$element().triggerHandler(e.eventNamespace()+d,[e,f])},before:function(d){return this.befores.push(d)},after:function(d){return this.bind("event-context-after",d)},isRunning:function(){return this._running},helpers:function(d){this.context_prototype=this.context_prototype.extend(d)},run:function(d){if(this.isRunning()){return false}var e=this;this.each(this.listeners,function(f,g){this.each(g,function(j,h){this._listen(f,h)})});this.trigger("run",{start_url:d});this._running=true;this.$element().data(this.data_store_name,this);this.last_location=null;if(this.getLocation()==""&&typeof d!="undefined"){this.setLocation(d)}this._checkLocation();this._interval=setInterval(function(){e._checkLocation.apply(e)},this.run_interval_every);this.bind("changed",function(){e.$element().find("form:not(."+e.eventNamespace()+")").bind("submit",function(){return e._checkFormSubmission(this)}).addClass(e.eventNamespace())});c("body").bind("onunload",function(){e.unload()});this.trigger("changed")},unload:function(){if(!this.isRunning()){return false}var d=this;this.trigger("unload");clearInterval(this._interval);this.$element().find("form").unbind("submit").removeClass(d.eventNamespace());this.$element().removeData(this.data_store_name);this.each(this.listeners,function(e,f){this.each(f,function(h,g){this._unlisten(e,g)})});this._running=false},addLogger:function(d){this.each(this.APP_EVENTS,function(f,g){this.bind(g,d)})},lookupRoute:function(f,e){var d=false;this.trigger("lookup-route",{verb:f,path:e});if(typeof this.routes[f]!="undefined"){this.each(this.routes[f],function(h,g){if(e.match(g.path)){d=g;return false}})}return d},runRoute:function(k,h,j){this.trigger("run-route",{verb:k,path:h,params:j});if(typeof j=="undefined"){j={}}var e=this.lookupRoute(k,h);if(e){this.trigger("route-found",{route:e});if((path_params=e.path.exec(h))!=null){path_params.shift();this.each(path_params,function(l,m){if(e.param_names[l]){j[e.param_names[l]]=m}else{if(!j.splat){j.splat=[]}j.splat.push(m)}})}var f=new this.context_prototype(this,k,h,j);this.last_route=e;var d=true;var i=this.befores.slice(0);while(i.length>0){if(i.shift().apply(f)===false){return false}}f.trigger("event-context-before");var g=e.callback.apply(f);f.trigger("event-context-after");return g}else{this.notFound(k,h)}},getLocation:function(){return this.clone(window.location).hash.toString()},setLocation:function(d){window.location=d},swap:function(d){return this.$element().html(d)},notFound:function(e,d){this.trigger("error-404",{verb:e,path:d});throw ("404 Not Found "+e+" "+d)},_defineRouteShortcut:function(e){var d=this;this[e]=function(f,g){d.route.apply(d,[e,f,g])}},_checkLocation:function(){try{var d,f;d=this.getLocation();if(d!=this.last_location){f=this.runRoute("get",d)}this.last_location=d}catch(g){this.last_location=d;if(g.toString().match(/^404/)&&this.silence_404){return f}else{throw (g)}}return f},_checkFormSubmission:function(g){var d,i,k,j,f;this.trigger("check-form-submission",{form:g});d=c(g);i=d.attr("action");k=d.attr("method").toString().toLowerCase();j={};d.find(":input[type!=submit]").each(function(){j[c(this).attr("name")]=c(this).val()});try{f=this.runRoute(k,i,j)}catch(h){if(h.toString().match(/^404/)&&this.silence_404){return true}else{throw (h)}}return(typeof f=="undefined")?false:f},_listen:function(d,e){return this.$element().bind(this.eventNamespace()+d,e)},_unlisten:function(d,e){return this.$element().unbind(this.eventNamespace()+d,e)}});Sammy.EventContext=Sammy.Object.extend({init:function(g,f,d,e){this.app=g;this.verb=f;this.path=d;this.params=new Sammy.Object(e)},$element:function(){return this.app.$element()},template:function(e,f,d){if(typeof d=="undefined"){d=e}return c.srender(d,e,c.extend({},f,this))},partial:function(g,f,i){var e,h,d;d=this;if(typeof i=="undefined"){if(typeof f=="function"){i=f;f={}}else{i=function(j){d.app.swap(j)}}}e=c.srender(g);f=c.extend({},f,this);if(e){h=e(f);i.apply(d,[h]);d.trigger("changed")}else{c.get(g,function(j){h=c.srender(g,j,f);i.apply(d,[h]);d.trigger("changed")})}},redirect:function(d){this.trigger("redirect",{to:d});return this.app.setLocation(d)},trigger:function(d,e){return this.app.trigger(d,e,this)},eventNamespace:function(){return this.app.eventNamespace()},notFound:function(){return this.app.notFound(this.verb,this.path)}});c.sammy=function(d){return new Sammy.Application(d)}})(jQuery);