﻿(function() {
    var cache = {};
    this.bind = function bind(str, data) {
        // Figure out if we're getting a template, or if we need to
        // load the template - and be sure to cache the result.
        var fn = !/\W/.test(str) ?
cache[str] = cache[str] ||
bind(document.getElementById(str).innerHTML) :
        // Generate a reusable function that will serve as a template
        // generator (and which will be cached).
new Function("obj",
str
                        .replace(/[\r\t\n]/g, " ")
                        .split('{%').join("\t")
                        .replace(/((^|%})[^\t]*)'/g, "$1\r")
                        .replace(/\t=(.*?)%}/g, "\r$1\r")
                        .split("\t").join("")
                        .split('id=').join("\t")
                        .replace(/\t"(.*?)"/g, "\t$1\t")
                        .replace(/<(.*?)\r(.*?)\r(.*?)\t(.*?)\t(.*?)>/g, "\t$2=$4\t")
                        .replace(/^[^\t]*\t/g, "")
                        .replace(/\t[^\t]*\t/g, "\r")
                        .replace(/\t[^\t]*$/g, "")
                        .replace(/(.*?)\s*=(.*?)(\r|$)/g,
                        "            $create(Sys.Binding, {\n" +
                        "                target: $get('$2'),\n" +
                        "                targetProperty: 'value',\n" +
                        "                source: obj,\n" +
                        "                path: '$1',\n" +
                        "                mode: Sys.BindingMode.twoWay\n" +
                        "            });\n")
);
        // Provide some basic currying to the user
        return data ? fn(data) : fn;
    };
})();
