jQuery.fn.isPNGSafe = function() {
    var tmp = navigator.appName == 'Microsoft Internet Explorer' ? 1 : 0;
    var isIE6 = false;
    /*try {
    if (tmp) isIE6 = document.namespaces && (!document.documentMode || document.documentMode < 7) ? true : false;
    //isIE6 = document.documentMode < 7 ? 1 : 0;
    } catch (e) { }*/
    if (!isIE6 && tmp) {
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        var rv = -1;
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
        isIE6 = (rv < 8 && rv != -1);

    }
    return !isIE6;
};

jQuery.fn.PNGSafe = function(options) {
    var $this = this;
    try { ini_vml(); } catch (e) { }
    if (!jQuery.fn.isPNGSafe()) {
        var images = $this;
        images.each(function(idx) {
            if (this.tagName.toLowerCase() === 'img') {
                var object = jQuery(this);
                var divCont = jQuery('<div></div>');
                divCont.attr("id", object.attr("id"));
                divCont.attr("class", object.attr("class"));
                object.css("opacity", "auto");
                divCont.attr("style", object.attr("style"));
                divCont.css({ width: object.css("width"), height: object.css("height") })
                divCont.append('<xml:namespace ns="urn:schemas-microsoft-com:vml" prefix="vml"/>');
                var vmlImage = jQuery('<vml:image></vml:image>');
                vmlImage.css({ width: object.css("width"), height: object.css("height") })
                if (vmlImage.css("width") == "0px")
                    divCont.append("<span style='color:red'>El ancho de la imagen es cero</span>")
                vmlImage.css("behavior", "url(#default#VML)");
                vmlImage.css({ position: "absolute", visibility: "visible" });
                if (object.attr("onmouseover")) vmlImage.mouseover(function() { object.mouseover(); });
                if (object.attr("onmouseout")) vmlImage.mouseleave(function() { object.mouseleave() });
                var idMap = object.attr("usemap");
                if (idMap) {
                    vmlImage.css("cursor", "pointer");
                    vmlImage.click(function() {
                        var strHrefMap = jQuery(idMap + " area").attr("href");
                        if (strHrefMap.indexOf("javascript:") > -1) {
                            strHrefMap = strHrefMap.replace("javascript:", "");
                            eval(strHrefMap);
                        }
                        else {
                            window.location = strHrefMap;
                        }

                    })
                }
                vmlImage.attr("src", object.attr("src"));
                if (options) {
                    if (options.container) divCont.css(options.container);
                    if (options.image) vmlImage.css(options.image);
                }
                divCont.append(vmlImage);
                object.replaceWith(divCont);
            }
            else {
                var _this = jQuery(this);
                var sizingMethod = "scale";
                if (options)
                    sizingMethod = options.sizingMethod ? options.sizingMethod : "scale";
                _this.attr("style", _this.attr("style").replace(/alpha\(opacity=[^)]*\)/, ""));
                _this.css("filter", "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',sizingMethod='" + sizingMethod + "',src='" + _this.css("background-image").replace('url("', '').replace('")', '') + "')");
                _this.css("background-image", "none");
            }
        });
    }
    function ini_vml() {
        if (!jQuery.fn.isPNGSafe()) {
            if (document.namespaces) {
                if (document.namespaces['vml'] == null) {
                    try {
                        var s = document.createStyleSheet();
                        s.addRule("vml\\:*", "behavior: url(#default#VML);");
                        document.namespaces.add("vml", "urn:schemas-microsoft-com:vml");
                    } catch (e) { }
                }
            }
        }
    }
    return this;
};
jQuery.fn.PNGSafeGetSrc = function() {
    var $this = this;
    var url = "";
    if (!jQuery.fn.isPNGSafe()) {
        var pngImage = $this.find("image");
        var strFilter = $this.css("filter");
        if (pngImage.length > 0) {
            strFilter = $this.html();
            if (strFilter.length > 0) {
                var reg = /src[ ]*=[ ]*\"([^\"]*)\"/;
                var mach = reg.exec(strFilter);
                if (mach)
                    url = mach[1];
            }
        }
        else if (strFilter.length > 0) {
            var reg = /src[ ]*=[ ]*\'([^\']*)\'/;
            var mach = reg.exec(strFilter);
            if (mach)
                url = mach[1];
        }
    }
    else {
        if ($this.get(0).tagName.toLowerCase() === 'img') {
            url = $this.attr("src");
        }
        else {
            var strUrl = $this.css("background-image");
            if (strUrl.length > 0) {
                var reg = /url\(([^)]*)\)/;
                var mach = reg.exec(strUrl);
                if (mach)
                    url = mach[1];
            }
        }
    }
    if (url.length == 0) url = $this.attr("src");
    return url;
};
jQuery.fn.PNGSafeChange = function(strSrc, options) {
    var $this = this;
    if (!jQuery.fn.isPNGSafe()) {
        $this.each(function(idx) {
            var pngImage = jQuery(this).find("image");
            if (pngImage.length > 0) {
                pngImage.attr("src", strSrc);
            }
            else {
                var _this = jQuery(this);
                var sizingMethod = "scale";
                if (options)
                    sizingMethod = options.sizingMethod ? options.sizingMethod : "scale";
                _this.css("filter", "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',sizingMethod='" + sizingMethod + "',src='" + strSrc + "')");
                _this.css("background-image", "none");
            }
        });
    }
    else {
        $this.each(function(idx) {
            if (this.tagName.toLowerCase() === 'img')
                jQuery(this).attr("src", strSrc);
            else
                jQuery(this).css({ "background-image": "url(" + strSrc + ")" });
        });
    }
    return this;
};

jQuery.fn.PNGOpacity = function(selector, endval, totaltime, complete) {
    return this.each(function() {
        var atributte = "gamma";
        var interval = Math.ceil(1000 / 35);
        var totalframes = Math.ceil(totaltime / interval);
        var obj = this;
        var begin = parseInt(obj.attr(atributte), 10);
        if (obj.attr(atributte) == undefined) begin = 100;
        if (begin <= 1) begin = begin * 100;
        var end = endval;
        var change = (begin >= 0) ? end - begin : end;
        for (var i = 1; i <= totalframes; i++) {
            (function() {
                var frame = i;
                function pngChgOpcy() {
                    var opcy = EasingSwing(frame, begin, change, totalframes);
                    obj.attr(atributte, opcy + "%");
                }
                setTimeout(pngChgOpcy, interval * frame);
            })();
        }
        if (complete)
            setTimeout(complete, interval * (totalframes + 1));
    });
    function EasingSwing(t, b, c, d) {
        return c * Math.sqrt(1 - (t = t / d - 1) * t) + b;
    }
};
