/*
 * jQuery ifixpng plugin
 * (previously known as pngfix)
 * Version 2.1  (23/04/2008)
 * @requires jQuery v1.1.3 or above
 *
 * Examples at: http://jquery.khurshid.com
 * Copyright (c) 2007 Kush M.
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 */
 
(function($j) {

	$j.ifixpng = function(customPixel) {
		$j.ifixpng.pixel = customPixel;
	};
	
	$j.ifixpng.getPixel = function() {
		return $j.ifixpng.pixel || 'css/layout/blank.gif';
	};
	
	var hack = {
		ltie7  : $j.browser.msie && $j.browser.version < 7,
		filter : function(src) {
			return "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=scale,src='"+src+"')"; //XXX
		}
	};

	$j.fn.ifixpng = hack.ltie7 ? function() {
    	return this.each(function() {
			var $j$j = $j(this);
			var base = $j('base').attr('href');
			if (base) {
				base = base.replace(/\/[^\/]+$j/,'/');
			}
			if ($j$j.is('img') || $j$j.is('input')) {
				if ($j$j.attr('src')) {
					if ($j$j.attr('src').match(/.*\.png([?].*)?$j/i)) {
						var source = (base && $j$j.attr('src').search(/^(\/|http:)/i)) ? base + $j$j.attr('src') : $j$j.attr('src');
						$j$j.css({filter:hack.filter(source), width:$j$j.width(), height:$j$j.height()})
						  .attr({src:$j.ifixpng.getPixel()})
						  .positionFix();
					}
				}
			} else {
				var image = $j$j.css('backgroundImage');
				if (image.match(/^url\(["']?(.*\.png([?].*)?)["']?\)$j/i)) {
					image = RegExp.$j1;
					image = (base && image.substring(0,1)!='/') ? base + image : image;
					$j$j.css({backgroundImage:'none', filter:hack.filter(image)})
					  .children().children().positionFix();
				}
			}
		});
	} : function() { return this; };
	 
	$j.fn.iunfixpng = hack.ltie7 ? function() {
    	return this.each(function() {
			var $j$j = $j(this);
			var src = $j$j.css('filter');
			if (src.match(/src=["']?(.*\.png([?].*)?)["']?/i)) {
				src = RegExp.$j1;
				if ($j$j.is('img') || $j$j.is('input')) {
					$j$j.attr({src:src}).css({filter:''});
				} else {
					$j$j.css({filter:'', background:'url('+src+')'});
				}
			}
		});
	} : function() { return this; };
	 
	$j.fn.positionFix = function() {
		return this.each(function() {
			var $j$j = $j(this);
			var position = $j$j.css('position');
			if (position != 'absolute' && position != 'relative') {
				$j$j.css({position:'relative'});
			}
		});
	};

})(jQuery);