(function(){angular.module("ModelCore",["ng"]).factory("ModelCore",function(e,b,d,a){var c=function c(f){this.$init.apply(this,arguments)};c.prototype={$uuid:null,$pkField:null,$settings:{},$mapping:{},$hasMany:{},$hasOne:{},$dataset:[],$scope:null,$init:function(g){var f=this;f.$settings=angular.extend({headers:{"Content-Type":"application/json"},dataField:{one:"content",many:"items"},urls:{base:null}},f.$settings);f.$uuid=c.getUUID()},$offline:false,$resetTo:function(h){var f=this;h=!h||{};for(var g in f.$mapping){if(f.$mapping.hasOwnProperty(g)){f.__proto__[g]=typeof h[g]!=="undefined"?h[g]:null}}return f},$reset:function(){return this.$resetTo({})},$add:function(f){this.$dataset.push(f)},$new:function(g){var f=this;g=typeof g==="undefined"?f.$mapping:g;var h=new new c.newInstance({},f);h._cache={};for(i in f.$mapping){h._cache[i]=c.clone(f.$mapping[i]);h[i]=c.clone(f.$mapping[i])}return h},$fetch:function(){return this.next()},$incremental:function(g,f){return this.$find(g,f,true)},$find:function(h,g,j){var f=this;j=!(typeof j==="undefined"||j==false);return f.$call({url:f.$url("get"),method:"GET"},f,h).success(function(k){if(j!==true){f.$dataset=[]}return f.$parse(k)})},$getRelationship:function(j,l,h){var g=this;for(var f in g.$hasMany){if(f===j){g[f]=new g.$mapping[f]();l=l||{};for(var k in g.$hasMany[f]){l[k]=g[g.$hasMany[f][k]]}return g[f].$find(l,h)}}for(var f in g.$hasOne){if(f===j){g[f]=g.$mapping[f]();var n=g[g.$hasMany[f].id];var m=g.$hasMany[f].field||null;return g[f].$get(n,m)}}},$get:function(j,h){var f=this;if(f.$pkField==null&&typeof h==="undefined"){throw"You must setup a model.$pkField to perform this operation or define the second parameter field"}h=typeof h!=="undefined"?h:f.$pkField;var g={};g[h]=j;return f.$call({url:f.$url("get",g),method:"GET"},f).success(function(k){f.$dataset=[];f.$parse(k);f.$fetch();f.$dataset=f.$dataset.length===1?f.$dataset:d("filter")(f.$dataset,function(l){return l[h]==j?l:false});return f.$dataset})},$save:function(){var f=this;return f.$call({url:f.$url("post"),method:"POST",data:f.$toObject()},f).success(function(){f.$cleanDiff()})},$cleanDiff:function(){var f=this;if(typeof f._cache==="undefined"){f._cache={}}var g=f.$toObject();for(var h in g){if(g.hasOwnProperty(h)){if(typeof g[h]==="object"){f._cache[h]=c.clone(g[h])}else{f._cache[h]=g[h]}}}},$delete:function(j,h){var f=this;if(f.$pkField==null&&typeof h==="undefined"){throw"You must setup a model.$pkField to perform this operation or define the second parameter field"}h=typeof h!=="undefined"?h:f.$pkField;j=typeof j==="undefined"?f[h]:j;var g={};g[h]=j;return f.$call({url:f.$url("delete",g),method:"DELETE",data:f.$toObject()},f).success(function(){c.destroy(f)})},$toObject:function(){var f=this;var h;var g={};for(h in f.$mapping){if(f.$mapping.hasOwnProperty(h)){g[h]=f[h]}}return g},$original:function(){var f=this;var h;var g={};if(typeof f._cache==="undefined"){f._cache={};return false}for(h in f.$mapping){if(f.$mapping.hasOwnProperty(h)){if(typeof g[h]!=="object"){g[h]=f._cache[h]}else{g[h]=angular.extend(g[h],f._cache[h])}}}return g},$isChanged:function(h){var f=this;var g=f.$diff();if(typeof h==="undefined"&&g!==false){return Object.keys(g).length>0}else{if(g[h]){return !!g[h]}else{return false}}},$diff:function(j,g){var f=this;var h={};j=typeof j==="undefined"?f.$original():j;g=typeof g==="undefined"?f.$toObject():g;if(j===false){return false}for(var l in g){if(g.hasOwnProperty(l)){if(!j||j[l]!==g[l]){if(typeof g[l]==="object"&&g[l]!==null&&typeof j[l]!=="undefined"){var k=f.$diff(j[l],g[l]);if(!c.isEmpty(k)){h[l]=k}}else{h[l]=g[l]}}}}return h},$call:function(h,g,j){var f=this;return !f.$offline?c.call(h,g,j):c.callOffline(h,g,j)},$parse:function(g){var f=this;return f.$dataset=f.$dataset.concat(c.parse(g,f))},__cursor:0,next:function(){var f=this;var g=f.__cursor;f.__cursor++;return f.$goTo(g)},prev:function(){var f=this;var g=f.__cursor;f.__cursor--;return f.$goTo(g)},$goTo:function(g){var f=this;if(f.__cursor>f.$dataset.length){f.__cursor=0;return false}else{if(f.__cursor<0){f.__cursor=0;return false}}f.$mapping=f.$dataset[g].$mapping;if(typeof f._cache==="undefined"){f._cache={}}f._cache=f.$dataset[g]._cache;for(var h in f.$dataset[g].$toObject()){f[h]=f.$dataset[g][h];f.__proto__[h]=f.$dataset[g][h]}return f.$dataset[g]},$url:function(f,m){var n=this;var h=(typeof n.$settings.urls[f]==="undefined"||n.$settings.urls[f]==null)?n.$settings.urls.base:n.$settings.urls[f];if(h==null||typeof h==="undefined"){throw ("Model URL missing for "+typeof n+" in ["+f+"].")}var g=/(:[A-z]+\/?)/g;var o=h.match(g);var k;for(k in o){if(o.hasOwnProperty(k)){var j=o[k].replace("/","");var l=j.replace(":","");if(!m||typeof m[l]==="undefined"){h=h.replace(o[k],"")}else{h=h.replace(j,m[l])}}}return h}};c.destroy=function(g){if(typeof g.$parentModel!=="undefined"){var f=g.$parentModel.$dataset.indexOf(g);g.$parentModel.$dataset.splice(f,1)}};c.OfflineSettings={key:"_ModelCoreOfflineData"};c.call=function(h,g,j){var f=this;if(typeof h==="undefined"){h={}}if(!h.method){throw"You must setup a method"}if(!h.url){throw"You must setup an url"}if(j){h.params=j}h.headers=g.$settings.headers;return e(h)};c.__call=function(j,h,m){var g=b.defer();var k=c.OfflineSettings.key;if(typeof Storage!=="undefined"){var l={calls:[]};var f=sessionStorage.getItem(k);if(typeof f==="undefined"||f==null){sessionStorage.setItem(k,"{}");f=sessionStorage.getItem(k)}f=JSON.parse(f);l.calls[j.method].push({options:j,uuid:h.$uuid});angular.extend(f,l);sessionStorage.setItem(k,JSON.stringify(l));g.resolve(h)}else{g.reject("Error processing offline call: "+l.toString())}return g};c.newInstance=function(g,f){return c.instance(g,f)};c.parse=function(l,g){var j=l[g.$settings.dataField.one]?[l[g.$settings.dataField.one]]:l[g.$settings.dataField.many];var h;var f=this;var k=function(o){angular.extend(o,g);return c.instance(o,g)};var n=[];for(h in j){if(j.hasOwnProperty(h)){var m;j[h].$mapping={};if(typeof g.$mapping!=="undefined"){angular.extend(j[h].$mapping,g.$mapping)}j[h]._cache={};for(m in j[h]){if(j[h].hasOwnProperty(m)){if(m!="$mapping"&&m!="_cache"){j[h].$mapping[m]=true;if(typeof j[h][m]==="object"){j[h]._cache[m]=c.clone(j[h][m])}else{j[h]._cache[m]=j[h][m]}}}}j[h].$parentModel=g;n.push(new new c.newInstance(j[h],g))}}return n};c.clone=function(j){if(null==j||"object"!==typeof j){return j}if(j instanceof Date){var k=new Date();k.setTime(j.getTime());return k}if(j instanceof Array){var k=[];for(var h=0,g=j.length;h