/**
 * PNGFix
 * Transparent PNGs in IE6
 *
 * Copyright (c) 2009 PJ Dietz
 * Version: 1.00
 * Licensed under the MIT license:
 * http://www.opensource.org/licenses/mit-license.php
 */

/*global jQuery */

(function ($) {

    var 
        /* Path to a transparent PNG */
        transparent, 
        
        /* Used for determining browser */
        agent, isIE6, 
        
        /* Function for determining if an image is a PNG */ 
        isPNG, 
        
        /* Function, defined as a do-nothing for non-IE */
        pngfix, 
        
        /* Function, defined only if IE. */     
        pngfixImg,
        pngfixCssSimple,
        pngfixCssOffset;
    
    
    
    // Path to a transparent GIF.
    // Customize for your site.
    transparent = "/images/transparent.gif";
    
    // Ensure the browser is IE 5.5 or 6 on Windows.
    agent = navigator.userAgent.toLowerCase();
    isIE6 = (agent.indexOf("mac") === -1 && agent.indexOf("msie 6") !== -1);
    
    // Determine if a URL points to a PNG.
    isPNG = function (url) {
        
        var ext;
        
        if (typeof url === "string") {
            url = url.split(".");
            ext = url[url.length - 1];

            return ext.toLowerCase() === "png";
        }
        
        return false;
        
    };
    
    
    
    // Define the pngfix function spcifically for IE6.
    // Otherwise, supply a do-nothing function for all good browsers.
    if (isIE6) {
    
        // Replace the src of an IMG tag.
        pngfixImg = function (elem) {
            
            var filter;
            
            // Apply the filter.
            filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(';
            filter += 'src="' + elem.attr("src") + '",';
            filter += 'sizingMethod="scale")';
            elem.get(0).runtimeStyle.filter = filter;

            // Replace the src with the global transparent.
            elem.attr("src", transparent);
            
        };
       
        // Replace a CSS background with a position of 0,0.
        pngfixCssSimple = function (elem, bgImg) {
       
            var alphaImgSrc, filter;
            
            // Stores url("/image/spam.png") as /images/spam.png
            alphaImgSrc = bgImg.slice(5, bgImg.length - 2);
            
            // Apply the filter
            filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(';
            filter += 'src="' + alphaImgSrc + '",';
            filter += 'sizingMethod="crop")';
            elem.get(0).runtimeStyle.filter = filter;

            // Set the transparent GIF as the background.
            elem.css("background-image", "url(" + transparent + ")");
                            
        };
       
        // Replace a CSS background offset with background-position.
        pngfixCssOffset = function (elem, bgImg, bgPosX, bgPosY) {
       
            var alphaImgSrc, child, width, height;

            // Stores url("/image/spam.png") as /images/spam.png
            alphaImgSrc = bgImg.slice(5, bgImg.length - 2);
            
            
            // Make sure elem is ready to have an absolutely positoned child.
            if (elem.css("position") === "static") {
                elem.css("position", "relative");
            }
            
            // Ensure the element will clip the contents.
            elem.css("overflow", "hidden");
            
            // Replace the original background with the global transparent.
            elem.css("background-image", "url(" + transparent + ")");
            
            
            // Create a new element to contain the image.
            child = document.createElement('img');
            child.src = alphaImgSrc;
            
            // Determine the width and height.
            width = child.width;
            height = child.height;
            
            // Extend the child. 
            child = $(child);
               
            // Apply CSS width and height.
            child.css("width", width + "px");
            child.css("height", height + "px");
            child.css("position", "absolute");
            
            // Position Horizontally
            if (bgPosX === "right") {
                child.css("right", 0);
            }
            else {
                child.css("left", bgPosX);
            }
            
            // Position Vertically
            if (bgPosY === "bottom") {
                child.css("bottom", 0);
            }
            else {
                child.css("top", bgPosY);
            }

                        
            // Apply the filter
            pngfixImg(child);

            // Redirect any clicks to the parent.
            child.click(function (event) {
                $(this).parent().click();
            });

            // Append the child.
            elem.append(child);
                  
        };
    
    
        // Main PNG replace function. Delegates to the above functions.
        pngfix = function (elem) {
            
            var tagName, bgImg, bgPosX, bgPosY;
            
            // Ensure elem is a jQuery extended element.
            elem = $(elem);
  
            tagName = elem.get(0).tagName;
            
            // IMG
            if (tagName === "IMG" && isPNG(elem.attr("src"))) {
                pngfixImg(elem);
            }               
            
            else {

                // Store the background image.
                bgImg = elem.css("background-image");
                            
                if (bgImg.toLowerCase().indexOf(".png") !== -1) {
        
                    // IE6 uses these funky CSS rules for background position.
                    bgPosX = elem.css("background-position-x");
                    bgPosY = elem.css("background-position-y");
                        
                    // Use the simple function that just replaces the background.   
                    if (bgPosX === "0px" && bgPosY === "0px") {
                        pngfixCssSimple(elem, bgImg);
                    }
                    
                    // Use the more complex function that adds and positions a new element.
                    else {
                        pngfixCssOffset(elem, bgImg, bgPosX, bgPosY);
                    }
        
                }
            
            }
            
        };
    
    }
    
    // Define a do-nothing function for non-suck browsers.
    else {
        pngfix = function (elem) {};
    } 
    
        

    // -------------------------------------------------------------------------
    // Extend jQuery 

    if (typeof $.fn.pngfix === "undefined") {
     
        $.fn.extend({  
            
            pngfix: function () {
                return this.each(function () {
                    pngfix(this);
                });
            }
           
        });    
        
    }      

}(jQuery));

/*jslint white: true, browser: true, onevar: true, undef: true, nomen: true, 
  eqeqeq: true, plusplus: true, bitwise: true, regexp: true, newcap: true, immed: true */
  
