/* DIGIVISION JQUERY TEMPLATE */
/** this is content loading with template dynamic 
 * usage:	- Dynamic loading template from htm file.
 * 			- Replace const text by content.
 * comp:	- Tested on IE 7 8 9+ | Chrome | FireFox 3.*+
 * 
 */

/* DECLARD VARIABALE OBJECT FOR CACHE TEMPLATE */
var cachetemp = new Object();

/* LOADING TEMPLATE FILE */
(function($) {
	//function begin
	$.fn.djqt = function(options) {
		var defaults = {
			template:'',
			nametemp:'',
			kind:'append',
			consts: {},
			success: function() { return false; }
		};
		var opts = $.extend(defaults, options);
		return this.each(function() {
			var $this = $(this);
			//add template loading into propery 'cachetemp' object
			if(cachetemp[opts.nametemp] == undefined)
				$.get(opts.template, function(data) {
					eval('cachetemp.'+opts.nametemp+'=data');
					activeconst();
				});
			//replace const by content data
			function activeconst() {
				var source = cachetemp[opts.nametemp];
				//loop for replace data.
				for (var constunit in opts.consts) {
					if(typeof(opts.consts[constunit]) == 'function')
						eval('source = source.replace(/\\{\\*'+constunit+'\\*\\}/, \''+opts.consts[constunit]()+'\')');
					else 
						eval('source = source.replace(/\\{\\*'+constunit+'\\*\\}/, \''+opts.consts[constunit]+'\')');
				}
				eval('$this.'+opts.kind+'(source)');
				//excute function custom when data add success;
				opts.success();
			}
		});
	};
	
})(jQuery);
