
/*
 * 
 * fixPng Plugin 1.0
 * Version 1.0
 * @requires jQuery v1.3.0
 * 
 * Copyright (c) 2010 Gareth Preston
 * Licensed under the GPL licenses:
 * http://www.gnu.org/licenses/gpl.html
 * 
 */

(function($) {
	$.fn.pngFix = function (options) {
		
		var defaults = {
			scalePngFix: 'scalePngFix',
			cropPngFix: 'cropPngFix',
			imagePngFix: 'imagePngFix',
			pngFix: 'pngFix',
			mode: 'scale' /* scale, image or crop */
		};
		
		var opts = $.extend({}, defaults, options);
		
		/* If the current browser is not Internet Explorer 6, then just don't run javascript */
		if(!$.browser.msie || ($.browser.msie && $.browser.version >= 7)) {
			return this;
		}
		
		return this.each(function(cntrl){
			
			/* Find out if the HTML element has the css class that we need */
			if($(this).hasClass(opts.pngFix) || $(this).hasClass(opts.scalePngFix) || $(this).hasClass(opts.cropPngFix) || $(this).hasClass(opts.imagePngFix)) {
				
				var mode = opts.mode;
				if($(this).hasClass(opts.scalePngFix))
					mode = 'scale';
				else if($(this).hasClass(opts.cropPngFix))
					mode = 'crop';
				else if($(this).hasClass(opts.imagePngFix))
					mode = 'image';
				
				/* CSS Background Image */
				var bg = $(this).css('backgroundImage');
				var matches = bg.match(/^url\("(.*)"\)$/);
				if(matches && matches.length) {
					
					$(this).css({
						backgroundImage: 'none',
						filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + matches[1] + '\',sizingMethod=\'' + mode +'\')'
					});
					
				}
				
				/* Image */
				else if($(this).get(0).tagName.toUpperCase() == 'IMG') {
					
					var src = $(this).attr('src');
					var width = $(this).attr('width');
					var height = $(this).attr('height');
					
					if (src.lastIndexOf('.png') != -1)
					{
						
						$(this).wrap('<span></span>');
						$(this).parent().css({
							display: 'block',
							overflow: 'hidden',
							width: width,
							height: height,
							filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + src + '\',sizingMethod=\'' + mode +'\')'
						});
						$(this).remove();
						
					}
					
				}
				
			}
			
		});
		
	}
})(jQuery);

