/*!
 * jQuery PNG Plugin v1.0
 *
 * Copyright (c) 2010 Wayne Haffenden
 * http://www.waynehaffenden.com/Blog/jQuery-PNG-Plugin
 *
 * $Id: jquery.png.js, v 1.0 2010-01-29 15:51:46Z whaffenden $
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 */
;(function($) {
    /**
     * The default PNG plugin settings that are static over all instances, unless overridden for a specific instance.
     */
    $.png = {
        defaults: {
            shimImage: 'images/pic/block.gif',
            force: false,
            children: true
        }
    };

    /**
     * The png() function fixes both the Internet Explorer 6 24-bit PNG transparency problem and the Internet Explorer 7 & 8 multiple
     * filter issue. If any options are specified, they will override the static default settings.
     */
    $.fn.png = function(options) {

        // Defines the local settings.
        var settings = $.extend({}, $.png.defaults, options);

        // Determines whether to run the plugin.
        if ($.browser.msie && (settings.force === true || $.browser.version === '6.0')) {
            /**
             * Applies the fix to the given element, if appropriate.
             */
            var fixElement = function(element) {
                var alphaLoader = 'progid:DXImageTransform.Microsoft.AlphaImageLoader',
                    regexp = /\.png/i;

                // Determines whether the fix needs to be applied to an image element or an element containing a CSS PNG background
                // image. Other element types are ignored.
                if (element.attr('src') !== undefined && element.attr('src').match(regexp)) {
                    // Forces the elements height and width attributes.
                    element.attr('height', element.height());
                    element.attr('width', element.width());

                    // Physically applies the filter to the given element.
                    element.css('filter', alphaLoader + '(enabled=true, src=\'' + element.attr('src') + '\', sizingMethod=\'crop\')');

                    // Replaces the original image source with the shim image.
                    element.attr('src', settings.shimImage);
                } else if (element.css('background-image').match(regexp)) {
                    var imagePath = element.css('background-image').split('"')[1],
                        sizing = (element.css('background-repeat') === 'no-repeat' ? 'crop' : 'scale');
                    
                    // Physically applies the filter to the given element.
                    element.css('filter', alphaLoader + '(enabled=true, src=\'' + imagePath + '\', sizingMethod=\'' + sizing + '\')');

                    // Removes the background image from the element.
                    element.css('background-image', 'none');
                }
            };

            // Traverses through the given element set and applies the fix to appropriate elements.
            this.each(function() {
                var element = $(this);

                // Applies the fix to the given element.
                fixElement(element);

                // Determines whether to apply the fix to appropriate child elements.
                if (settings.children === true) {
                    element.find('*').each(function() {
                        fixElement($(this));
                    });
                }
            });
        }

        return this;
    };
})(jQuery);