!function(a,b){var c={};c.Emitter=function(){},c.Emitter.extend=a.Model.extend,b.extend(c.Emitter,a.Events),b.extend(c.Emitter.prototype,a.Events),a.SE=c,c.Engine={},c.Engine.Base=c.Emitter.extend({sync:function(a,b,c){return this[a](b,c)}},{createSync:function(){var a,c;return a=this.construct.apply(this,arguments),c=b.bind(a.sync,a),c.engine=a,c},construct:function(){throw"`construct` factory method not implemented"}}),c.Engine.LocalStorage=function(a,b,d){function e(){function a(){return(65536*(1+Math.random())|0).toString(16).substring(1)}return a()+a()+"-"+a()+"-"+a()+"-"+a()+"-"+a()+a()+a()}var f=function(a){if(!this.localStorage)throw"Backbone.localStorage: Environment does not support localStorage.";this.name=a;var b=this.localStorage().getItem(this.name);this.records=b&&b.split(",")||[]};return b.extend(f.prototype,{save:function(){this.localStorage().setItem(this.name,this.records.join(","))},create:function(a){return a.id||(a.id=e(),a.set(a.idAttribute,a.id)),this.localStorage().setItem(this.name+"-"+a.id,JSON.stringify(a)),this.records.push(a.id.toString()),this.save(),this.find(a)},update:function(a){return this.localStorage().setItem(this.name+"-"+a.id,JSON.stringify(a)),b.include(this.records,a.id.toString())||this.records.push(a.id.toString()),this.save(),this.find(a)},find:function(a){return this.jsonData(this.localStorage().getItem(this.name+"-"+a.id))},findAll:function(){return(b.chain||b)(this.records).map(function(a){return this.jsonData(this.localStorage().getItem(this.name+"-"+a))},this).compact().value()},destroy:function(a){return a.isNew()?!1:(this.localStorage().removeItem(this.name+"-"+a.id),this.records=b.reject(this.records,function(b){return b===a.id.toString()}),this.save(),a)},localStorage:function(){return a.localStorage},jsonData:function(a){return a&&JSON.parse(a)},_clear:function(){var a=this.localStorage(),c=new RegExp("^"+this.name+"-");a.removeItem(this.name),(b.chain||b)(a).keys().filter(function(a){return c.test(a)}).each(function(b){a.removeItem(b)}),this.records.length=0},_storageSize:function(){return this.localStorage().length}}),c.Engine.Base.extend({constructor:function(a){c.Engine.Base.prototype.constructor.apply(this,arguments),this.store=new f(a)},sync:function(a,b,c){var e,f,g=this.store,h=d.$.Deferred&&d.$.Deferred();try{switch(a){case"read":e=void 0!==b.id?g.find(b):g.findAll();break;case"create":e=g.create(b);break;case"update":e=g.update(b);break;case"delete":e=g.destroy(b)}}catch(i){f=22===i.code&&0===g._storageSize()?"Private browsing is unsupported":i.message}return e?(c&&c.success&&("0.9.10"===d.VERSION?c.success(b,e,c):c.success(e)),h&&h.resolve(e)):(f=f||"Record Not Found",c&&c.error&&("0.9.10"===d.VERSION?c.error(b,f,c):c.error(f)),h&&h.reject(f)),c&&c.complete&&c.complete(e),h&&h.promise()}},{construct:b.memoize(function(a){return new this(a)})})}(this,b,a),c.Router={},c.Router.Base=c.Engine.Base,c.Router.LateBind=c.Router.Base.extend({constructor:function(a){this._cb=a},sync:function(a,c){var d=this._cb.apply(this,arguments);return c.sync=b.bind(d.sync,d),c.sync.apply(c,arguments)}},{construct:function(a){return new this(a)}}),c.Engine.ChromeStorage=function(a,b,d){function e(a){a=""+a||"local",chrome.storage[a]||(console.warn("Unknown type %s, defaulting to local",a),a="local"),this.type=a,this.storage=chrome.storage[this.type]}function f(a){return function(){var b=chrome.runtime.lastError;b?(console.warn("chromeStorage error: '%s'",b.message),a.reject(a,b.message,b)):a.resolve.apply(a,arguments)}}function g(a){return function(b){var c=n.Deferred();return"function"==typeof b&&c.done(b),this.storage[a](f(c)),c.promise()}}function h(a){return function(b,c){var d=n.Deferred();return"function"==typeof c&&d.done(c),this.storage[a](b,f(d)),d.promise()}}function i(a,c){b.bindAll(this),this.name=a,this.type=c||i.defaultType||"local",this.store=new e(this.type),this.loaded=this.store.get(this.name).pipe(this._parseRecords).done(function(a){this.records=a,chrome.storage.onChanged.addListener(this.updateRecords.bind(this))}.bind(this))}function j(a){return function(){return a.toJSON()}}function k(){return(65536*(1+Math.random())|0).toString(16).substring(1)}function l(){return k()+k()+"-"+k()+"-"+k()+"-"+k()+"-"+k()+k()+k()}function m(a){return"string"==typeof a?JSON.parse(a):a}var n=d.$;return b(e.prototype).extend({getBytesInUse:g("getBytesInUse"),clear:g("clear"),get:h("get"),set:h("set"),remove:h("remove"),getQuotaObject:function(){return b(this.storage).pick("QUOTA_BYTES","QUOTA_BYTES_PER_ITEM","MAX_ITEMS","MAX_SUSTAINED_WRITE_OPERATIONS_PER_MINUTE","MAX_WRITE_OPERATIONS_PER_HOUR")}}),i.defaultType="local",b(i.prototype).extend({updateRecords:function(a,b){var c=a[this.name];this._recordsChanged(c,b)&&(this.records=c.newValue)},_recordsChanged:function(a,c){return c===this.type&&a?!b.isEqual(a.newValue,this.records):!1},create:function(a){return a.id||(a.id=l(),a.set(a.idAttribute,a.id)),this.store.set(this._wrap(a),this._created.bind(this,a)).pipe(j(a))},_created:function(a){this.records.push(""+a.id),this.save()},update:function(a){return this.store.set(this._wrap(a),this._updated.bind(this,a)).pipe(j(a))},_updated:function(a){var c=""+a.id;b(this.records).include(c)||(this.records.push(c),this.save())},find:function(a){return this.store.get(this._wrap(a)).pipe(this._found.bind(this,a))},_found:function(a,b){return m(b[this._idOf(a)])},findAll:function(){var a=n.Deferred(),b=a.resolve.bind(a);return n.when(this.loaded).done(function(){var a=this._getRecordIds();this.store.get(a,b)}.bind(this)),a.pipe(this._foundAll)},_foundAll:function(a){return b(a).map(m)},destroy:function(a){return this.store.remove(this._idOf(a),this._destroyed.bind(this,a)).pipe(j(a))},_destroyed:function(a){this.records=b.without(this.records,a.id),this.save()},quota:function(){var a=this.store.getQuotaObject();return this.store.getBytesInUse().pipe(function(c){return b(a).extend({QUOTA_BYTES_IN_USE:c})})},save:function(){var a={};a[this.name]=this.records,this.store.set(a)},_getRecordIds:function(){return this.records.map(this._idOf)},_idOf:function(a){return this.name+"-"+(b.isString(a)?a:a.id)},_wrap:function(a){var c={};return c[this._idOf(a)]=b.isString(a)?a:a.toJSON(),c},_parseRecords:function(a){var c=a&&a[this.name]?a[this.name]:null;return b.isArray(c)?c:"string"==typeof c?(console.debug("[Backbone.ChromeStorage (%s / %s)] upgrading from stringified array of ids",this.type,this.name),c.split(",")):[]}}),c.Engine.Base.extend({constructor:function(a,b){c.Engine.Base.prototype.constructor.apply(this,arguments),this.store=new i(a,b)},sync:function(a,c,d,e){var f,g=this.store,h=b.isFunction;switch(a){case"read":f=null!==c.id?g.find(c):g.findAll();break;case"create":f=g.create(c);break;case"update":f=g.update(c);break;case"delete":f=g.destroy(c);break;default:var i=new Error('Unknown Method: "'+a+'"');f=n.Deferred(),f.reject(f,i.message,i)}return h(d.success)&&f.done(d.success),h(d.error)&&f.fail(d.error),h(e)&&f.fail(d.error),f&&f.promise()}},{construct:b.memoize(function(a,b){return new this(a,b)},function(a,b){return a+b})})}(this,b,a)}(Backbone,_);