// JavaScript Document
(function($){
 $.fn.renderText = function(options) {

    return this.each(function() {
		curObj = $(this);
		curText = curObj.html();
		metrics = textMetrics(curText, curObj);
		//if (curObj.css('width') != metrics.Width) metrics.Width = curObj.css('width').split('px')[0];
		//if (curObj.css('height') != metrics.Height) metrics.Height = curObj.css('height').split('px')[0];
		//Possible combinations are regular, bold, boldItalic
		curStyle = '';
		if (curObj.css("font-weight") == 'bold') curStyle = 'bold';
		if (curObj.css("font-style") == 'italic') curStyle = curStyle + 'Italic';
		
		curColor = curObj.css("color");
		isRGB = (curColor.match('rgb') != null);
		curColor = isRGB ? rgb2hex(curColor) : curColor.split('#')[1];
		
		
		var options = {
			className: "",	
			imgWidth: metrics.Width,
			lineHeight: metrics.lineHeight,
			imgHeight: metrics.Height,
			fontSize: metrics.fontSize,
			fontColor: curColor,
			fontFace: curObj.css("font-family").split(",")[0],
			fontStyle: curStyle,
			textAlign: curObj.css("text-align"),
			encodedText: curObj.attr('encoded') != undefined ? curObj.attr('encoded') : ""
		}
		
		//var styles = $.extend(options, styles);
		outputText = curText.replace("'","%27");
		if (outputText == '') return;
		renderedImg = '<img border="0" '+
						'title="' + unescape(outputText) + '"' +
						'alt="' +  unescape(outputText) + '"' +
						'class="' +  options.className + '"' +
						//'height="' +  options.imgHeight + '"' +
						'src="/modules/localCFImageText.cfm?' + 
							'&outputText=' + escape(outputText) +
							'&encodedText=' + options.encodedText +
							'&lineHeight=' + options.lineHeight +
							'&textAlign=' + options.textAlign +
							'&fontSize=' + options.fontSize + 
							'&fontFace=' + options.fontFace + 
							'&fontStyle=' + options.fontStyle + 
							'&fontColor=' + options.fontColor + 
							'&imgHeight=' + options.imgHeight +
							'&imgWidth=' +  options.imgWidth + 
						'"' +
					'/>';
		curObj.html(renderedImg);
		//console.log(renderedImg);
    });

 };
})(jQuery);

//Function to determine the text size of a given object

function textMetrics(text, styledObj){
	tempID = 'tempSpan22';
	$('body').append('<span id="' + tempID + '"></span>');
	$('#' + tempID).html(curText);
	$('#' + tempID).css('font-size', styledObj.css('font-size'));
	$('#' + tempID).css('font-style', styledObj.css('font-style'));
	$('#' + tempID).css('font-weight', styledObj.css('font-weight'));
	$('#' + tempID).css('font-family', styledObj.css('font-family'));
	$('#' + tempID).css('line-height', styledObj.css('line-height'));
	$('#' + tempID).css('text-transform', styledObj.css('text-transform'));
	$('#' + tempID).css('letter-spacing',styledObj.css('letter-spacing'));
	$('#' + tempID).hide();
	var metrics = {
		Width: $('#' + tempID).width(),
		Height: $('#' + tempID).height(),
		lineHeight: $('#' + tempID).css('line-height').split('px')[0],
		fontSize: styledObj.css("font-size").split('px')[0]
	}
	return metrics;
}

//Function to convert hex format to a rgb color
function rgb2hex(rgb) {
	rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
	function hex(x) {
		hexDigits = new Array("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");
		return isNaN(x) ? "00" : hexDigits[(x - x % 16) / 16] + hexDigits[x % 16];
	}
	return hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
}