﻿/**
 * --------------------------------------------------------------------
 * jQuery-Plugin "pngFix"
 * Version: 1.1, 11.09.2007
 * 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:
 *    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($) {
	function _PNG_fnLoadImage() {
		this.Source.attr('width', this.width);
		this.Source.attr('height', this.height);

		var prevStyle   = '';
		var strNewHTML  = '';
		var imgId       = (this.Source.attr('id'))            ? 'id="'    + this.Source.attr('id') + '" ' : '';
		var imgClass    = (this.Source.attr('class'))         ? 'class="' + this.Source.attr('class') + '" ' : '';
		var imgTitle    = (this.Source.attr('title'))         ? 'title="' + this.Source.attr('title') + '" ' : '';
		var imgAlt      = (this.Source.attr('alt'))           ? 'alt="'   + this.Source.attr('alt') + '" ' : '';
		var imgAlign    = (this.Source.attr('align'))         ? 'float:'  + this.Source.attr('align') + ';' : '';
		var imgHand     = (this.Source.parent().attr('href')) ? 'cursor:hand;' : '';
		if (this.Source.get(0).style.border) {
			prevStyle += 'border:'+this.Source.get(0).style.border+';';
			this.Source.get(0).style.border = '';
		}
		if (this.Source.get(0).style.padding) {
			prevStyle += 'padding:'+this.Source.get(0).style.padding+';';
			this.Source.get(0).style.padding = '';
		}
		if (this.Source.get(0).style.margin) {
			prevStyle += 'margin:'+this.Source.get(0).style.margin+';';
			this.Source.get(0).style.margin = '';
		}
		var imgStyle = (this.Source.get(0).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:' + this.width + 'px;' + 'height:' + this.height + 'px;';
		strNewHTML += 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + this.Source.attr('src') + '\', sizingMethod=\'scale\');';
		strNewHTML += imgStyle+'"></span>';

		if (prevStyle != ''){
			strNewHTML = '<span style="position:relative;display:inline-block;'+prevStyle+imgHand+'width:' + this.width + 'px;' + 'height:' + this.height + 'px;'+'">' + strNewHTML + '</span>';
		}

		this.Source.hide();
		this.Source.after(strNewHTML);
	}

  jQuery.fn.pngFix = function(settings) {
  	// Settings
  	settings = jQuery.extend({
  		blankgif: '/_images/blank.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

      var test = [];
  		jQuery(this).find("img[src$=.png]").each(function() {
  		  var $this = $(this);
  		  test.push( new Image() );
  		  test[test.length-1].src    = $(this).attr("src");
  		  test[test.length-1].Source = $this;
  		  test[test.length-1].onload = _PNG_fnLoadImage;
  		});

  		// fix css background pngs
  		jQuery(this).find("*").each(function(){
  			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='scale')";
  			}
  		});

  		//fix input with png-source
  		jQuery(this).find("input[src$=.png]").each(function() {
  			var bgIMG = jQuery(this).attr('src');
  			jQuery(this).get(0).runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + bgIMG + '\', sizingMethod=\'scale\');';
     		jQuery(this).attr('src', settings.blankgif)
  		});


  		// fix css background pngs
  		jQuery(this).each(function(){
  			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='scale')";
  			}
  		});

  		//fix input with png-source
  		jQuery(this).each(function() {
  			var bgIMG = jQuery(this).attr('src');
  			jQuery(this).get(0).runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + bgIMG + '\', sizingMethod=\'scale\');';
     		jQuery(this).attr('src', settings.blankgif)
  		});
  	}

  	return jQuery;

  };
})(jQuery);

