(function(n,t){var i=nspace("kidscount.routes");i.ModelMapper=function(n){if(t.isUndefined(n))throw"ModelMapper requires settings.";this.settings=n},i.ModelMapper.prototype.map=function(i){var u=this.settings,r=new Backbone.Model;return t.each(t.keys(u),function(f){var o=u[f],e,s,h;(o.value?e=o.value:(e=i.get(f),t.isArray(e)?e=e.slice(0):t.isObject(e)&&(e=n.extend(!0,{},e))),s=t.reduce(o.handlers,function(n,i){return!i||!t.isFunction(i.handle)?n:i.handle(n,i.options,r)},e),s)&&(h=o.destination||f,r.set(h,s))}),r}})(jQuery,_),function(n,t,i){function e(i,u){var f,h,o,e,c,l;if(!t.isString(i))throw"indicatorId must be a string.";if(!t.isObject(u))throw"model is expected to be an object.";if(f=u.clone().toJSON(),f.created=Date.now(),h=20,o=s(),JSON.stringify(f).length<3800){for(o[r(i)]=f,e=t.chain(o).sortBy(function(n){return n.created});e.size().value()>h||JSON.stringify(e.value()).length>3800;)c=e.first().value(),l=r(c.indicatorId),n.removeCookie(l,{path:"/"}),e=e.rest();n.cookie(r(i),f,{expires:1,path:"/"})}return f}function f(){return"selections"}function r(n){if(!t.isString(n))throw"key must be a string.";return f()+"-"+n}function o(t){return n.cookie(r(t))}function s(){var i=n.cookie(),r={},t;for(t in i)t.indexOf(f())==0&&(r[t]=i[t]);return r}var u=window.nspace("kidscount.routes");u.ModelPersistence=function(i){t.bindAll(this),this.model=i,n.cookie.json=!0},u.ModelPersistence.prototype.getSelections=function(n){var r=this.model.get("indicatorId"),u=o(r),f=u?u:n;if(t.isUndefined(r))throw"indicatorId must be present in the model to retrieve the selections for a given indicator.";return t.isObject(f)?new Backbone.Model(f):i},u.ModelPersistence.prototype.setSelections=function(n){var u=this,i=n&&n.get?n.get("indicatorId"):null,r;if(t.isUndefined(i))throw"indicatorId must be present in the model to persist the model";return r=e(i,n),this.model&&setTimeout(function(){u.model.trigger("persistence:updated")},1),r}}(jQuery,_,undefined),function(n,t){var i=nspace("kidscount.routes"),r=nspace("kidscount.routes.fn");i.ModelNormalizer=function(n,t){this.persist=n||new i.ModelPersistence,this.settings=t||i.settings},i.ModelNormalizer.prototype.setSelections=function(n){var r=new i.ModelMapper(this.settings),t;return t=r.map(n),this.persist.setSelections(t)},i.ModelNormalizer.prototype.getSelections=function(n){return this.persist.getSelections(n)},r.toArray=function(n){return t.isArray(n)?n:[n]},i.settings={indicatorId:{destination:"indicatorId"},allCharacteristicIds:{destination:"allCharacteristicIds"},allDataTypeIds:{destination:"allDataTypeIds"},allTimeFrameIds:{destination:"allTimeFrameIds"},locationIds:{destination:"loc"},timeFrameId:{destination:"tf",handlers:[{handle:r.toArray}]},timeFrameIds:{destination:"tf"},locationTypeId:{destination:"loct"},characteristicIds:{destination:"ch"},dataTypeId:{destination:"fmt",handlers:[{handler:r.toArray}]},dataTypeIds:{destination:"fmt"},isCompared:{destination:"isCompared"},currentSection:{destination:"currentSection"}}}(jQuery,_),function(n){var r=nspace("kidscount.routes"),t=nspace("kidscount.routes.fn"),i=nspace("kidscount.vizUtil");t.getVizTypes=function(){return _.filter(_.keys(t.settings),function(n){return n!="fallback"})},t.getAllRouteProperties=function(){var n=t.getVizTypes(),i=_.map(n,function(n){var i=t.settings[n].route;return t.getRouteParts(i)});return _.reduce(i,function(n,t){var i=_.filter(t,function(t){return!_.contains(n,t)});return _.each(i,function(t){n.push(t)}),n},[])},t.getRouteParts=function(n){return _.map(n.split("/"),function(n){return n.replace(":","")})},t.settings={fallback:{route:"",serialize:{locationIds:i.getPackedArray,characteristicIds:i.getCharUrlValue,isCompared:i.getBooleanValue}},ranking:{route:":currentSection/:locationTypeId/:locationIds/:isCompared/:timeFrameId/:characteristicIds/:dataTypeId"},detailed:{route:":currentSection/:locationTypeId/:locationIds/:isCompared/:timeFrameIds/:characteristicIds/:dataTypeIds",serialize:{timeFrameIds:i.getArrayValue,dataTypeIds:i.getArrayValue}},map:{route:":locationTypeId/:locationIds/:isCompared/:timeFrameId/:characteristicIds/:dataTypeId/:activeColorPalette/:mapPosition"},line:{route:":locationTypeId/:locationIds/:isCompared/:timeFrameIds/:timeFrameDirection/:characteristicIds/:dataTypeId",serialize:{timeFrameIds:i.getArrayValue}},bar:{route:":locationTypeId/:locationIds/:isCompared/:timeFrameId/:characteristicIds/:dataTypeId"}},r.RouteSerializer=function(){},r.RouteSerializer.prototype.getParts=function(i,r,u){var f=n.extend(!0,{},t.settings.fallback,t.settings[i]),o=f.route||"",s=t.getRouteParts(o),e;return e=_.map(s,function(n){var t=r.get(n)||"";return t=!t&&u&&u[n]?u[n]:f.serialize&&f.serialize[n]?f.serialize[n](t):t.toString()})},r.RouteSerializer.prototype.serialize=function(n,t,i){var r=this.getParts(n,t,i);return _.isArray(r)?r.join("/"):""}}(jQuery)