/*!
 * Arenite JavaScript Library v2.1.2
 * https://github.com/lcavadas/arenite
 *
 * Copyright 2014, Luís Serralheiro
 */
Arenite=function(n){var e=Arenite.Object();return e.object.forEach(e.object,function(n,e){Object.prototype[e]||Object.defineProperty(Object.prototype,e,{writable:!0,enumerable:!1,value:function(){return n.apply(this,[this].concat([].slice.call(arguments)))}})}),e.object.forEach(e.array,function(n,e){Array.prototype[e]||Object.defineProperty(Array.prototype,e,{writable:!0,enumerable:!1,value:function(){return n.apply(this,[this].concat([].slice.call(arguments)))}})}),e.fuseWith(Arenite.Html(e)),e.fuseWith(Arenite.Async(e)),e.fuseWith(Arenite.Url(e)),e.fuseWith(Arenite.DI(e)),e.fuseWith(Arenite.AnnotationProcessor(e)),e.fuseWith(Arenite.Context(e)),e.fuseWith(Arenite.Loader(e)),e.fuseWith(Arenite.Bus(e)),e.di.init(n),e},Arenite.AnnotationProcessor=function(arenite){var _processAnnotations=function(n){for(var e,t=/@arenite-instance\s+["']([^"']+)["']\s*(.*);\s*(@arenite-init\s+["']([^"']+)["']\s*(.*);)*\s*(@arenite-start\s+["']([^"']+)["']\s*(.*);)*\s*\*\/\s*([\w.]+)/g;e=t.exec(n);)_processAnnotation(e)},_processArgAnnotation=function(match){var args=[],split=match.split(",");return split.forEach(function(arg){var argPair=arg.split(":"),argObj={};argObj[argPair[0].trim()]="ref"===argPair[0].trim()?argPair[1].trim():eval(argPair[1].trim()),args.push(argObj)}),args},_processAnnotation=function(n){var e=n[1],t=n[9];if(arenite.config.context||(arenite.config.context={instances:{}}),arenite.config.context.instances[e]={namespace:t},n[2]&&(arenite.config.context.instances[e].args=_processArgAnnotation(n[2])),n[4]&&(arenite.config.context.instances[e].init={func:n[4]},n[5]&&(arenite.config.context.instances[e].init.args=_processArgAnnotation(n[5]))),n[7]){var o={instance:e,func:n[7]};n[8]&&(o.args=_processArgAnnotation(n[8])),arenite.config.context.start||(arenite.config.context.start=[]),arenite.config.context.start.push(o)}};return{annotation:{processAnnotations:_processAnnotations}}},Arenite.Async=function(n){var e=function(n,e,t){var o=0,r=n.length,i=function(){r>o?e(n[o],o++):t()};return{next:i}},t=function(e,t,o){var r=0,i=o||(new Date).getTime();return n.config.debug&&(window.console.groupCollapsed('Latch: Starting latch "'+i+'" for',e,"times"),window.console.trace(),window.console.groupEnd()),{countDown:function(){r++,n.config.debug&&window.console.log('Latch: Counting down latch "'+i+'" ,',e-r,"remaining"),r===e&&(n.config.debug&&window.console.log('Latch: Triggering latch "'+i+'"'),t(r))},countUp:function(){r--,n.config.debug&&window.console.log('Latch: Counting up latch "'+i+'" ,',e-r,"remaining"),r===e&&(n.config.debug&&window.console.log('Latch: Triggering latch "'+i+'"'),t(r))}}};return{async:{seqLatch:e,latch:t}}},Arenite.Bus=function(){var n={},e=function(e,t){n[e]||(n[e]=[]),n[e].push(t)},t=function(e,t){n[e]&&n[e].forEach(function(o,r){o===t&&n[e].splice(r,1)})},o=function(e,t){window.console.groupCollapsed('sending event:"'+e+'" with ',t),window.console.trace(),window.console.groupEnd(),n[e]&&n[e].forEach(function(n){window.setTimeout(function(){n(t)},0)})};return{bus:{subscribe:e,unsubscribe:t,publish:o}}},Arenite.Context=function(n){var e={},t={},o=1,r=function(n,o,r){r?t[n]=o:e[n]=o},i=function(n){e.deleteInPath(n)},c=function(r){if(t.hasOwnProperty(r)){var c="__factory_instance_"+r+"__"+o++,a={};a[c]=t[r].fuseWith({factory:!1}),n.di.wire(a);var s=e[c];return i(c),s}return e[r]};return window.require=c,window.define=function(n,e,t){for(;!Array.isArray(e);)t=e,e="function"==typeof n||Array.isArray(n)?n:[],n="string"==typeof n?n:void 0;var o=[];e.forEach(function(n){"exports"===n?o.push({}):"require"===n?o.push(c):o.push(c(n))});var i=t.apply(window,o);if(!n)try{throw new Error}catch(a){var s=/([^\/]+?)(\.min)*\.js[:\d]+\)*$/gm;if(s.exec(a.stack)){var u=s.exec(a.stack);u.length>1&&(n=u[1])}}n&&r(n,i)},window.define.amd={jQuery:!0},{context:{get:c,add:r,remove:i}}},Arenite.DI=function(n){var e=1,t=function(e){var t=e.func;return t="function"==typeof e.func?e.func:e.extension?n[e.instance].getInPath(e.func):n.context.get(e.instance).getInPath(e.func)},o=function(t,o,r,i){t.args=t.args||[];var c=!1,a=[];return t.args.forEach(function(o,u){if("undefined"!=typeof o.value)a.push(o.value);else if("undefined"!=typeof o.ref){var f=n.context.get(o.ref);f?a.push(f):(c=!0,n.debug&&window.console.log("Arenite: Failed to resolve arg",o))}else if("undefined"!=typeof o.func)a.push(o.func);else if("undefined"!=typeof o.exec)a.push(o.exec(n));else if("undefined"!=typeof o.instance){var l={instances:{}},d="__anonymous_temp_instance__"+e++;l.instances[d]=o.instance,s(l,i),a.push(n.context.get(d)),"factory"===r?n.context.remove(d):t.args.splice(u,1,{ref:d})}}),t.wait&&"function"==typeof o&&a.push(o),c?null:a},r=function(n,e,r,i){var c=t(n);if(!c)throw'Unknown function "'+n.func+'" for instance "'+n.instance+'"';var a=o(n,r,void 0,i);if(!a)throw'Unable to resolve arguments for "'+n.func+'" of instance "'+n.instance+'"';n.wait&&"function"==typeof e&&e(),c.apply(c,a)},i=function(e,t,r){if(e){var c={};if(e.toKeyArray().forEach(function(r){if(e[r].factory)n.context.add(r,e[r],!0);else{var i=window.getInPath(e[r].namespace);if(!i)throw'Unknown function "'+e[r].namespace+'"';var a=o(e[r],null,t,[r]);if(a){var s=i.apply(i,a);if("extension"===t){var u={};u[r]=s,n=n.fuseWith(u)}n.debug&&window.console.log("Arenite: Instance",r,"wired"),n.context.add(r,s)}else c[r]=e[r]}}),c.toKeyArray().length!==e.toKeyArray().length&&c.toKeyArray().length>0)c.forEach(function(n,e){var t={};t[e]=n,i(t,void 0,r.concat(e))});else if(0!==c.toKeyArray().length)throw"Make sure you don't have circular dependencies, Unable to resolve the following instances: "+c.toKeyArray().join(", ")+" - ["+r.join(", ")+"]"}},c=function(n,e,t){n&&n.forEach(function(n,o){n.init&&!n.factory&&("string"==typeof n.init&&(n.init={func:n.init}),r({instance:o,extension:t}.fuseWith(n.init),function(){e.countUp()},function(){e.countDown()}))})},a=function(n){n&&("complete"!==document.readyState?window.setTimeout(function(){a(n)},100):n.forEach(function(n){r(n)}))},s=function(e,t){if(n.config.debug&&window.console.time("Arenite context load"),e){var o=n.async.latch(1,function(){n.config.debug&&window.console.timeEnd("Arenite context load"),a(e.start)},"instances"),r=n.async.latch(1,function(){i(e.instances,void 0,t||[]),c(e.instances,o),o.countDown()},"extensions");i(e.extensions,"extension",t||[]),c(e.extensions,r,!0),r.countDown()}},u=function(e){var t=n.async.latch(e.async?e.async.length:0,function(){s(n.config.context)},"dependencies");e.async.forEach(function(e){n.loader.loadScript(e,t.countDown)})},f=function(){if(!n.config.context||!n.config.context.dependencies)return s(n.config.context);var e;e=n.config.context.dependencies[n.config.mode]?n.config.context.dependencies[n.config.mode]:{sync:[],async:[]};var t=n.async.seqLatch(e.sync||[],function(e){n.loader.loadScript(e,t.next)},function(){u(e)});t.next()},l=/^((http:\/\/)|(http:\/\/)|(\/\/)).*$/,d=/[\d]+\.[\d]+\.*[\d]*/,p="//rawgit.com/{vendor}/{version}/{module}/",h="//cdn.rawgit.com/{vendor}/{version}/{module}/",g=function(e,t,o){var r={},i=e.toKeyArray();if(i.length){var c=n.async.latch(i.length,function(){e.forEach(function(e,t){var i=e.vendor?"default":n.config.mode,c={async:[],sync:[]};(r[t].module.context.dependencies[i]||[]).forEach(function(n,o){n.forEach(function(n){"string"==typeof n?c[o].push(n.match(l)||!e.vendor?n:r[t].path+n):c[o].push(n.fuseWith({url:n.url.match(l)||!e.vendor?n.url:r[t].path+n.url}))})}),delete r[t].module.context.dependencies,o.context=o.context||{},o.context.dependencies=o.context.dependencies||{"default":{sync:[],async:[]}},o.context.dependencies[o.mode]=o.context.dependencies[o.mode]||{sync:[],async:[]},o.context.dependencies.forEach(function(n){n.fuseWith(c)}),o=o.fuseWith(r[t].module)}),t()},"modules");e.forEach(function(e,t){var o;e.vendor?(o=n.config.repo?n.config.repo.replace("{vendor}",e.vendor):e.version.match(d)?h.replace("{vendor}",e.vendor):p.replace("{vendor}",e.vendor),o=o.replace("{version}",e.version),o=o.replace("{module}",e.module)):o=e.module+"/",n.loader.loadResource(o+"module.json",function(n){r[t]={path:o,module:JSON.parse(n.responseText)},r[t].module.imports?g(r[t].module.imports,c.countDown,r[t].module):c.countDown()})})}else t()},y=function(e,t){if(n.context.add("arenite",n),n.config=e,n.config.mode=n.config.mode||n.url.query().mode||"default",window.console.log("Arenite: Starting in mode",n.config.mode),e.debug&&"function"==typeof e.debug&&(e.debug=e.debug(n)),e.expose){var o=e.expose;"function"==typeof e.expose&&(o=e.expose(n)),o&&(window[o]=n)}n.config.imports?(window.console.log("Arenite: Fetching modules",n.config.imports.toKeyArray()),g(JSON.parse(JSON.stringify(n.config.imports)),t,n.config)):t()},v=function(e){n.config?f():y(e,f)},w=function(n){i(n,"factory",[]),c(n)};return{di:{init:v,loadConfig:y,resolveArgs:o,exec:r,wire:w}}},Arenite.Html=function(n){var e=function(n){var e=document.createElement("span");return e.innerText=n,e.innerHTML},t=function(n){var e=document.createElement("span");return e.innerHTML=n,e.innerText};return{html:{escape:e,unescape:t}}},Arenite.Loader=function(arenite){var firefoxOs=navigator.userAgent.indexOf("Firefox")>-1&&navigator.userAgent.indexOf("Mobile")>-1,_createCORSRequest=function(n,e,t,o){var r=new XMLHttpRequest;return r.open(n,e,!0),r.onreadystatechange=function(){4===r.readyState&&(r.status%100<4?t():o())},arenite.config.withCredentials&&(r.withCredentials=!0),r},_loadResource=function(n,e,t){var o=_createCORSRequest("GET",n,function(){e(o)},function(){"function"==typeof t&&t(o)});o.send()},_loadStyleWithTag=function(n,e){var t=document.getElementsByTagName("head")[0],o=document.createElement("link");o.rel="stylesheet",o.type="text/css",-1===navigator.appVersion.indexOf("MSIE 9")&&(o.onreadystatechange=function(){"complete"===this.readyState&&e()}),o.onload=e,o.href=n,t.appendChild(o)},_loadScriptWithTag=function(n,e){var t=document.getElementsByTagName("head")[0],o=document.createElement("script");o.async=!0,o.type="text/javascript",-1===navigator.appVersion.indexOf("MSIE 9")&&(o.onreadystatechange=function(){"complete"===this.readyState&&e()}),o.onload=e,o.src=n,t.appendChild(o)},_loadScriptAsResource=function(url,callback){_loadResource(url,function(req){arenite.annotation.processAnnotations(req.responseText);var file_tag="\n//@ sourceURL="+window.location.origin+"/"+url+"\n//# sourceURL="+window.location.origin+"/"+url;eval(req.responseText+file_tag),callback()})},_sameOrigin=function(n){var e=window.location,t=document.createElement("a");return t.href=n,t.hostname===e.hostname&&t.port===e.port&&t.protocol===e.protocol},_loadScriptFrom=function(n,e){var t=n.match(/.*\.(\w+)\?*.*/)[1];if(_sameOrigin(n)&&!firefoxOs&&"js"===t)_loadScriptAsResource(n,e);else if("js"===t)_loadScriptWithTag(n,e);else{if("css"!==t)throw'Uknown extension "'+t+'"';_loadStyleWithTag(n,e)}},_loadScript=function(n,e){"string"==typeof n?_loadScriptFrom(n,e):_loadScriptFrom(n.url,function(){n.instances.forEach(function(n,e){var t=window[n];t&&arenite.context.add(e,t),delete window[n]}),"function"==typeof e&&e()})};return{loader:{loadResource:_loadResource,loadScript:_loadScript}}},Arenite.Object=function(){var n=function(n,e){if(n){var t=e.split("."),o=t.splice(t.length-1,1),r=n;return t.forEach(function(n){r&&(r[n]||(r[n]={}),r=r[n])}),{object:r,path:o}}},e=function(n,e){if(n){var t=e.split("."),o=n;return t.forEach(function(n){o&&(o=o[n])}),o}},t=function(e,t,o){var r=n(e,t);return r&&r.object?(r.object[r.path]=o,r.object[r.path]):void 0},o=function(e,t){var o=n(e,t);o&&o.object&&delete o.object[o.path]},r=function(n,e){for(var t in e)e.hasOwnProperty(t)&&(n[t]&&"object"==typeof n[t]?n[t].constructor===Array&&e[t]&&e[t].constructor===Array?n[t]=a(n[t].concat(e[t])):r(n[t],e[t]):n[t]=e[t]);return n},i=function(n){var e,t=[];for(e in n)n.hasOwnProperty(e)&&t.push(e);return t},c=function(n,e){if(n.length){var t=!1;return n.forEach(function(n){t=t||n===e}),t}for(var o in n)if(n.hasOwnProperty(o)&&o===e)return!0;return!1},a=function(n){var e=[];return n.forEach(function(n){e.indexOf(n)<0&&e.push(n)}),e},s=function(n,e){return a(n.concat(e))},u=function(n,e){var t=[];for(var o in n)n.hasOwnProperty(o)&&t.push(n[o][e]);return t},f=function(n,t){var o={};return n.forEach(function(n){o[e(n,t)]=n}),o},l=function(n,e){var t;for(t in n)n.hasOwnProperty(t)&&e(n[t],t)},d=function(n){var e=[];return l(n,function(n){e.push(n)}),e},p=function(n,e){var t={};return e.forEach(function(e){n.hasOwnProperty(e)&&(t[e]=n[e])}),t},h=function(n,e){var t,o=Array.isArray(n);return l(n,function(n,r){!t&&e(n,r)&&(o?t=n:(t={},t[r]=n))}),t},g=function(n,t,o){return h(n,function(n){return(t?e(n,t):n)===o})},y=function(n,e){var t=Array.isArray(n),o=t?[]:{};return l(n,function(n,r){e(n,r)&&(t?o.push(n):o[r]=n)}),o},v=function(n,t,o){return y(n,function(n){return(t?e(n,t):n)===o})},w=function(n,e){var t=Array.isArray(n),o=t?[]:{};return l(n,function(n,r){var i=e(n,r);void 0!==i&&(t?o.push(i):o[r]=i)}),o},m=function(n,t){return n.collectWhere(function(n){return e(n,t)})};return{object:{getInPath:e,setInPath:t,deleteInPath:o,fuseWith:r,toKeyArray:i,forEach:l,containsKey:c,toArray:d,filterWith:p,findWhere:h,findBy:g,findAllWhere:y,findAllBy:v,collectWhere:w,collectBy:m},array:{containsElement:c,filterUnique:a,mergeWith:s,toArrayOf:u,toObject:f,findWhere:h,findBy:g,findAllWhere:y,findAllBy:v,collectWhere:w,collectBy:m}}},Arenite.Url=function(){var n,e=function(e){if(!n||e){n={};for(var t=window.location.search.substring(1),o=t.split("&"),r=0;r<o.length;r++){var i=o[r].split("=");"undefined"==typeof n[i[0]]?n[i[0]]=i[1]:"string"==typeof n[i[0]]?n[i[0]]=[n[i[0]],i[1]]:n[i[0]].push(i[1])}}return n};return{url:{query:e}}};