/***********************************************************************************************************************
 File		: TmplObj.js
 File Type	: Javascript (Internet Explorer > 5, Firefox 1.5, Netscape 7.1)

 Copyright 	: xtendo technologies Kurt Schwedes, 76185 Karlsruhe, Germany
			  All rights reserved
.......................................................................................................................
 Created		: 22.09.04 (based on TmplObj.js, project xtCS031001)
 Author			: Kurt Schwedes, xtendo technologies
 Last Change	: 12.01.06
.......................................................................................................................

 For handling textual code templates (e.g. html, xml)
 
.......................................................................................................................
 Develeped for 	: xtendo technologies Kurt Schwedes, 76185 Karlsruhe, Germany
 Contact		: kurt.schwedes@xtendo.de
 Documentation 	: ---
***********************************************************************************************************************/

/*----------------------------------------------------------------------------------------------------------------------
 CLASS CONSTANTS/DEFINITIONS
----------------------------------------------------------------------------------------------------------------------*/

TmplObj.TokenScan 	= /~([\w\d]+)~/gi;
TmplObj.SectorScan	= /<!--\[SECTOR\s+([\w\d]+)\]-->([\w\W\d\D\s\S€]*)<!--\[END\s+\1\]-->/i;

/*----------------------------------------------------------------------------------------------------------------------
 CONSTRUCTOR OF CLASS, INHERITANCE
----------------------------------------------------------------------------------------------------------------------*/

function TmplObj (def)
{
	if (! def || typeof def != 'object') def = {};
	this.CLASS 		= TmplObj;
	
	this.content 	= def.content || '';
	this.output 	= '';
	this.tokens 	= {};
	this.sectors 	= {};
	
	this.init();
	return this;
}

/*----------------------------------------------------------------------------------------------------------------------
 PUBLIC OBJECT METHODS
----------------------------------------------------------------------------------------------------------------------*/

TmplObj.prototype.init = function (c)
{
	with (this) {
		if (typeof c == 'string') content = c;
		if (content)
			findSectors().findTokens();
	}
	return this;
}

TmplObj.prototype.findTokens = function ()
{
	with (this) {
		while(TmplObj.TokenScan.exec(content))
			tokens[RegExp.$1] = '';
	}
	return this
}

TmplObj.prototype.findSectors = function ()
{
	var R = TmplObj.SectorScan;
	with (this) 
		while(R.exec(content)){
			content = content.replace(R, '<!--[SR ' + RegExp.$1 + ']-->');
			sectors[RegExp.$1] = new TmplObj({content: RegExp.$2});
		}
	return this;
}

TmplObj.prototype.setTokens = function (Data)
{
	with (this) {
		if (Data && typeof Data == 'object' && Data.length == null)
			for (var p in Data)
				tokens[p] = Data[p];
	}
	return this;
}

TmplObj.prototype.resetTokens = function ()
{
	with (this) 
		for (var p in tokens) tokens[p] = '';
	return this;
}

TmplObj.prototype.resolveTokens = function (Data)
{
	with (this) {
		if (Data) setTokens(Data);
		output = output || content;
		output = output.replace(TmplObj.TokenScan, function (s, m) { return m && tokens[m] ? tokens[m] : '' });
	}
	return this;
}

TmplObj.prototype.resolveSector = function (s, appendMode)
{
	with (this) {
		var oS;
		if (typeof s == 'string')
			oS = sectors[s];
		if (oS){
			var out = oS.output || oS.content;
			oS.output = '';
			if (appendMode) out += '<!--[SR ' + s + ']-->';
			output = output || content;
			output = output.replace(new RegExp("<!--\\[SR " + s + "\\]-->", 'gi'), out);
		}
	}
	return this;
}


