
(function(){var Backbone;if(typeof exports!=='undefined'){Backbone=exports;}else{Backbone=this.Backbone={};}
Backbone.VERSION='0.3.3';var _=this._;if(!_&&(typeof require!=='undefined'))_=require("underscore")._;var $=this.jQuery||this.Zepto;Backbone.emulateHTTP=false;Backbone.emulateJSON=false;Backbone.Events={bind:function(ev,callback){var calls=this._callbacks||(this._callbacks={});var list=this._callbacks[ev]||(this._callbacks[ev]=[]);list.push(callback);return this;},unbind:function(ev,callback){var calls;if(!ev){this._callbacks={};}else if(calls=this._callbacks){if(!callback){calls[ev]=[];}else{var list=calls[ev];if(!list)return this;for(var i=0,l=list.length;i<l;i++){if(callback===list[i]){list.splice(i,1);break;}}}}
return this;},trigger:function(ev){var list,calls,i,l;if(!(calls=this._callbacks))return this;if(list=calls[ev]){for(i=0,l=list.length;i<l;i++){list[i].apply(this,Array.prototype.slice.call(arguments,1));}}
if(list=calls['all']){for(i=0,l=list.length;i<l;i++){list[i].apply(this,arguments);}}
return this;}};Backbone.Model=function(attributes,options){attributes||(attributes={});if(this.defaults)attributes=_.extend({},this.defaults,attributes);this.attributes={};this._escapedAttributes={};this.cid=_.uniqueId('c');this.set(attributes,{silent:true});this._previousAttributes=_.clone(this.attributes);if(options&&options.collection)this.collection=options.collection;this.initialize(attributes,options);};_.extend(Backbone.Model.prototype,Backbone.Events,{_previousAttributes:null,_changed:false,initialize:function(){},toJSON:function(){return _.clone(this.attributes);},get:function(attr){return this.attributes[attr];},escape:function(attr){var html;if(html=this._escapedAttributes[attr])return html;var val=this.attributes[attr];return this._escapedAttributes[attr]=escapeHTML(val==null?'':val);},set:function(attrs,options){options||(options={});if(!attrs)return this;if(attrs.attributes)attrs=attrs.attributes;var now=this.attributes,escaped=this._escapedAttributes;if(!options.silent&&this.validate&&!this._performValidation(attrs,options))return false;if('id'in attrs)this.id=attrs.id;for(var attr in attrs){var val=attrs[attr];if(!_.isEqual(now[attr],val)){now[attr]=val;delete escaped[attr];if(!options.silent){this._changed=true;this.trigger('change:'+attr,this,val,options);}}}
if(!options.silent&&this._changed)this.change(options);return this;},unset:function(attr,options){options||(options={});var value=this.attributes[attr];var validObj={};validObj[attr]=void 0;if(!options.silent&&this.validate&&!this._performValidation(validObj,options))return false;delete this.attributes[attr];delete this._escapedAttributes[attr];if(!options.silent){this._changed=true;this.trigger('change:'+attr,this,void 0,options);this.change(options);}
return this;},clear:function(options){options||(options={});var old=this.attributes;var validObj={};for(attr in old)validObj[attr]=void 0;if(!options.silent&&this.validate&&!this._performValidation(validObj,options))return false;this.attributes={};this._escapedAttributes={};if(!options.silent){this._changed=true;for(attr in old){this.trigger('change:'+attr,this,void 0,options);}
this.change(options);}
return this;},fetch:function(options){options||(options={});var model=this;var success=function(resp){if(!model.set(model.parse(resp),options))return false;if(options.success)options.success(model,resp);};var error=wrapError(options.error,model,options);(this.sync||Backbone.sync)('read',this,success,error);return this;},save:function(attrs,options){options||(options={});if(attrs&&!this.set(attrs,options))return false;var model=this;var success=function(resp){if(!model.set(model.parse(resp),options))return false;if(options.success)options.success(model,resp);};var error=wrapError(options.error,model,options);var method=this.isNew()?'create':'update';(this.sync||Backbone.sync)(method,this,success,error);return this;},destroy:function(options){options||(options={});var model=this;var success=function(resp){if(model.collection)model.collection.remove(model);if(options.success)options.success(model,resp);};var error=wrapError(options.error,model,options);(this.sync||Backbone.sync)('delete',this,success,error);return this;},url:function(){var base=getUrl(this.collection);if(this.isNew())return base;return base+(base.charAt(base.length-1)=='/'?'':'/')+this.id;},parse:function(resp){return resp;},clone:function(){return new this.constructor(this);},isNew:function(){return!this.id;},change:function(options){this.trigger('change',this,options);this._previousAttributes=_.clone(this.attributes);this._changed=false;},hasChanged:function(attr){if(attr)return this._previousAttributes[attr]!=this.attributes[attr];return this._changed;},changedAttributes:function(now){now||(now=this.attributes);var old=this._previousAttributes;var changed=false;for(var attr in now){if(!_.isEqual(old[attr],now[attr])){changed=changed||{};changed[attr]=now[attr];}}
return changed;},previous:function(attr){if(!attr||!this._previousAttributes)return null;return this._previousAttributes[attr];},previousAttributes:function(){return _.clone(this._previousAttributes);},_performValidation:function(attrs,options){var error=this.validate(attrs);if(error){if(options.error){options.error(this,error);}else{this.trigger('error',this,error,options);}
return false;}
return true;}});Backbone.Collection=function(models,options){options||(options={});if(options.comparator){this.comparator=options.comparator;delete options.comparator;}
this._boundOnModelEvent=_.bind(this._onModelEvent,this);this._reset();if(models)this.refresh(models,{silent:true});this.initialize(models,options);};_.extend(Backbone.Collection.prototype,Backbone.Events,{model:Backbone.Model,initialize:function(){},toJSON:function(){return this.map(function(model){return model.toJSON();});},add:function(models,options){if(_.isArray(models)){for(var i=0,l=models.length;i<l;i++){this._add(models[i],options);}}else{this._add(models,options);}
return this;},remove:function(models,options){if(_.isArray(models)){for(var i=0,l=models.length;i<l;i++){this._remove(models[i],options);}}else{this._remove(models,options);}
return this;},get:function(id){if(id==null)return null;return this._byId[id.id!=null?id.id:id];},getByCid:function(cid){return cid&&this._byCid[cid.cid||cid];},at:function(index){return this.models[index];},sort:function(options){options||(options={});if(!this.comparator)throw new Error('Cannot sort a set without a comparator');this.models=this.sortBy(this.comparator);if(!options.silent)this.trigger('refresh',this,options);return this;},pluck:function(attr){return _.map(this.models,function(model){return model.get(attr);});},refresh:function(models,options){models||(models=[]);options||(options={});this._reset();this.add(models,{silent:true});if(!options.silent)this.trigger('refresh',this,options);return this;},fetch:function(options){options||(options={});var collection=this;var success=function(resp){collection.refresh(collection.parse(resp));if(options.success)options.success(collection,resp);};var error=wrapError(options.error,collection,options);(this.sync||Backbone.sync)('read',this,success,error);return this;},create:function(model,options){var coll=this;options||(options={});if(!(model instanceof Backbone.Model)){model=new this.model(model,{collection:coll});}else{model.collection=coll;}
var success=function(nextModel,resp){coll.add(nextModel);if(options.success)options.success(nextModel,resp);};return model.save(null,{success:success,error:options.error});},parse:function(resp){return resp;},chain:function(){return _(this.models).chain();},_reset:function(options){this.length=0;this.models=[];this._byId={};this._byCid={};},_add:function(model,options){options||(options={});if(!(model instanceof Backbone.Model)){model=new this.model(model,{collection:this});}
var already=this.getByCid(model);if(already)throw new Error(["Can't add the same model to a set twice",already.id]);this._byId[model.id]=model;this._byCid[model.cid]=model;model.collection=this;var index=this.comparator?this.sortedIndex(model,this.comparator):this.length;this.models.splice(index,0,model);model.bind('all',this._boundOnModelEvent);this.length++;if(!options.silent)model.trigger('add',model,this,options);return model;},_remove:function(model,options){options||(options={});model=this.getByCid(model)||this.get(model);if(!model)return null;delete this._byId[model.id];delete this._byCid[model.cid];delete model.collection;this.models.splice(this.indexOf(model),1);this.length--;if(!options.silent)model.trigger('remove',model,this,options);model.unbind('all',this._boundOnModelEvent);return model;},_onModelEvent:function(ev,model){if(ev==='change:id'){delete this._byId[model.previous('id')];this._byId[model.id]=model;}
this.trigger.apply(this,arguments);}});var methods=['forEach','each','map','reduce','reduceRight','find','detect','filter','select','reject','every','all','some','any','include','invoke','max','min','sortBy','sortedIndex','toArray','size','first','rest','last','without','indexOf','lastIndexOf','isEmpty'];_.each(methods,function(method){Backbone.Collection.prototype[method]=function(){return _[method].apply(_,[this.models].concat(_.toArray(arguments)));};});Backbone.Controller=function(options){options||(options={});if(options.routes)this.routes=options.routes;this._bindRoutes();this.initialize(options);};var namedParam=/:([\w\d]+)/g;var splatParam=/\*([\w\d]+)/g;_.extend(Backbone.Controller.prototype,Backbone.Events,{initialize:function(){},route:function(route,name,callback){Backbone.history||(Backbone.history=new Backbone.History);if(!_.isRegExp(route))route=this._routeToRegExp(route);Backbone.history.route(route,_.bind(function(fragment){var args=this._extractParameters(route,fragment);callback.apply(this,args);this.trigger.apply(this,['route:'+name].concat(args));},this));},saveLocation:function(fragment){Backbone.history.saveLocation(fragment);},_bindRoutes:function(){if(!this.routes)return;for(var route in this.routes){var name=this.routes[route];this.route(route,name,this[name]);}},_routeToRegExp:function(route){route=route.replace(namedParam,"([^\/]*)").replace(splatParam,"(.*?)");return new RegExp('^'+route+'$');},_extractParameters:function(route,fragment){return route.exec(fragment).slice(1);}});Backbone.History=function(){this.handlers=[];this.fragment=this.getFragment();_.bindAll(this,'checkUrl');};var hashStrip=/^#*/;_.extend(Backbone.History.prototype,{interval:50,getFragment:function(loc){return(loc||window.location).hash.replace(hashStrip,'');},start:function(){var docMode=document.documentMode;var oldIE=($.browser.msie&&(!docMode||docMode<=7));if(oldIE){this.iframe=$('<iframe src="javascript:0" tabindex="-1" />').hide().appendTo('body')[0].contentWindow;}
if('onhashchange'in window&&!oldIE){$(window).bind('hashchange',this.checkUrl);}else{setInterval(this.checkUrl,this.interval);}
return this.loadUrl();},route:function(route,callback){this.handlers.push({route:route,callback:callback});},checkUrl:function(){var current=this.getFragment();if(current==this.fragment&&this.iframe){current=this.getFragment(this.iframe.location);}
if(current==this.fragment||current==decodeURIComponent(this.fragment))return false;if(this.iframe){window.location.hash=this.iframe.location.hash=current;}
this.loadUrl();},loadUrl:function(){var fragment=this.fragment=this.getFragment();var matched=_.any(this.handlers,function(handler){if(handler.route.test(fragment)){handler.callback(fragment);return true;}});return matched;},saveLocation:function(fragment){fragment=(fragment||'').replace(hashStrip,'');if(this.fragment==fragment)return;window.location.hash=this.fragment=fragment;if(this.iframe&&(fragment!=this.getFragment(this.iframe.location))){this.iframe.document.open().close();this.iframe.location.hash=fragment;}}});Backbone.View=function(options){this._configure(options||{});this._ensureElement();this.delegateEvents();this.initialize(options);};var selectorDelegate=function(selector){return $(selector,this.el);};var eventSplitter=/^(\w+)\s*(.*)$/;_.extend(Backbone.View.prototype,Backbone.Events,{tagName:'div',$:selectorDelegate,initialize:function(){},render:function(){return this;},remove:function(){$(this.el).remove();return this;},make:function(tagName,attributes,content){var el=document.createElement(tagName);if(attributes)$(el).attr(attributes);if(content)$(el).html(content);return el;},delegateEvents:function(events){if(!(events||(events=this.events)))return;$(this.el).unbind();for(var key in events){var methodName=events[key];var match=key.match(eventSplitter);var eventName=match[1],selector=match[2];var method=_.bind(this[methodName],this);if(selector===''){$(this.el).bind(eventName,method);}else{$(this.el).delegate(selector,eventName,method);}}},_configure:function(options){if(this.options)options=_.extend({},this.options,options);if(options.model)this.model=options.model;if(options.collection)this.collection=options.collection;if(options.el)this.el=options.el;if(options.id)this.id=options.id;if(options.className)this.className=options.className;if(options.tagName)this.tagName=options.tagName;this.options=options;},_ensureElement:function(){if(this.el)return;var attrs={};if(this.id)attrs.id=this.id;if(this.className)attrs["class"]=this.className;this.el=this.make(this.tagName,attrs);}});var extend=function(protoProps,classProps){var child=inherits(this,protoProps,classProps);child.extend=extend;return child;};Backbone.Model.extend=Backbone.Collection.extend=Backbone.Controller.extend=Backbone.View.extend=extend;var methodMap={'create':'POST','update':'PUT','delete':'DELETE','read':'GET'};Backbone.sync=function(method,model,success,error){var type=methodMap[method];var modelJSON=(method==='create'||method==='update')?JSON.stringify(model.toJSON()):null;var params={url:getUrl(model),type:type,contentType:'application/json',data:modelJSON,dataType:'json',processData:false,success:success,error:error};if(Backbone.emulateJSON){params.contentType='application/x-www-form-urlencoded';params.processData=true;params.data=modelJSON?{model:modelJSON}:{};}
if(Backbone.emulateHTTP){if(type==='PUT'||type==='DELETE'){if(Backbone.emulateJSON)params.data._method=type;params.type='POST';params.beforeSend=function(xhr){xhr.setRequestHeader("X-HTTP-Method-Override",type);};}}
$.ajax(params);};var ctor=function(){};var inherits=function(parent,protoProps,staticProps){var child;if(protoProps&&protoProps.hasOwnProperty('constructor')){child=protoProps.constructor;}else{child=function(){return parent.apply(this,arguments);};}
ctor.prototype=parent.prototype;child.prototype=new ctor();if(protoProps)_.extend(child.prototype,protoProps);if(staticProps)_.extend(child,staticProps);child.prototype.constructor=child;child.__super__=parent.prototype;return child;};var getUrl=function(object){if(!(object&&object.url))throw new Error("A 'url' property or function must be specified");return _.isFunction(object.url)?object.url():object.url;};var wrapError=function(onError,model,options){return function(resp){if(onError){onError(model,resp);}else{model.trigger('error',model,resp,options);}};};var escapeHTML=function(string){return string.replace(/&(?!\w+;)/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/"/g,'&quot;');};})();
