/**
 * @author Michael.Howlett
 * requires 'runOnLoad.js' for image width and height
 */




$(document).ready(function() {
    //get difr-cache xml
    var difrCacheXml;
    difrCacheXml = fnLoadXml(SiteVirtualDirectory + "/_config/difr-cache.xml");


    $('.difr').each(

		function() {


			/*********************************************
			* PROPERTIES
			**********************************************/
		    var textcontent = $("span", this).html();
		    var htmlcontent = $(this).html();
		    var difrItem = $(this);
		    var rollOverQuery = "";
		    var hasRollover = false



			/*********************************************
			* FORMAT THE IMAGE FROM THE CSS
			**********************************************/
		    var fontSize = $(this).css("font-size");
		    fontSize = fontSize.substring(0, (fontSize.length - 2));

		    var font = $(this).css("font-family");

		    var colour = $(this).css("color");

		    if (!$.browser.msie) {
		        colour = rgbToHex(colour);
		    }
		    colour = colour.substring(1, colour.length);

		    var kerning = 1
		    if (($(this).css("letter-spacing") != "normal")) {
		        kerning = $(this).css("letter-spacing");
		        kerning = kerning.substring(0, (kerning.length - 2));
		    }

		    var lineheight = 30
		    if (($(this).css("line-height") != "normal")) {
		        lineheight = $(this).css("line-height");
		        lineheight = lineheight.substring(0, (lineheight.length - 2));
		    }

		    if ($(this).css("text-transform") == "uppercase" && textcontent) {
		        textcontent = textcontent.toUpperCase();
		    }

		    // check to see if the element has a rollover
		    if ($(this).hasClass('hover')) {
		        hasRollover = true;
		        rollOverQuery = "&isRlOvr=true&hovcolr=110C2A";
		    }


			/*********************************************
			* FORMAT THE REQUEST
			**********************************************/
		    var sFileName = fnCompressString(textcontent + fontSize + colour + kerning);
		    var bImgCached = false;
		    var iCImgW = 0;
		    var iCImgH = 0;
		    
		    // load the image
		    var image = new Image();

		    $(difrCacheXml).find("image").each(function() {
		        if ($(this).attr("file") == sFileName + ".png") {
		            iCImgW = parseFloat($(this).attr("w"));
		            iCImgH = parseFloat($(this).attr("h"));
		            bImgCached = true;
		        }
		    });

		    if (bImgCached) {
		        image.src = SiteVirtualDirectory + "/images/difr/" + sFileName + ".png";
		        image.width = iCImgW;
		        image.height = iCImgH;

		        //$(this).css("background", "transparent  url(" + image.src + ") no-repeat scroll top left");
		        this.appendChild(image);

		        
		    } else {
		        //service call for the difr system. edit to change settings.
		        //image.src = "/services/fontImageReplace.ashx?text=" + escape(textcontent) + "&font=../fonts/" + font + ".ttf&size=" + fontSize + "&colour=" + colour + "&kerning=" + kerning + "&isRlOvr=true&hovcolr=110C2A&useGif=true";

		        // build the request
		        var source = SiteVirtualDirectory + "/services/fontImageReplace.ashx?text=" + escape(textcontent) + "&size=" + fontSize + "&colour=" + colour + "&lineheight=" + lineheight + "&kerning=" + kerning + rollOverQuery;

		        // load the image
		        $(difrItem).image(source, function() {
		            var span = "<span>" + textcontent + "</span>"
		        });
		    
		    }

		}
	);






});


/*********************************************
* IMAGE LOADER
**********************************************/
$.fn.image = function(src, f){
 return this.each(function(){
        var i = new Image();
		i.src = src;
		i.onload = f;
		
		
		//if ($.browser.msie && $.browser.version == "6.0") {
		this.appendChild(i);
	    if ($.browser.msie && $.browser.version == "6.0") {
		    //DD_belatedPNG.fix($(this));
		}
			//$(this).css({ width: i.width, height: i.height });
		//}else{
			//$(this).css("background", "transparent  url(" + i.src + ") no-repeat scroll left top");
			//$(this).css({ width: i.width, height: i.height });
		//}
		
    });
 }



/*********************************************
* HELPERS
**********************************************/

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 '#' + (getRGBzeros(rval.toString(16)) + getRGBzeros(gval.toString(16)) + getRGBzeros(bval.toString(16))).toUpperCase();

}

function getRGBzeros(sColourVal) {
	if (sColourVal.length < 2) {
		sColourVal = "0" + sColourVal;
	}
	return sColourVal;
}


function fnCompressString(strIn){

//was using hashing, found it was overkill - oliver

    strIn = removeAll(strIn," ");
    strIn = removeAll(strIn,".");
    strIn = removeAll(strIn,":");
    strIn = removeAll(strIn,"&copy;");
    strIn = removeAll(strIn,";");
    strIn = removeAll(strIn,"#");
    //strIn = removeAll(strIn,"?");
    strIn = removeAll(strIn,"%20");
    strIn = removeAll(strIn,"©");
    strIn = removeAll(strIn,"&");
    return strIn;    
}

function removeAll(strIn, reStr){
    var strReturn = "";
    strArr = strIn.split(reStr);
    for(var i = 0;i<strArr.length;i++)
    {
       strReturn = strReturn + strArr[i].replace(reStr,"");
    }
    return strReturn;
}

function ascii_value (c)
{
	// restrict input to a single character
	c = c . charAt (0);

	// loop through all possible ASCII values
	var i;
	for (i = 0; i < 256; ++ i)
	{
		// convert i into a 2-digit hex string
		var h = i . toString (16);
		if (h . length == 1)
			h = "0" + h;

		// insert a % character into the string
		h = "%" + h;

		// determine the character represented by the escape code
		h = unescape (h);

		// if the characters match, we've found the ASCII value
		if (h == c)
			break;
	}
	return i;
}


function fnLoadXml(sURL) {
    var xResponse = $.ajax({
        type: "GET",
        url: sURL,
        dataType: "xml",
        async: false
    });
    return xResponse.responseXML;
}