﻿/**
* --------------------------------------------------------------------
* jQuery-Plugin "pngFix" (valley edition)
* Version: 1.0, 16-02-2009
* by Lucien Immink, lucien@thevalley.nl
* http://www.thevalley.nl
*
* Copyright (c) 2009 Lucien Immink
* Licensed under GPL (http://www.opensource.org/licenses/gpl-license.php)
*
* Changelog:
* 16-02-2009 Version 1.0
* - Revamped to "valley edition"
* - Added class listener to get scaling method
* - rewrote browser checking code (* note this is deprecated in jQuery 1.3)
*
* Version history jquery.PNGFix.js by Andreas Eberhard, andreas.eberhard@gmail.com
* 06-02-2009 Version 1.1.1
* - fixed for jQuery 1.3 (Lucien Immink, the people's valley)
* 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
* --------------------------------------------------------------------
*/
jQuery.fn.pngFix = function(settings) {
    // Settings
    settings = jQuery.extend({
        blankgif: 'blank.gif'
    }, settings);
    if (jQuery.browser.msie && jQuery.browser.version < 7) {
        //fix images with png-source
        jQuery(this).find("img[src$=.png]").each(function() {
            jQuery(this).attr('width', jQuery(this).width());
            jQuery(this).attr('height', jQuery(this).height());
            var prevStyle = '';
            var strNewHTML = '';
            var imgId = (jQuery(this).attr('id')) ? 'id="' + jQuery(this).attr('id') + '" ' : '';
            var imgClass = (jQuery(this).attr('class')) ? 'class="' + jQuery(this).attr('class') + '" ' : '';
            var sizingMethod = getIE6Style(imgClass);
            var imgTitle = (jQuery(this).attr('title')) ? 'title="' + jQuery(this).attr('title') + '" ' : '';
            var imgAlt = (jQuery(this).attr('alt')) ? 'alt="' + jQuery(this).attr('alt') + '" ' : '';
            var imgAlign = (jQuery(this).attr('align')) ? 'float:' + jQuery(this).attr('align') + ';' : '';
            var imgHand = (jQuery(this).parent().attr('href')) ? 'cursor:hand;' : '';
            if (this.style.border) {
                prevStyle += 'border:' + this.style.border + ';';
                this.style.border = '';
            }
            if (this.style.padding) {
                prevStyle += 'padding:' + this.style.padding + ';';
                this.style.padding = '';
            }
            if (this.style.margin) {
                prevStyle += 'margin:' + this.style.margin + ';';
                this.style.margin = '';
            }
            var imgStyle = (this.style.cssText);
            strNewHTML += '<span ' + imgId + imgClass + imgTitle + imgAlt;
            strNewHTML += 'style="position:relative;white-space:pre-line;display:inline-block;background:transparent;' + imgAlign + imgHand;
            strNewHTML += 'width:' + jQuery(this).width() + 'px;' + 'height:' + jQuery(this).height() + 'px;';
            strNewHTML += 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + jQuery(this).attr('src') + '\', sizingMethod=\'' + sizingMethod + '\');';
            strNewHTML += imgStyle + '"></span>';
            if (prevStyle != '') {
                strNewHTML = '<span style="position:relative;display:inline-block;' + prevStyle + imgHand + 'width:' + jQuery(this).width() + 'px;' + 'height:' + jQuery(this).height() + 'px;' + '">' + strNewHTML + '</span>';
            }
            jQuery(this).hide();
            jQuery(this).after(strNewHTML);
        });
        // fix css background pngs
        jQuery(this).find("*").each(function() {
            var bgIMG = jQuery(this).css('background-image');
            var imgClass = jQuery(this).attr('class');
            var sizingMethod = getIE6Style(imgClass);
            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='"+sizingMethod+"')";
            }
        });

        //fix input with png-source
        jQuery(this).find("input[src$=.png]").each(function() {
            var bgIMG = jQuery(this).attr('src');
            var imgClass = jQuery(this).attr('class');
            var sizingMethod = getIE6Style(imgClass);
            jQuery(this).get(0).runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + bgIMG + '\', sizingMethod=\''+sizingMethod+'\');';
            jQuery(this).attr('src', settings.blankgif)
        });

    }

    return jQuery(this);
};

function getIE6Style(imgClass) {
    var sizingMethod = '';
    if (imgClass.indexOf("ie6scale") > -1) {
        sizingMethod = "scale";
    } else if (imgClass.indexOf("ie6crop") > -1) {
        sizingMethod = "crop";
    } else {
        sizingMethod = "scale";
    }
    return sizingMethod;
}
