/**
* --------------------------------------------------------------------
* jQuery-Plugin "pngFix"
* Version: 1.2, 09.03.2009
* by Andreas Eberhard, andreas.eberhard@gmail.com
*                      http://jquery.andreaseberhard.de/
*
* Copyright (c) 2007 Andreas Eberhard
* Licensed under GPL (http://www.opensource.org/licenses/gpl-license.php)
*
* Changelog:
*    09.03.2009 Version 1.2
*    - Update for jQuery 1.3.x, removed @ from selectors
*    11.09.2007 Version 1.1
*    - removed noConflict
*    - added png-support for input type=image
*    - 01.08.2007 CSS background-image support extension added by Scott Jehl, scott@filamentgroup.com, http://www.filamentgroup.com
*    31.05.2007 initial Version 1.0
* --------------------------------------------------------------------
* @example $(function(){$(document).pngFix();});
* @desc Fixes all PNG's in the document on document.ready
*
* jQuery(function(){jQuery(document).pngFix();});
* @desc Fixes all PNG's in the document on document.ready when using noConflict
*
* @example $(function(){$('div.examples').pngFix();});
* @desc Fixes all PNG's within div with class examples
*
* @example $(function(){$('div.examples').pngFix( { blankgif:'ext.gif' } );});
* @desc Fixes all PNG's within div with class examples, provides blank gif for input with png
* --------------------------------------------------------------------
*/

(function($) {

    jQuery.fn.pngFix = function(settings) {

        // Settings
        settings = jQuery.extend({
            blankgif: '/images/1x1.GIF'
        }, settings);

        var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
        var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);

        if (jQuery.browser.msie && (ie55 || ie6)) {

            //fix images with png-source
            jQuery(this).find("img[src$=.png]").each(function() {
                //alert(jQuery(this).attr('src'));
                var width = jQuery(this).width();
                if (width == 0) {
                    var t = new Image();
                    t.src = jQuery(this).attr('src');
                    width = t.width;
                    //alert(t.width);
                }

                var height = 0;
                if (height == 0) {
                    var t = new Image();
                    t.src = jQuery(this).attr('src');
                    height = t.height;
                    //alert(t.height);
                }



                jQuery(this).css("width", width);
                jQuery(this).css("height", height);
                jQuery(this).css("filter", "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + jQuery(this).attr('src') + "', sizingMethod='scale')");

                jQuery(this).attr('src', settings.blankgif);

            });

            // fix css background pngs
            jQuery(this).find("*").each(function() {
                var mode = 'scale';
                if (jQuery(this).css('background-repeat') == 'no-repeat') {
                    mode = 'crop';
                }
                var bgIMG = jQuery(this).css('background-image');
                if (bgIMG.indexOf(".png") != -1) {
                    var iebg = bgIMG.split('url("')[1].split('")')[0];
                    jQuery(this).css('background-image', 'none');
                    jQuery(this).get(0).runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + iebg + "',sizingMethod='" + mode + "')";
                }
            });


            //fix input with png-source
            jQuery(this).find("input[src$=.png]").each(function() {
                var bgIMG = jQuery(this).attr('src');
                var width = jQuery(this).width();
                if (width == 0) {
                    var t = new Image();
                    t.src = jQuery(this).attr('src');
                    width = t.width;
                    //alert(t.width);
                }

                var height = 0;
                if (height == 0) {
                    var t = new Image();
                    t.src = jQuery(this).attr('src');
                    height = t.height;
                    //alert(t.height);
                }

                jQuery(this).css("width", width);
                jQuery(this).css("height", height);
                jQuery(this).get(0).runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + bgIMG + '\', sizingMethod=\'scale\');';
                jQuery(this).attr('src', settings.blankgif)
            });

            //fix a and input position
            jQuery(this).find("a").each(function() {

                jQuery(this).css("position", "relative");
            });
            jQuery(this).find("input").each(function() {
                jQuery(this).css("position", "relative");
            });

        }

        return jQuery;

    };

})(jQuery);
