var Tempo=(function(tempo){
tempo.prepare=function(container){if(typeof container==="string"){container=document.getElementById(container)}var templates=new Templates(false);templates.parse(container);return new Renderer(templates)};function Templates(nested){this.defaultTemplate=null;this.namedTemplates={};this.container=null;this.nested=nested!=undefined?nested:false;return this}Templates.prototype={parse:function(container){this.container=container;var children=container.getElementsByTagName("*");for(var i=0;i<children.length;i++){if(children[i].getAttribute("data-template")!==null&&(this.nested||!utils.isNested(children[i]))){var element=children[i].cloneNode(true);element.style.removeProperty("display");this.container=children[i].parentNode;for(var a=0;a<element.attributes.length;a++){var attr=element.attributes[a];if(utils.startsWith(attr.name,"data-if-")){var val;if(attr.value===""){val=true}else{val="'"+attr.value+"'"}this.namedTemplates[attr.name.substring(8,attr.name.length)+"=="+val]=element;element.removeAttribute(attr.name)}}this.defaultTemplate=element}}utils.clearContainer(this.container)},templateFor:function(item){for(var templateName in this.namedTemplates){if(eval("item."+templateName)){return this.namedTemplates[templateName].cloneNode(true)}}if(this.defaultTemplate){return this.defaultTemplate.cloneNode(true)}}};
function Renderer(templates){this.templates=templates;return this}Renderer.prototype={render:function(data){utils.clearContainer(this.templates.container);if(data){if(utils.typeOf(data)=="object"){data=[data]}var fragment=document.createDocumentFragment();for(var i=0;i<data.length;i++){var renderItem=function(renderer,item,fragment){var template=renderer.templates.templateFor(item);if(template&&item){var nestedDeclaration=template.innerHTML.match(/data-template="(.*?)"/);if(nestedDeclaration){var t=new Templates(true);t.parse(template);var r=new Renderer(t);r.render(item[nestedDeclaration[1]])}var html=template.innerHTML;for(var p in renderer.tags){html=html.replace(new RegExp(renderer.tags[p].regex,"gi"),renderer.tags[p].handler(renderer,item))}html=utils.replaceVariable(item,html);if(template.getAttribute("class")){template.className=utils.replaceVariable(item,template.className)}if(template.getAttribute("id")){template.id=utils.replaceVariable(item,template.id)}fragment.appendChild(utils.getElement(template,html))}}(this,data[i],fragment)}this.templates.container.appendChild(fragment)}return this},tags:[{regex:"\\{\\{if (.*?)\\}\\}(.*?)\\{\\{endif\\}\\}",handler:function(renderer,item){return function(match,condition,content){var member_regex="";for(var member in item){if(member_regex.length>0){member_regex+="|"}member_regex+=member}condition=condition.replace(/&amp;/g,"&");condition=condition.replace(new RegExp(member_regex,"gi"),function(match){return"item."+match});if(eval(condition)){return content}return""}}}]}
;var utils={startsWith:function(str,prefix){return(str.indexOf(prefix)===0)},replaceVariable:function(item,str){return str.replace(/\{\{([A-Za-z0-9\._]*?)\}\}/g,replace=function(match,variable){var val=eval("item."+variable);if(val){return val}return""})},clearContainer:function(el){if(el!==undefined&&el.childNodes!==undefined){for(var i=el.childNodes.length;i>=0;i--){if(el.childNodes[i]!==undefined&&el.childNodes[i].getAttribute!==undefined&&el.childNodes[i].getAttribute("data-template")!==null){el.childNodes[i].parentNode.removeChild(el.childNodes[i])}}}},isNested:function(el){var p=el.parentNode;while(p){if(p.getAttribute!==undefined&&p.getAttribute("data-template")!==null){return true}p=p.parentNode}return false},equalsIgnoreCase:function(str1,str2){return str1.toLowerCase()===str2.toLowerCase()},getElement:function(template,html){if(utils.equalsIgnoreCase(template.tagName,"tr")){var el=document.createElement("div");el.innerHTML="<table><tbody>"+html+"</tbody></table>";var depth=3;while(depth--){el=el.lastChild}return el}else{template.innerHTML=html;return template}},typeOf:function(obj){if(typeof(obj)=="object"){if(obj===null){return"null"}if(obj.constructor==(new Array).constructor){return"array"}if(obj.constructor==(new Date).constructor){return"date"}if(obj.constructor==(new RegExp).constructor){return"regex"}return"object"}return typeof(obj)}};return tempo})(Tempo||{});
