/*!
 * Simple JavaScript Templating
 * 
 * @author John Resig - http://ejohn.org/ - MIT Licensed
 * @see http://ejohn.org/blog/javascript-micro-templating
 * 
 * Adapted for jQuery and updated by Ivan Petropolsky <jscss.ru@gmail.com>
 */
;(function($) {

	var
		cache = {},

		// Regular expressions are created only once
		re = [
			/\W/,
			/[\r\t\n]/g,
			/((^|%>)[^\t]*)'/g,
			/\t=(.*?)%>/g,
			/^[\s\n]*\/\/\<\!\[CDATA\[/,
			/\/\/\]\]\>[\s\n]*$/
		];
 
	$.tmpl = function(str, data)
	{
		var
			fn,
			$el;

		// We can work with strings only (template body or id)
		if (typeof str != 'string')
		{
			return false;
		}

		/*
			If the string does not contain special characters,
			consider it an identifier of an element with a template
		*/
		if (!re[0].test(str))
		{
			$el = $('#' + str);
			if (!$el.length)
			{
				return false;
			}
			str = $el.html()
				.replace(re[4], '')
				.replace(re[5], '');
		}

		// An empty string can cause an infinite recursion
		if (!str.length)
		{
			return false;
		}

		// 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 = $el
				? (cache[str] = cache[str] || $.tmpl(str))

				// 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(re[1], " ")
						.split("<%").join("\t")
						.replace(re[2], "$1\r")
						.replace(re[3], "',$1,'")
						.split("\t").join("');")
						.split("%>").join("p.push('")
						.split("\r").join("\\'")
				+ "');}return p.join('');");

		// Provide some basic currying to the user
		return data
			? fn(data)
			: fn;
	};

})(jQuery);