(function(a){var u={},t={},h={};function m(B,x,z){var w=this,v,A,y;x=x||{};A=a.extend({overwriteCache:false,complete:null,success:null,error:function(){a(this).each(function(){a(this).html(A.errorMessage)})},errorMessage:"There was an error loading the template.",paged:false,pageNo:1,elemPerPage:10,append:false,prepend:false,beforeInsert:null,afterInsert:null},z);if(a.type(x)==="array"){return r.call(this,B,x,A)}if(!g(B)){v=a(B)}y=A.isFile||(typeof A.isFile==="undefined"&&(typeof v==="undefined"||v.length===0));if(y&&!A.overwriteCache&&u[B]){p(B,w,x,A)}else{if(y&&!A.overwriteCache&&u.hasOwnProperty(B)){c(B,w,x,A)}else{if(y){l(B,w,x,A)}else{n(v,w,x,A)}}}return this}function b(w,v){if(v){h[w]=v}else{h=a.extend(h,w)}}function g(v){return typeof v==="string"&&v.indexOf("/")>-1}function r(E,w,B){B=B||{};var v=this,F=w.length,y=B.prepend&&!B.append,x=0,D=0,z=false,A;if(B.paged){var C=(B.pageNo-1)*B.elemPerPage;w=w.slice(C,C+B.elemPerPage);F=w.length}A=a.extend({},B,{complete:function(){if(y){v.prepend(this.html())}else{v.append(this.html())}x++;if(x===F){if(z&&B&&typeof B.error==="function"){B.error.call(v)}if(B&&typeof B.complete==="function"){B.complete()}}},success:function(){D++;if(D===F){if(B&&typeof B.success==="function"){B.success()}}},error:function(){z=true}});if(!B.append&&!B.prepend){v.html("")}if(y){w.reverse()}a(w).each(function(){var G=a("
");m.call(G,E,this,A)});return this}function c(y,w,v,x){if(t[y]){t[y].push({data:v,selection:w,settings:x})}else{t[y]=[{data:v,selection:w,settings:x}]}}function p(z,x,w,y){var v=u[z].clone();o.call(x,v,w,y);if(typeof y.success==="function"){y.success()}}function l(z,x,w,y){var v=a("");u[z]=null;v.load(z,function(A,B){if(B==="error"){j(z,x,w,y)}else{k(v,z,x,w,y)}})}function n(v,y,x,z){var w=a("");if(v.is("script")){v=a.parseHTML(a.trim(v.html()))}w.html(v);o.call(y,w,x,z);if(typeof z.success==="function"){z.success()}}function o(x,v,w){f(x,v);a(this).each(function(){var y=a(x.html());if(w.beforeInsert){w.beforeInsert(y)}if(w.append){a(this).append(y)}else{if(w.prepend){a(this).prepend(y)}else{a(this).html(y)}}if(w.afterInsert){w.afterInsert(y)}});if(typeof w.complete==="function"){w.complete.call(a(this))}}function j(y,w,v,x){var z;if(typeof x.error==="function"){x.error.call(w)}a(t[y]).each(function(A,B){if(typeof B.settings.error==="function"){B.settings.error.call(B.selection)}});if(typeof x.complete==="function"){x.complete.call(w)}while(t[y]&&(z=t[y].shift())){if(typeof z.settings.complete==="function"){z.settings.complete.call(z.selection)}}if(typeof t[y]!=="undefined"&&t[y].length>0){t[y]=[]}}function k(v,z,x,w,y){var A;u[z]=v.clone();o.call(x,v,w,y);if(typeof y.success==="function"){y.success.call(x)}while(t[z]&&(A=t[z].shift())){o.call(A.selection,u[z].clone(),A.data,A.settings);if(typeof A.settings.success==="function"){A.settings.success.call(A.selection)}}}function f(w,v){v=v||{};s("data-content",w,v,function(x,y){x.html(e(x,y,"content"))});s("data-content-append",w,v,function(x,y){x.append(e(x,y,"content"))});s("data-content-prepend",w,v,function(x,y){x.prepend(e(x,y,"content"))});s("data-src",w,v,function(x,y){x.attr("src",e(x,y,"src"))},function(x){x.remove()});s("data-alt",w,v,function(x,y){x.attr("alt",e(x,y,"alt"))});s("data-value",w,v,function(x,y){x.val(e(x,y,"value"))});s("data-link",w,v,function(x,z){var y=a("");y.attr("href",e(x,z,"link"));y.html(x.html());x.html(y)});s("data-link-wrap",w,v,function(x,z){var y=a("");y.attr("href",e(x,z,"link-wrap"));x.wrap(y)});s("data-options",w,v,function(x,y){a(y).each(function(){var z=a("");z.attr("value",this).text(this).appendTo(x)})});q(w,v)}function s(v,z,w,x,y){a("["+v+"]",z).each(function(){var A=a(this),B=A.attr(v),C=i(w,B);A.removeAttr(v);if(C&&x){x(A,C)}else{if(y){y(A)}}});return}function q(w,v){a("[data-template-bind]",w).each(function(){var x=a(this),y=a.parseJSON(x.attr("data-template-bind"));x.removeAttr("data-template-bind");a(y).each(function(){var A;if(typeof(this.value)==="object"){A=i(v,this.value.data)}else{A=i(v,this.value)}if(typeof A!=="undefined"&&this.attribute){switch(this.attribute){case"content":x.html(d(x,A,this));break;case"contentAppend":x.append(d(x,A,this));break;case"contentPrepend":x.prepend(d(x,A,this));break;case"options":var z=this;a(A).each(function(){var B=a("");B.attr("value",this[z.value.value]).text(d(x,this[z.value.content],z)).appendTo(x)});break;default:x.attr(this.attribute,d(x,A,this))}}})})}function d(v,x,w){if(w.formatter&&h[w.formatter]){return h[w.formatter].call(v,x,w.formatOptions)}return x}function i(v,w){var x=w.split("."),y,z=v;while((y=x.shift())&&typeof z!=="undefined"){z=z[y]}return z}function e(v,A,w){var z=v.attr("data-format-target"),y;if(z===w||(!z&&w==="content")){y=v.attr("data-format");if(y&&typeof h[y]==="function"){var x=v.attr("data-format-options");return h[y].call(v[0],A,x)}}return A}a.fn.loadTemplate=m;a.addTemplateFormatter=b})(jQuery);