/*  js-model JavaScript library, version 0.10.1
 *  (c) 2010-2011 Ben Pickles
 *
 *  Released under MIT license.
 */
var Model=function(a,c){var b=function(e){this.attributes=jQuery.extend({},e);this.changes={};this.errors=new Model.Errors(this);this.uid=[a,Model.UID.generate()].join("-");jQuery.isFunction(this.initialize)&&this.initialize()};Model.Module.extend.call(b,Model.Module);b._name=a;b.collection=[];b.unique_key="id";b.extend(Model.Callbacks).extend(Model.ClassMethods);b.prototype=new Model.Base;b.prototype.constructor=b;jQuery.isFunction(c)&&c.call(b,b,b.prototype);return b};
Model.Callbacks={bind:function(a,c){this.callbacks=this.callbacks||{};this.callbacks[a]=this.callbacks[a]||[];this.callbacks[a].push(c);return this},trigger:function(a,c){this.callbacks=this.callbacks||{};var b=this.callbacks[a];if(b)for(var e=0;e<b.length;e++)b[e].apply(this,c||[]);return this},unbind:function(a,c){this.callbacks=this.callbacks||{};if(c)for(var b=this.callbacks[a]||[],e=0;e<b.length;e++)b[e]===c&&this.callbacks[a].splice(e,1);else delete this.callbacks[a];return this}};
Model.ClassMethods={add:function(a){var c=a.id();if(jQuery.inArray(a,this.collection)===-1&&!(c&&this.find(c))){this.collection.push(a);this.trigger("add",[a])}return this},all:function(){return this.collection.slice()},chain:function(a){return jQuery.extend({},this,{collection:a})},count:function(){return this.all().length},detect:function(a){for(var c=this.all(),b,e=0,f=c.length;e<f;e++){b=c[e];if(a.call(b,b,e))return b}},each:function(a,c){for(var b=this.all(),e=0,f=b.length;e<f;e++)a.call(c||
b[e],b[e],e,b);return this},find:function(a){return this.detect(function(){return this.id()==a})},first:function(){return this.all()[0]},load:function(a){if(this._persistence){var c=this;this._persistence.read(function(b){for(var e=0,f=b.length;e<f;e++)c.add(b[e]);a&&a.call(c,b)})}return this},last:function(){var a=this.all();return a[a.length-1]},map:function(a,c){for(var b=this.all(),e=[],f=0,d=b.length;f<d;f++)e.push(a.call(c||b[f],b[f],f,b));return e},persistence:function(a){if(arguments.length==
0)return this._persistence;else{var c=Array.prototype.slice.call(arguments,1);c.unshift(this);this._persistence=a.apply(a,c);return this}},pluck:function(a){for(var c=this.all(),b=[],e=0,f=c.length;e<f;e++)b.push(c[e].attr(a));return b},remove:function(a){for(var c,b=0,e=this.collection.length;b<e;b++)if(this.collection[b]===a){c=b;break}if(c!=undefined){this.collection.splice(c,1);this.trigger("remove",[a]);return true}else return false},reverse:function(){return this.chain(this.all().reverse())},
select:function(a,c){for(var b=this.all(),e=[],f,d=0,g=b.length;d<g;d++){f=b[d];if(a.call(c||f,f,d,b))e.push(f)}return this.chain(e)},sort:function(a){return this.chain(this.all().sort(a))},sortBy:function(a){var c=jQuery.isFunction(a);return this.sort(function(b,e){var f=c?a.call(b):b.attr(a),d=c?a.call(e):e.attr(a);return f<d?-1:f>d?1:0})},use:function(a){var c=Array.prototype.slice.call(arguments,1);c.unshift(this);a.apply(this,c);return this}};
Model.Errors=function(a){this.errors={};this.model=a};Model.Errors.prototype={add:function(a,c){this.errors[a]||(this.errors[a]=[]);this.errors[a].push(c);return this},all:function(){return this.errors},clear:function(){this.errors={};return this},each:function(a){for(var c in this.errors)for(var b=0;b<this.errors[c].length;b++)a.call(this,c,this.errors[c][b]);return this},on:function(a){return this.errors[a]||[]},size:function(){var a=0;this.each(function(){a++});return a}};
Model.InstanceMethods={asJSON:function(){return this.attr()},attr:function(a,c){if(arguments.length===0)return jQuery.extend({},this.attributes,this.changes);else if(arguments.length===2){if(this.attributes[a]===c)delete this.changes[a];else this.changes[a]=c;return this}else if(typeof a==="object"){for(var b in a)this.attr(b,a[b]);return this}else return a in this.changes?this.changes[a]:this.attributes[a]},callPersistMethod:function(a,c){var b=this,e=function(f){if(f){b.merge(b.changes).reset();
a==="destroy"?b.constructor.remove(b):b.constructor.add(b);b.trigger(a)}var d;if(c)d=c.apply(b,arguments);return d};if(this.constructor._persistence)this.constructor._persistence[a](this,e);else e.call(this,true)},destroy:function(a){this.callPersistMethod("destroy",a);return this},id:function(){return this.attributes[this.constructor.unique_key]},merge:function(a){jQuery.extend(this.attributes,a);return this},newRecord:function(){return this.id()===undefined},reset:function(){this.errors.clear();
this.changes={};return this},save:function(a){if(this.valid())this.callPersistMethod(this.newRecord()?"create":"update",a);else a&&a(false);return this},valid:function(){this.errors.clear();this.validate();return this.errors.size()===0},validate:function(){return this}};
Model.localStorage=function(a){if(!window.localStorage)return{create:function(f,d){d(true)},destroy:function(f,d){d(true)},read:function(f){f([])},update:function(f,d){d(true)}};var c=[a._name,"collection"].join("-"),b=function(){var f=localStorage[c];return f?JSON.parse(f):[]},e=function(f){localStorage.setItem(f.uid,JSON.stringify(f.asJSON()));f=f.uid;var d=b();if(jQuery.inArray(f,d)===-1){d.push(f);localStorage.setItem(c,JSON.stringify(d))}};return{create:function(f,d){e(f);d(true)},destroy:function(f,
d){localStorage.removeItem(f.uid);var g=f.uid,h=b();g=jQuery.inArray(g,h);if(g>-1){h.splice(g,1);localStorage.setItem(c,JSON.stringify(h))}d(true)},read:function(f){if(!f)return false;for(var d=a.map(function(){return this.uid}),g=b(),h=[],i,j,l=0,k=g.length;l<k;l++){j=g[l];if(jQuery.inArray(j,d)==-1){i=JSON.parse(localStorage[j]);i=new a(i);i.uid=j;h.push(i)}}f(h)},update:function(f,d){e(f);d(true)}}};Model.Log=function(){window.console&&window.console.log.apply(window.console,arguments)};
Model.Module={extend:function(a){jQuery.extend(this,a);return this},include:function(a){jQuery.extend(this.prototype,a);return this}};
Model.REST=function(a,c,b){var e=/:([\w\d]+)/g,f=function(){for(var d=[],g;(g=e.exec(c))!==null;)d.push(g[1]);return d}();return jQuery.extend({path:function(d){var g=c;jQuery.each(f,function(h,i){g=g.replace(":"+i,d.attributes[i])});return g},create:function(d,g){return this.xhr("POST",this.create_path(d),d,g)},create_path:function(d){return this.path(d)},destroy:function(d,g){return this.xhr("DELETE",this.destroy_path(d),d,g)},destroy_path:function(d){return this.update_path(d)},params:function(d){var g;
if(d){var h=d.asJSON();delete h[d.constructor.unique_key];g={};g[d.constructor._name.toLowerCase()]=h}else g=null;if(jQuery.ajaxSettings.data)g=jQuery.extend({},jQuery.ajaxSettings.data,g);return JSON.stringify(g)},read:function(d){return this.xhr("GET",this.read_path(),null,function(g,h,i){i=jQuery.makeArray(i);g=[];h=0;for(var j=i.length;h<j;h++)g.push(new a(i[h]));d(g)})},read_path:function(){return c},update:function(d,g){return this.xhr("PUT",this.update_path(d),d,g)},update_path:function(d){return[this.path(d),
d.id()].join("/")},xhr:function(d,g,h,i){var j=this,l=d=="GET"?undefined:this.params(h);return jQuery.ajax({type:d,url:g,contentType:"application/json",dataType:"json",data:l,dataFilter:function(k){return/\S/.test(k)?k:undefined},complete:function(k,m){j.xhrComplete(k,m,h,i)}})},xhrComplete:function(d,g,h,i){var j=Model.REST["handle"+d.status];j&&j.call(this,d,g,h);g=g==="success";j=Model.REST.parseResponseData(d);g&&h&&j&&h.attr(j);i&&i.call(h,g,d,j)}},b)};Model.RestPersistence=Model.REST;
Model.REST.handle422=function(a,c,b){if(a=Model.REST.parseResponseData(a)){b.errors.clear();for(var e in a)for(c=0;c<a[e].length;c++)b.errors.add(e,a[e][c])}};Model.REST.parseResponseData=function(a){try{return/\S/.test(a.responseText)?jQuery.parseJSON(a.responseText):undefined}catch(c){Model.Log(c)}};Model.UID={counter:0,generate:function(){return[(new Date).valueOf(),this.counter++].join("-")},reset:function(){this.counter=0;return this}};Model.VERSION="0.10.1";
Model.Base=function(){function a(){}a.prototype=jQuery.extend({},Model.Callbacks,Model.InstanceMethods);return a}();

