var enableTTF = false;

function ttf(_selector)
{	
	function rgb2hex(rgb) 
	{
		if(rgb.match(/^\#([0-9A-Za-z]+)$/))
		{
			return rgb.substring(1,rgb.length);
		}
		else 
			rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
		
		function hex(x) 
		{
			return	(isNaN(x) || (x == '0')) ? "00" : ("0" + parseInt(x).toString(16)).slice(-2);
		}
		
		return hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
	}
	
	function toInt(x,d)
	{
		if(x) x = x.replace(/[^0-9]+/g,'');
		return (isNaN(x)) ? d : parseInt(x, d);
	}
	
	//processing
	var selectors = new Array();
	
	if(!Ext.isArray(_selector)) selectors[0] = _selector;
	else selectors = _selector;
	
	Ext.each(selectors, function(selector) 
	{
		var setItems = Ext.select(selector);
		//get hover color of the current class
		var hoverClass = Ext.util.CSS.getRule(selector + ':hover');
		var hoverColor = '00FFFF';
		if (hoverClass) hoverColor = rgb2hex(hoverClass.style.color);
		
		if(setItems) setItems.each(function(o)
		{
			var w = (o.getStyle('min-width') == 'undefined')  ? 0 : toInt(o.getStyle('min-width'));
			var h = (o.getStyle('min-height') == 'undefined') ? 0 : toInt(o.getStyle('min-height'));
			var a 	= o.child('img.__ttf');
			//console.debug(a,o.dom.innerHTML,encodeURIComponent(o.dom.innerHTML));
			var txt = (a != null) ? a.dom.alt : encodeUTF8(o.dom.innerHTML);
			var alt = (a != null) ? a.dom.alt : o.dom.innerHTML.replace(/<\/?[^>]+>/g,' ');
			
			o.update('<img class="__ttf" src="' + 'txt2img.php?t=' + txt + '&c='+ rgb2hex(o.getStyle('color'))+'&s='+toInt(o.getStyle('font-size'))+'&f='+o.getStyle('font-weight')+'&cs=0&a=' + o.getStyle('text-align') +'&w=' + w + '&h=' + h + '" alt="' + alt + '" border="0" hspace="0" vspace="0">');

			if(o.dom.localName == 'a') //hover 
			{
				var cfg = {
					color 		: rgb2hex(o.getStyle('color')),
					hoverColor 	: hoverColor,
					text		: txt,
					width 		: w,
					height  	: h 
				};
					
				o.on("mouseenter",function(e,t,oo) 
				{
					//if(( e.target.localName == 'a')/* && (e.type == 'mouseover')*/ ) //do hover
					{
						var o = Ext.fly(t);
						var cfg = this;
						var i = (e.target.localName == 'img') ? o.dom : o.child('img');
						
						i.src = 'txt2img.php?t=' + cfg.text + '&c=' +  cfg.hoverColor + '&s='+toInt(o.getStyle('font-size'))+'&f='+o.getStyle('font-weight')+'&cs=0&a=' + o.getStyle('text-align') +'&w=' + cfg.width + '&h=' + cfg.height;				
						//console.log(e.type,cfg);
					}
					return true;
				},cfg);
				
				o.on("mouseleave",function(e,t,oo) 
				{
					//if(( e.target.localName == 'a') ) //do hover
					{
						var o = Ext.fly(t);
						var cfg = this;
						var i = (e.target.localName == 'img') ? o.dom : o.child('img',true);
						i.src = 'txt2img.php?t=' + cfg.text + '&c=' + cfg.color +'&s='+toInt(o.getStyle('font-size'))+'&f='+o.getStyle('font-weight')+'&cs=0&a=' + o.getStyle('text-align') +'&w=' + cfg.width + '&h=' + cfg.height;
						//console.log(e.type, cfg);
					}
					return true;
				}, cfg);
			} 							
		});
	});
}

