/**
 * @fileOverview KfW Template
 * @version 0.1
 * @author Michael Schieben <michael@twoantennas.com> 
 */

/*jslint nomen: true, debug: true, evil: false, onevar: false, browser: true, plusplus: false, undef: true */
/*global window: true, KfW: true, jQuery: true */

(function ($) {
	
	/**
	 * Template
	 * @class
	 * <p>
	 * We use the Template-System to keep all HTML that is inserted into the DOM
	 * by JavaScript in one place.
	 * </p>
	 * <p>
	 * A Template is a small piece of HTML with variable-placeholders that can be
	 * replaced with data.
	 * </p>
	 * @example
	 * <p class="sum">Summe: $sum $currency</p>
	 */
	var Template = function () {
		return new Template.fn.init();
	};

	Template.fn = Template.prototype = {
	
		/**
		 * Templates holds all templates.
		 * @see  Template.extend()
		 */
		Templates: {},
	
		init: function () {
			return this;
		},
	
		/**
		 * Extends the Templates.
		 * @param templates An object with new templates
		 * <p>
		 * Each entry is a key-value pair: The _key_ holds the name of the Template.
		 * The name is used to reference the Template in the lookup table and should
		 * be unique. The _value_ holds the html snippet.
		 * </p><p>
		 * @see kfw.template.templates.js for default templates
		 * </p>
		 */
		extend : function (templates) {
			$.extend(this.Templates, templates);
			return this;
		},
		
		/**
		 * Check if a Template exists
		 * @param name The _key_ of the template in the lookup table
		 */
		exists : function (name) {
			return this.Templates[name] !== undefined;
		},
	
		/**
		 * Renders a Template.
		 * @param name The _key_ of the template in the lookup table
		 * @param data The data to render the template in form of
		 * key-value pairs. All occurrences of $_key_ in the
		 * template will be replace with its _value_.
		 */
		render : function (name, data) {
			if (data !== undefined) {
				return this.parse(this.Templates[name], data);
			} else {
				return this.Templates[name];
			}
		},
		
		/**
		 * Renders a Template as jQuery element
		 * @param name The _key_ of the template in the lookup table
		 * @param data The data to render the template in form of
		 * key-value pairs. All occurrences of $_key_ in the
		 * template will be replace with its _value_.
		 */
		element : function (name, data) {
			return $(this.render(name, data));
		},
		
		/**
		 * Uses jQuery.Tokenizer to parse a template
		 * @param name The template
		 * @param data The data to render the template in form of
		 * key-value pairs. All occurrences of $_key_ in the
		 * template will be replace with its _value_.
		 * @see jquery.Tokenizer.js
		 */
		parse : function (template, data) {
			var tokenizer = new $.tokenizer(/\$(\w+)/, function (src, real, re) {
				return real ? src.replace(re, function (all, name) {
					return data[name];
				}) : src;
			});
			return tokenizer.parse(template).join('');
		}
	};

	Template.fn.init.prototype = Template.fn;
	Template = new Template();

	//make it global
	KfW.Template = Template;

}(jQuery));
