
// Simple JavaScript Templating
// John Resig - http://ejohn.org/ - MIT Licensed
(function(){
  var cache = {};
  
  this.fetchTmpl = function fetchTmpl(url) {
	  $('body').append('<div style="display:none">'+$.ajax({
          url: str,
          async: false
         }).responseText+'</div>');	  
  }
  
  this.tmpl = function tmpl(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] ||
        tmpl(document.getElementById(str).innerHTML) :

      // Generate a reusable function that will serve as a template
      // generator (and which will be cached).
      new Function("obj",
        "var p=[],print=function(){p.push.apply(p,arguments);};" +

        // Introduce the data as local variables using with(){}
        "with(obj){p.push('" +

        // Convert the template into pure JavaScript
        str
          .replace(/[\r\t\n]/g, " ") // remove carriage returns, tabs etc...
          .split("<%").join("\t") // now split with tabs
          .replace(/((^|%>)[^\t]*)'/g, "$1\r")
          .replace(/\t==(.*?)%>/g, "',typeof($1)=='undefined'?'':$1,'") // direct insertion
          .replace(/\t=(.*?)%>/g, "',$1,'") // allows for evaluation, but will die if undefined (unless you test like above)
          .replace(/\t\+(.*?),(.*?)%>/g, "',tmpl\($1,$2\),'") // calls another template
          .split("\t").join("');")
          .split("%>").join("p.push('")
          .split("\r").join("\\'")
      + "');}return p.join('');");
    if (data && (data instanceof Array)) {
        tmp = "";
        for(var x=0;x<data.length;x++) {
            if ( (data[x]) &&
                 ((typeof data[x]) == "object") &&
                 (!(data[x] instanceof Array))
              ) data[x].i = x;
            tmp += tmpl(str, data[x]);
        }
        return tmp;
    } else return data ? fn( data ) : fn;
  };
})();

(function($){
 $.fn.tappend = function(tmplId, testData, options) {

  var defaults = {
  };
  var options = $.extend(defaults, options);
  var testData = (testData == null) ? {} : testData;
  var tmplId = tmplId;

  return this.each(function() {
    obj = $(this);
    obj.append(tmpl(tmplId, testData));
  });
 };
})(jQuery);

(function($){
 $.fn.thtml = function(tmplId, testData, options) {

  var defaults = {
  };
  var options = $.extend(defaults, options);
  var testData = (testData == null) ? {} : testData;
  var tmplId = tmplId;

  return this.each(function() {
    obj = $(this);
    html = tmpl(tmplId, testData)
    obj.html(html);
    $("a.history", obj).each(function() {
      this.url = this.href.substring(this.href.indexOf('#')+1);
      $(this).click(function() {
        $.historyLoad(this.url);
      });
      this.href="javascript:void(0)";
    });
  });
 };
})(jQuery);



/**
 * json.js:
 * This file defines functions JSON.parse() and JSON.serialize()
 * for decoding and encoding JavaScript objects and arrays from and to
 * application/json format.
 *
 * The JSON.parse() function is a safe parser: it uses eval() for
 * efficiency but first ensures that its argument contains only legal
 * JSON literals rather than unrestricted JavaScript code.
 *
 * This code is derived from the code at http://www.json.org/json.js
 * which was written and placed in the public domain by Douglas Crockford.
 **/
// This object holds our parse and serialize functions
var JSON = {};

// The parse function is short but the validation code is complex.
// See http://www.ietf.org/rfc/rfc4627.txt
JSON.parse = function(s) {
    try {
        return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
                                   s.replace(/"(\\.|[^"\\])*"/g, ''))) &&
            eval('(' + s + ')');
    }
    catch (e) {
        return false;
    }
};

// Our JSON.serialize() function requires a number of helper functions.
// They are all defined within this anonymous function so that they remain
// private and do not pollute the global namespace.
(function () {
    var m = {  // A character conversion map
            '\b': '\\b', '\t': '\\t',  '\n': '\\n', '\f': '\\f',
            '\r': '\\r', '"' : '\\"',  '\\': '\\\\'
        },
        s = { // Map type names to functions for serializing those types
            'boolean': function (x) { return String(x); },
            'null': function (x) { return "null"; },
            number: function (x) { return isFinite(x) ? String(x) : 'null'; },
            string: function (x) {
                if (/["\\\x00-\x1f]/.test(x)) {
                    x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) {
                        var c = m[b];
                        if (c) {
                            return c;
                        }
                        c = b.charCodeAt();
                        return '\\u00' +
                            Math.floor(c / 16).toString(16) +
                            (c % 16).toString(16);
                    });
                }
                return '"' + x + '"';
            },
            array: function (x) {
                var a = ['['], b, f, i, l = x.length, v;
                for (i = 0; i < l; i += 1) {
                    v = x[i];
                    f = s[typeof v];
                    if (f) {
                        v = f(v);
                        if (typeof v == 'string') {
                            if (b) {
                                a[a.length] = ',';
                            }
                            a[a.length] = v;
                            b = true;
                        }
                    }
                }
                a[a.length] = ']';
                return a.join('');
            },
            object: function (x) {
                if (x) {
                    if (x instanceof Array) {
                        return s.array(x);
                    }
                    var a = ['{'], b, f, i, v;
                    for (i in x) {
                        v = x[i];
                        f = s[typeof v];
                        if (f) {
                            v = f(v);
                            if (typeof v == 'string') {
                                if (b) {
                                    a[a.length] = ',';
                                }
                                a.push(s.string(i), ':', v);
                                b = true;
                            }
                        }
                    }
                    a[a.length] = '}\n';
                    return a.join('');
                }
                return 'null';
            }
        };

    // Export our serialize function outside of this anonymous function
    JSON.serialize = function(o) { return s.object(o); };
})(); // Invoke the anonymous function once to define JSON.serialize()
