﻿$.fn.DiFR = function(options) {
    var defaults = { color: "FFFFFF", spacing: "normal", style: "normal", size: "16", kerning: "normal", backgroundColor: "transparent", rollover: false, rolloverColor: "0000EE", rolloverClass: "hover", asBackground: false, lineHeight: 16, cacheXmlPath: VirtualDirectory + "/_config/difr-cache.xml", cacheImagePath: "/images/difr/", DiFRPath: VirtualDirectory + "/services/difr.ashx" }; var opts = $.extend({}, defaults, options); var init = function(initOptions) { defaults = $.extend(defaults, initOptions); }
    var randomnumber = Math.floor(Math.random() * 11009343)
    var cacheXml = fnLoadXml(opts.cacheXmlPath + "?t=" + randomnumber); var selector = this.selector; var tmp = 1; return this.each(function() {
        var item = $(this); var content = $.trim(item.html()); content = content.replace("<strong>", ""); content = content.replace("</strong>", ""); content = content.replace("<em>", ""); content = content.replace("</em>", ""); content = content.replace("'", "%27"); if (item.css("text-transform") == "uppercase") content = content.toUpperCase(); var font = {}; font.size = item.css("font-size").replace("px", ""); var familyComma = item.css("font-family").indexOf(","); if (familyComma > -1) { font.family = "HapticPro-ExtraBold"; }
        else { font.family = "HapticPro-ExtraBold"; }
        font.color = item.css("color"); if (!$.browser.msie) font.color = rgbToHex(font.color); font.color = font.color.replace("#", ""); if (options.kerning) font.kerning = options.kerning; else font.kerning = item.css("letter-spacing").replace("px", ""); if (font.kerning == "normal") font.kerning = 0; font.lineHeight = item.css("line-height").replace("px", ""); if (font.lineHeight == "normal") font.lineHeight = opts.lineHeight; font.style = item.css("font-weight"); if (font.style == "normal") font.style = "regular"; try { if (parseInt(font.style) > 100) font.style = "bold" } catch (e) { }
        font.wordSpacing = item.css("word-spacing").replace("px", ""); if (font.wordSpacing == 0) font.wordSpacing = opts.spacing; if (opts.rollover) { if (!$.browser.msie) opts.rolloverColor = rgbToHex(item.css("outline-color")).replace("#", ""); else opts.rolloverColor = item.css("outline-color").replace("#", ""); }
        cacheFileName = fnCompressString(removeAll(removeAll(content, " "), ".") + font.size + font.color + font.lineHeight + font.style + font.kerning); if (opts.rollover) { cacheFileName += "_" + opts.rolloverColor; }
        cacheFileName = cacheFileName.toUpperCase(); cacheFileName = removeAll(cacheFileName, "<BR>"); cacheFileName = removeAll(cacheFileName, "<BR />"); cacheFileName = removeAll(cacheFileName, "."); cacheFileName = removeAll(cacheFileName, ","); cacheFileName = removeAll(cacheFileName, "'"); cacheFileName = removeAll(cacheFileName, "’"); var isCached = false; var cachedImage = $(cacheXml).find("image[file=" + cacheFileName + "]"); if (cachedImage.length > 0) { isCached = true; cacheWidth = parseFloat(cachedImage.attr("w")); cacheHeight = parseFloat(cachedImage.attr("h")); }
        var difrSource = new String(); if (isCached) { difrSource = VirtualDirectory + opts.cacheImagePath + cacheFileName + ".png"; }
        else {
            difrSource = opts.DiFRPath + "?text=" + encodeURIComponent(content) + "&font=" + font.family + "&style=" + font.style + "&size=" + font.size + "&color=" + font.color + "&backgroundColor=" + opts.backgroundColor + "&lineheight=" + font.lineHeight + "&kerning=" + font.kerning + "&spacing=" + font.wordSpacing + "&withRollover=" + opts.rollover +
((opts.rollover) ? "&rolloverColor=" + opts.rolloverColor : "");
        }
        var image = new Image()
        image.src = difrSource; item.empty().append("<span style='display: none'>" + content + "</span>"); if (opts.rollover) {
            runOnLoad(function() {
                var rolloverHeight = 0; rolloverHeight = parseFloat(image.height) / 2; item.css({ background: "transparent url(" + difrSource + ") no-repeat", display: "block", height: rolloverHeight + "px", width: image.width + "px" }); item.hover(function() {
                    var hoverShift = rolloverHeight; if ($.browser.msie && $.browser.version == "6.0") { hoverShift = hoverShift - 1; }
                    item.css("background-position", "0 -" + hoverShift + "px");
                }, function() { item.css("background-position", "0 0"); })
            });
        } else { item.append(image); }
        item.show();
    });
}; function padZero(number, length) {
    var str = '' + number; while (str.length < length) { str = '0' + str; }
    return str;
}
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 fnLoadXml(sURL) { var returnXml; var xResponse = $.ajax({ type: "GET", url: sURL, dataType: "xml", async: false, success: function(xml) { } }); return xResponse.responseXML; }
function createXmlDOMObject(xmlString) {
    var xmlDoc = null; if (!window.DOMParser) { xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async = false; xmlDoc.loadXML(xmlString); }
    else { parser = new DOMParser(); xmlDoc = parser.parseFromString(xmlString, "text/xml"); }
    return xmlDoc;
}
function processXmlDoc(xmlDoc) { }
function parseXml(xml) {
    if ($.browser.msie) { var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.loadXML(xml); xml = xmlDoc; }
    return xml;
}
function getRGBzeros(scolorVal) {
    if (scolorVal.length < 2) { scolorVal = "0" + scolorVal; }
    return scolorVal;
}
function fnCompressString(strIn) { strIn = removeAll(strIn); strIn = removeAll(strIn, "'"); strIn = removeAll(strIn, ":"); strIn = removeAll(strIn, "&COPY;"); strIn = removeAll(strIn, "&AMP;"); strIn = removeAll(strIn, "&amp;"); strIn = removeAll(strIn, ";"); strIn = removeAll(strIn, "#"); strIn = removeAll(strIn, "?"); strIn = removeAll(strIn, "%20"); strIn = removeAll(strIn, "�"); strIn = removeAll(strIn, "&"); strIn = removeAll(strIn, ","); strIn = removeAll(strIn, "&TRADE;"); strIn = removeAll(strIn, "&REG;"); strIn = removeAll(strIn, "™"); strIn = removeAll(strIn, "®"); strIn = removeAll(strIn, "&quot;"); 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 enableDocumentSelection(enable) {
    if (enable) { document.onselectstart = _original_onselectstart; }
    else { _original_onselectstart = document.onselectstart; document.onselectstart = function() { return false; } } 
}
