﻿var arVersion = navigator.appVersion.split("MSIE");
var version = parseFloat(arVersion[1]);
var cnt = 0;

function FIXpng(img)
{
    if ((version >= 5.5) && (version < 7.0) && (document.body.filters))
    {
        
        var imgID = (img.id) ? "id='" + img.id + "' " : ""
        var imgClass = (img.className) ? "class='" + img.className + "' " : ""
        var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
        var imgStyle = "display:inline-block;" + img.style.cssText
        if (img.align == "left") imgStyle = "float:left;" + imgStyle
        if (img.align == "right") imgStyle = "float:right;" + imgStyle
        if (img.parentElementhref) imgStyle = "cursor:hand;" + imgStyle
        var strNewHTML = "<span " + imgID + imgClass + imgTitle;
        strNewHTML += " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";";
        strNewHTML += "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
        strNewHTML += "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
        img.outerHTML = strNewHTML;
    }
}

var FontReplacer = {

    _url: '/FontImageHandler.ashx?font=[font]&text=[text]&height=[height]&color=[color]&bcolor=[bcolor]&size=[size]&pt=[paddingTop]',
    _enableTransparency: true,
    _forceHeight: true,
    _forceWidth: false,

    replace: function(VDir, tag, className, font)
    {
        var _urlSave = this._url;
        if (VDir != '/') this._url = VDir + this._url;
        this.replaceIn(document, tag, className, font);
        this._url = _urlSave;
    },

    replaceIn: function(root, tag, className, font)
    {
        var elements = this.getElementsByClassName(root, tag, className);
        this.replaceElements(elements, font);
    },

    replaceElements: function(elements, font)
    {

        for (var i = 0; i < elements.length; i++)
        {
            var el = elements[i];

            // only handle text nodes
            if (el.firstChild.nodeType != 3)
                continue;


            // get attributes of elements
            var title = el.firstChild.nodeValue;

            var text = encodeURIComponent(title);
            var color = this.getStyle(el, 'color');
            var bcolor = this.getStyle(el, 'backgroundColor');
            var height = this.getStyle(el, 'height');
            var width = this.getStyle(el, 'width');
            var size = this.getStyle(el, 'fontSize');
            var paddingTop = 0; // this.getStyle(el, 'paddingTop');


            if (color.indexOf('rgb') != -1)
                color = this.cssRgb2Hex(color);

            if (bcolor == 'transparent' && !this._enableTransparency)
                bcolor = '';

            bcolor = "";

            if (bcolor.indexOf('rgb') != -1)
                bcolor = this.cssRgb2Hex(bcolor);

            color = color.replace('#', '');
            bcolor = bcolor.replace('#', '');

            //            if (size == '13px') { size = '12px' }
            //            else if (size == '12px') { size = '11px' }
            //            else if (size == '11px') size = '10px';

            // create image for replacement
            var url = this._url.replace('[text]', text).replace('[font]', font).replace('[bcolor]', bcolor).replace('[color]', color).replace('[size]', size).replace('[paddingTop]', paddingTop);
            if (this._forceWidth)
                url = url.replace('[width]', width);
            if (this._forceHeight)
                url = url.replace('[height]', height);


            var img = new Image();
            img.target = el;
            img.parentElementhref = el.href;
            img.src = url;
            img.alt = title;

            if (img.height == 0)
            {
                img.loadHandler = function()
                {
                    cnt++;
                    //window.status += cnt;
                    //this.target.replaceChild(this, this.target.firstChild);
                    FIXpng(this);
                };
                img.onload = img.loadHandler;
            }

            el.replaceChild(img, el.firstChild);
            if (img.height == 0) FIXpng(img);



            //window.status = url;

            //el.replaceChild(img, el.firstChild);
            //if (img.width > 0) FIXpng(img);
        }
    },

    // this doesn't always work for font sizes, so we're using it for colors mostly
    getStyle: function(el, prop)
    {
        if (document.defaultView && document.defaultView.getComputedStyle)
        {
            return document.defaultView.getComputedStyle(el, null)[prop];
        } else if (el.currentStyle)
        {
            return el.currentStyle[prop];
        } else
        {
            return el.style[prop];
        }
    },

    cssRgb2Hex: function(color)
    {
        var c = color.replace('rgb(', '').replace(')', '').split(',');
        return this.rbg2hex(parseInt(c[0]), parseInt(c[1]), parseInt(c[2]));
    },

    rbg2hex: function(red, green, blue)
    {
        var r = red.toString(16);
        var g = green.toString(16);
        var b = blue.toString(16);

        var c =
		((r.toString().length == 1) ? '0' : '') + r +
		((g.toString().length == 1) ? '0' : '') + g +
		((b.toString().length == 1) ? '0' : '') + b;

        return c;
    },

    // credit: http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/
    getElementsByClassName: function(oElm, strTagName, strClassName)
    {
        var arrElements = (strTagName == "*" && oElm.all) ? oElm.all : oElm.getElementsByTagName(strTagName);
        var arrReturnElements = new Array();
        strClassName = strClassName.replace(/\-/g, "\\-");
        var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
        var oElement;
        for (var i = 0; i < arrElements.length; i++)
        {
            oElement = arrElements[i];
            if (strClassName == '')
            {
                arrReturnElements.push(oElement);
            }
            else
            {
                if (oRegExp.test(oElement.className))
                {
                    arrReturnElements.push(oElement);
                }
            }
        }
        return (arrReturnElements)
    }
};