/*******************************************************************************

PNGFix 1.1

Written by PJ Dietz
Modified 2008-09-16

Requires prototype.js

Adds the method pngfix() to all elements. When called, this method replaces the
transparent PNG background image or src image with a transparent GIF and uses
the IE Alpha Image Loader to display the PNG with transparency.

To use the method, simply call it on any element you wish to have proper
transparency. 

Event.observe(document, 'dom:loaded', function() {
    var gif = 'images/blank.gif';
    $('PngImg').pngfix(gif);
});

NOTE: If the script detects that the browser is anything other than IE 5.5 or
IE 6 on Windows, the pngfix() method added is a do-nothing, so no fears about
lots of errors or lots of overhead. 

*******************************************************************************/

PNGFix = {
    
    // Path to the transparent GIF.
    transparent: '/explorer/images/transparent.gif',
    
    // Setup the pngfix() method.
    // This is PNGFix.IE6pngfix() for IE6; otherwise, it's a do-nothing.
    init: function() {
    
         // Ensure the browser is IE 5.5 or 6 on Windows.
        var agent = navigator.userAgent.toLowerCase();
        var is_ie = ( (agent.indexOf("mac") == -1) &&
                    ( (agent.indexOf("msie 5.5") != -1) || 
                      (agent.indexOf("msie 6") != -1) ) );

        if (is_ie)
            Element.addMethods({ pngfix: PNGFix.IE6pngfix });
        else
            Element.addMethods({ pngfix: function(elem) {} });
    
    },
    
    // Actual pngfix method, attached for IE6 only.
    IE6pngfix: function(elem) {
    
        // IMG tags
        if ( (elem.tagName == 'IMG') && (elem.src.include('png')) ) 
        {
            var alphaImgSrc = elem.src;
            var sizingMethod = 'scale';
            elem.src = PNGFix.transparent;
        }
        
        // CSS Background Image
        else if (elem.getStyle('background-image').include('png')) 
        {
            // Store the background color.
            var bgColor = '';
            if (elem.getStyle('background-color'))
                bgColor = elem.getStyle('background-color') + ' ';
            
            // Stores url("/image/spam.png") as /images/spam.png
            var elemBg = elem.getStyle('background-image');
            var alphaImgSrc = elemBg.slice(5, elemBg.length - 2);
            var sizingMethod = 'crop';
            
            // Apply the transparent image as the background.
            elem.setStyle({
                background: '#{color} url("#{img}")'.interpolate({ 
                    color: bgColor,
                    img: PNGFix.transparent 
                })
            });
          
        }
        
        // Apply the filter to the object (IMG or element with background)
        if (alphaImgSrc)
        {
            elem.runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="#{alphaImgSrc}",sizingMethod="#{sizingMethod}")'.interpolate({
                alphaImgSrc: alphaImgSrc, sizingMethod: sizingMethod
            });
        }

    }
    
};

// Initialize imediately.
PNGFix.init();

