/**
 * @author Michael.Howlett
 * requires 'runbOnLoad.js' for image width and height
 */




$(document).ready(function() {

	$('.difr').each(

		function() {

			var textcontent = $(this).text();
			var htmlcontent = $(this).html();
			var item = $(this)


			// settings from css
			var fontSize = $(this).css("font-size");
			fontSize = fontSize.substring(0, (fontSize.length - 2))

			//var font = $(this).css("font-family")
			var font = "INTR____";

			var colour = $(this).css("color")
			if (!$.browser.msie) {
				colour = rgbToHex(colour)
			}
			colour = colour.substring(1, colour.length)


			var kerning = 5
			if (($(this).css("letter-spacing") != "normal")) {
				kerning = $(this).css("letter-spacing")
				kerning = kerning.substring(0, (kerning.length - 2))
			}

			if ($(this).css("text-transform") == "uppercase") {
				textcontent = textcontent.toUpperCase();
			}






			// load the image
			var image = new Image();






			image.src = VirtualDirectory + "/services/fontImageReplace.ashx?text=" + escape(textcontent) + "&font=../fonts/" + font + ".ttf&size=" + fontSize + "&colour=" + colour + "&kerning=" + kerning;

			//hide the text
			$(this).html("<span>" + htmlcontent + "</span>")
			//$(this).css("background", "transparent  url('" + image.src + "') no-repeat scroll center top")
			$(this).css("background-image", "url('" + image.src + "')")

			// once loaded - format the image
			runOnLoad(function() {
				$(item).css({ width: image.width, height: image.height });
				$(item).ifixpng();
				
			});

		}
	);



	function rgbToHex(rgb) {
		var rgbvals = /rgb\((.+),(.+),(.+)\)/i.exec(rgb);
		var rval = parseInt(rgbvals[1]);
		var gval = parseInt(rgbvals[2]);
		var bval = parseInt(rgbvals[3]);
		return '#' + (rval.toString(16) + gval.toString(16) + bval.toString(16)).toUpperCase();

	}







});
