
YAHOO.namespace("YAHOO.extwidget");


YAHOO.extwidget.util = 
{

	/**
	* Uses regexp to databind 
	* @property _replaceX
	*/
	replaceEx: function ( t, o, scope,  uid )
	{
		if(!t)
		{	
			YAHOO.log('replaceEx error');
			return;
		}
		return t.replace(/{([^{}]*)}/g, 
        function (a, b) 
		{  
			// special tokens - unique id
			if (b.indexOf("__uid")==0)
				return uid;
	
			// callback functions for '__' prefix tokens
			if (b.indexOf("__")==0)
			{
				var formatFn = scope.formatFunctions[ b ];
				if ( formatFn )
					return formatFn.call( scope, o );	
				else
					return '';
			}
		
			if (!o)
				return t;

			// evalute the token assumng a.b.c notation
			var values = b.split('.');
			var r = o;
			for(var i=0;i<values.length;i++) 
			{
				r = (r[values[i]]) != null ? (r[values[i]]) : null;
				if (!r) break;
			}
			
			if (!r)
				return '';
			
			// string types are returned as is .. number mapped against format functions
			switch (typeof r)
			{
				case 'string':
					return r;
				break;
				case 'number':
				{
					var formatFn = scope.formatFunctions ? scope.formatFunctions[ values.pop() ] : null;
					if ( formatFn )
						return formatFn.call( scope, r );	
					else
						return ''+r;
				}				
				break;
				default:
                	return r; 
			}
        });
	}
}
