(function(){var I=this,A=I.Backbone,L;L=typeof exports!=="undefined"?exports:I.Backbone={};L.VERSION="0.5.3";var K=I._;if(!K&&typeof require!=="undefined"){K=require("underscore")._}var J=I.jQuery||I.Zepto;L.noConflict=function(){I.Backbone=A;return this};L.emulateHTTP=!1;L.emulateJSON=!1;L.Events={bind:function(f,e,h){var g=this._callbacks||(this._callbacks={});(g[f]||(g[f]=[])).push([e,h]);return this},unbind:function(g,f){var j;if(g){if(j=this._callbacks){if(f){j=j[g];if(!j){return this}for(var i=0,h=j.length;i<h;i++){if(j[i]&&f===j[i][0]){j[i]=null;break}}}else{j[g]=[]}}}else{this._callbacks={}}return this},trigger:function(j){var i,p,o,n,m=2;if(!(p=this._callbacks)){return this}for(;m--;){if(i=m?j:"all",i=p[i]){for(var l=0,k=i.length;l<k;l++){(o=i[l])?(n=m?Array.prototype.slice.call(arguments,1):arguments,o[0].apply(o[1]||this,n)):(i.splice(l,1),l--,k--)}}}return this}};L.Model=function(f,e){var g;f||(f={});if(g=this.defaults){K.isFunction(g)&&(g=g.call(this)),f=K.extend({},g,f)}this.attributes={};this._escapedAttributes={};this.cid=K.uniqueId("c");this.set(f,{silent:!0});this._changed=!1;this._previousAttributes=K.clone(this.attributes);if(e&&e.collection){this.collection=e.collection}this.initialize(f,e)};K.extend(L.Model.prototype,L.Events,{_previousAttributes:null,_changed:!1,idAttribute:"id",initialize:function(){},toJSON:function(){return K.clone(this.attributes)},get:function(e){return this.attributes[e]},escape:function(f){var e;if(e=this._escapedAttributes[f]){return e}e=this.attributes[f];return this._escapedAttributes[f]=(e==null?"":""+e).replace(/&(?!\w+;|#\d+;|#x[\da-f]+;)/gi,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;").replace(/'/g,"&#x27;").replace(/\//g,"&#x2F;")},has:function(e){return this.attributes[e]!=null},set:function(i,f){f||(f={});if(!i){return this}if(i.attributes){i=i.attributes}var n=this.attributes,m=this._escapedAttributes;if(!f.silent&&this.validate&&!this._performValidation(i,f)){return !1}if(this.idAttribute in i){this.id=i[this.idAttribute]}var l=this._changing;this._changing=!0;for(var k in i){var j=i[k];if(!K.isEqual(n[k],j)){n[k]=j,delete m[k],this._changed=!0,f.silent||this.trigger("change:"+k,this,j,f)}}!l&&!f.silent&&this._changed&&this.change(f);this._changing=!1;return this},unset:function(f,e){if(!(f in this.attributes)){return this}e||(e={});var g={};g[f]=void 0;if(!e.silent&&this.validate&&!this._performValidation(g,e)){return !1}delete this.attributes[f];delete this._escapedAttributes[f];f==this.idAttribute&&delete this.id;this._changed=!0;e.silent||(this.trigger("change:"+f,this,void 0,e),this.change(e));return this},clear:function(f){f||(f={});var e,h=this.attributes,g={};for(e in h){g[e]=void 0}if(!f.silent&&this.validate&&!this._performValidation(g,f)){return !1}this.attributes={};this._escapedAttributes={};this._changed=!0;if(!f.silent){for(e in h){this.trigger("change:"+e,this,void 0,f)}this.change(f)}return this},fetch:function(f){f||(f={});var e=this,g=f.success;f.success=function(j,i,h){if(!e.set(e.parse(j,h),f)){return !1}g&&g(e,j)};f.error=H(f.error,e,f);return(this.sync||L.sync).call(this,"read",this,f)},save:function(g,e){e||(e={});if(g&&!this.set(g,e)){return !1}var j=this,i=e.success;e.success=function(k,m,l){if(!j.set(j.parse(k,l),e)){return !1}i&&i(j,k,l)};e.error=H(e.error,j,e);var h=this.isNew()?"create":"update";return(this.sync||L.sync).call(this,h,this,e)},destroy:function(f){f||(f={});if(this.isNew()){return this.trigger("destroy",this,this.collection,f)}var e=this,g=f.success;f.success=function(h){e.trigger("destroy",e,e.collection,f);g&&g(e,h)};f.error=H(f.error,e,f);return(this.sync||L.sync).call(this,"delete",this,f)},url:function(){var e=F(this.collection)||this.urlRoot||E();if(this.isNew()){return e}return e+(e.charAt(e.length-1)=="/"?"":"/")+encodeURIComponent(this.id)},parse:function(e){return e},clone:function(){return new this.constructor(this)},isNew:function(){return this.id==null},change:function(e){this.trigger("change",this,e);this._previousAttributes=K.clone(this.attributes);this._changed=!1},hasChanged:function(e){if(e){return this._previousAttributes[e]!=this.attributes[e]}return this._changed},changedAttributes:function(f){f||(f=this.attributes);var e=this._previousAttributes,h=!1,g;for(g in f){K.isEqual(e[g],f[g])||(h=h||{},h[g]=f[g])}return h},previous:function(e){if(!e||!this._previousAttributes){return null}return this._previousAttributes[e]},previousAttributes:function(){return K.clone(this._previousAttributes)},_performValidation:function(f,e){var g=this.validate(f);if(g){return e.error?e.error(this,g,e):this.trigger("error",this,g,e),!1}return !0}});L.Collection=function(f,e){e||(e={});if(e.comparator){this.comparator=e.comparator}K.bindAll(this,"_onModelEvent","_removeReference");this._reset();f&&this.reset(f,{silent:!0});this.initialize.apply(this,arguments)};K.extend(L.Collection.prototype,L.Events,{model:L.Model,initialize:function(){},toJSON:function(){return this.map(function(e){return e.toJSON()})},add:function(f,e){if(K.isArray(f)){for(var h=0,g=f.length;h<g;h++){this._add(f[h],e)}}else{this._add(f,e)}return this},remove:function(f,e){if(K.isArray(f)){for(var h=0,g=f.length;h<g;h++){this._remove(f[h],e)}}else{this._remove(f,e)}return this},get:function(e){if(e==null){return null}return this._byId[e.id!=null?e.id:e]},getByCid:function(e){return e&&this._byCid[e.cid||e]},at:function(e){return this.models[e]},sort:function(e){e||(e={});if(!this.comparator){throw Error("Cannot sort a set without a comparator")}this.models=this.sortBy(this.comparator);e.silent||this.trigger("reset",this,e);return this},pluck:function(e){return K.map(this.models,function(f){return f.get(e)})},reset:function(f,e){f||(f=[]);e||(e={});this.each(this._removeReference);this._reset();this.add(f,{silent:!0});e.silent||this.trigger("reset",this,e);return this},fetch:function(f){f||(f={});var e=this,g=f.success;f.success=function(j,h,i){e[f.add?"add":"reset"](e.parse(j,i),f);g&&g(e,j)};f.error=H(f.error,e,f);return(this.sync||L.sync).call(this,"read",this,f)},create:function(f,e){var h=this;e||(e={});f=this._prepareModel(f,e);if(!f){return !1}var g=e.success;e.success=function(i,k,j){h.add(i,e);g&&g(i,k,j)};f.save(null,e);return f},parse:function(e){return e},chain:function(){return K(this.models).chain()},_reset:function(){this.length=0;this.models=[];this._byId={};this._byCid={}},_prepareModel:function(f,e){if(f instanceof L.Model){if(!f.collection){f.collection=this}}else{var g=f;f=new this.model(g,{collection:this});f.validate&&!f._performValidation(g,e)&&(f=!1)}return f},_add:function(f,e){e||(e={});f=this._prepareModel(f,e);if(!f){return !1}var g=this.getByCid(f);if(g){throw Error(["Can't add the same model to a set twice",g.id])}this._byId[f.id]=f;this._byCid[f.cid]=f;this.models.splice(e.at!=null?e.at:this.comparator?this.sortedIndex(f,this.comparator):this.length,0,f);f.bind("all",this._onModelEvent);this.length++;e.silent||f.trigger("add",f,this,e);return f},_remove:function(f,e){e||(e={});f=this.getByCid(f)||this.get(f);if(!f){return null}delete this._byId[f.id];delete this._byCid[f.cid];this.models.splice(this.indexOf(f),1);this.length--;e.silent||f.trigger("remove",f,this,e);this._removeReference(f);return f},_removeReference:function(e){this==e.collection&&delete e.collection;e.unbind("all",this._onModelEvent)},_onModelEvent:function(f,e,h,g){(f=="add"||f=="remove")&&h!=this||(f=="destroy"&&this._remove(e,g),e&&f==="change:"+e.idAttribute&&(delete this._byId[e.previous(e.idAttribute)],this._byId[e.id]=e),this.trigger.apply(this,arguments))}});K.each(["forEach","each","map","reduce","reduceRight","find","detect","filter","select","reject","every","all","some","any","include","contains","invoke","max","min","sortBy","sortedIndex","toArray","size","first","rest","last","without","indexOf","lastIndexOf","isEmpty","groupBy"],function(e){L.Collection.prototype[e]=function(){return K[e].apply(K,[this.models].concat(K.toArray(arguments)))}});L.Router=function(e){e||(e={});if(e.routes){this.routes=e.routes}this._bindRoutes();this.initialize.apply(this,arguments)};var z=/:([\w\d]+)/g,y=/\*([\w\d]+)/g,x=/[-[\]{}()+?.,\\^$|#\s]/g;K.extend(L.Router.prototype,L.Events,{initialize:function(){},route:function(f,e,g){L.history||(L.history=new L.History);K.isRegExp(f)||(f=this._routeToRegExp(f));L.history.route(f,K.bind(function(h){h=this._extractParameters(f,h);g.apply(this,h);this.trigger.apply(this,["route:"+e].concat(h))},this))},navigate:function(f,e){L.history.navigate(f,e)},_bindRoutes:function(){if(this.routes){var f=[],e;for(e in this.routes){f.unshift([e,this.routes[e]])}e=0;for(var g=f.length;e<g;e++){this.route(f[e][0],f[e][1],this[f[e][1]])}}},_routeToRegExp:function(e){e=e.replace(x,"\\$&").replace(z,"([^/]*)").replace(y,"(.*?)");return RegExp("^"+e+"$")},_extractParameters:function(f,e){return f.exec(e).slice(1)}});L.History=function(){this.handlers=[];K.bindAll(this,"checkUrl")};var G=/^#*/,d=/msie [\w.]+/,D=!1;K.extend(L.History.prototype,{interval:50,getFragment:function(f,e){if(f==null){if(this._hasPushState||e){f=window.location.pathname;var g=window.location.search;g&&(f+=g);f.indexOf(this.options.root)==0&&(f=f.substr(this.options.root.length))}else{f=window.location.hash}}return decodeURIComponent(f.replace(G,""))},start:function(f){if(D){throw Error("Backbone.history has already been started")}this.options=K.extend({},{root:"/"},this.options,f);this._wantsPushState=!!this.options.pushState;this._hasPushState=!(!this.options.pushState||!window.history||!window.history.pushState);f=this.getFragment();var e=document.documentMode;if(e=d.exec(navigator.userAgent.toLowerCase())&&(!e||e<=7)){this.iframe=J('<iframe src="javascript:0" tabindex="-1" />').hide().appendTo("body")[0].contentWindow,this.navigate(f)}this._hasPushState?J(window).bind("popstate",this.checkUrl):"onhashchange" in window&&!e?J(window).bind("hashchange",this.checkUrl):setInterval(this.checkUrl,this.interval);this.fragment=f;D=!0;f=window.location;e=f.pathname==this.options.root;if(this._wantsPushState&&!this._hasPushState&&!e){return this.fragment=this.getFragment(null,!0),window.location.replace(this.options.root+"#"+this.fragment),!0}else{if(this._wantsPushState&&this._hasPushState&&e&&f.hash){this.fragment=f.hash.replace(G,""),window.history.replaceState({},document.title,f.protocol+"//"+f.host+this.options.root+this.fragment)}}if(!this.options.silent){return this.loadUrl()}},route:function(f,e){this.handlers.unshift({route:f,callback:e})},checkUrl:function(){var e=this.getFragment();e==this.fragment&&this.iframe&&(e=this.getFragment(this.iframe.location.hash));if(e==this.fragment||e==decodeURIComponent(this.fragment)){return !1}this.iframe&&this.navigate(e);this.loadUrl()||this.loadUrl(window.location.hash)},loadUrl:function(f){var e=this.fragment=this.getFragment(f);return K.any(this.handlers,function(g){if(g.route.test(e)){return g.callback(e),!0}})},navigate:function(f,e){var h=(f||"").replace(G,"");if(!(this.fragment==h||this.fragment==decodeURIComponent(h))){if(this._hasPushState){var g=window.location;h.indexOf(this.options.root)!=0&&(h=this.options.root+h);this.fragment=h;window.history.pushState({},document.title,g.protocol+"//"+g.host+h)}else{if(window.location.hash=this.fragment=h,this.iframe&&h!=this.getFragment(this.iframe.location.hash)){this.iframe.document.open().close(),this.iframe.location.hash=h}}e&&this.loadUrl(f)}}});L.View=function(e){this.cid=K.uniqueId("view");this._configure(e||{});this._ensureElement();this.delegateEvents();this.initialize.apply(this,arguments)};var c=/^(\S+)\s*(.*)$/,C=["model","collection","el","id","attributes","className","tagName"];K.extend(L.View.prototype,L.Events,{tagName:"div",$:function(e){return J(e,this.el)},initialize:function(){},render:function(){return this},remove:function(){J(this.el).remove();return this},make:function(f,e,g){f=document.createElement(f);e&&J(f).attr(e);g&&J(f).html(g);return f},delegateEvents:function(g){if(g||(g=this.events)){for(var f in K.isFunction(g)&&(g=g.call(this)),J(this.el).unbind(".delegateEvents"+this.cid),g){var j=this[g[f]];if(!j){throw Error('Event "'+g[f]+'" does not exist')}var i=f.match(c),h=i[1];i=i[2];j=K.bind(j,this);h+=".delegateEvents"+this.cid;i===""?J(this.el).bind(h,j):J(this.el).delegate(i,h,j)}}},_configure:function(f){this.options&&(f=K.extend({},this.options,f));for(var e=0,h=C.length;e<h;e++){var g=C[e];f[g]&&(this[g]=f[g])}this.options=f},_ensureElement:function(){if(this.el){if(K.isString(this.el)){this.el=J(this.el).get(0)}}else{var e=this.attributes||{};if(this.id){e.id=this.id}if(this.className){e["class"]=this.className}this.el=this.make(this.tagName,e)}}});L.Model.extend=L.Collection.extend=L.Router.extend=L.View.extend=function(f,e){var g=b(this,f,e);g.extend=this.extend;return g};var a={create:"POST",update:"PUT","delete":"DELETE",read:"GET"};L.sync=function(f,e,h){var g=a[f];h=K.extend({type:g,dataType:"json"},h);if(!h.url){h.url=F(e)||E()}if(!h.data&&e&&(f=="create"||f=="update")){h.contentType="application/json",h.data=JSON.stringify(e.toJSON())}if(L.emulateJSON){h.contentType="application/x-www-form-urlencoded",h.data=h.data?{model:h.data}:{}}if(L.emulateHTTP&&(g==="PUT"||g==="DELETE")){if(L.emulateJSON){h.data._method=g}h.type="POST";h.beforeSend=function(i){i.setRequestHeader("X-HTTP-Method-Override",g)}}if(h.type!=="GET"&&!L.emulateJSON){h.processData=!1}return J.ajax(h)};var B=function(){},b=function(f,e,h){var g;g=e&&e.hasOwnProperty("constructor")?e.constructor:function(){return f.apply(this,arguments)};K.extend(g,f);B.prototype=f.prototype;g.prototype=new B;e&&K.extend(g.prototype,e);h&&K.extend(g,h);g.prototype.constructor=g;g.__super__=f.prototype;return g},F=function(e){if(!e||!e.url){return null}return K.isFunction(e.url)?e.url():e.url},E=function(){throw Error('A "url" property or function must be specified')},H=function(f,e,g){return function(h){f?f(e,h,g):e.trigger("error",e,h,g)}}}).call(this);
