

/**
Written By John Bakker
me [ at ] johnbakker.name

Feel free to change,fry and/or eat .. just be sure to drop me a line :)

Usage:

  _tT.define('template1','Hello {name}');
  alert(_tT.parse('template1',{name:'john'}));



**/


/**
Creates the container.. you can have multiple templates easily and can reference them by name.
**/
function tTemplatesContainer()
{
	this.templates = new Array();
}
/**
	you define a template here
**/
tTemplatesContainer.prototype.define = function(name,template)
{
	newtemplate = this.templates.length;
	this.templates[newtemplate]= new tTemplate(name,template);
}
tTemplatesContainer.prototype.parse = function(name,vals)
{
	empty ='';
	if(typeof vals!='object')
	{
		returndata ='';
	}
	else
	{
		lu = this.lookup(name);
		if(lu>-1)
		{
			
			returndata = this.templates[lu].parse(vals);
	
		}
		else
		{
			returndata = '';
		}
	}
	
	return returndata;
	
	
}
tTemplatesContainer.prototype.lookup = function(name)
{
	for(tT=0;tT<this.templates.length;tT++)
	{
		//now it reads throught the templates 
		try
		{
			//just in case
			if(this.templates[tT].name==name)
			{
				return tT;
			}
		}
		catch(e)
		{
			//whoops :)
		}
	}
	
	return -1;
}

function tTemplate(name,content)
{
	this.name    = name;
	this.content = content;
}

tTemplate.prototype.parse = function(vals)
{
	output = this.content;
	for (var i in vals)
	{
		cl = "/{"+i+"}/g";
		eval("output = output.replace("+cl+",vals[i])");
		
		
	}
	return output;
}
 
  _tT = new tTemplatesContainer('_tT');
  