var pngfix	= function() {
	
	var root = false;
	
	// Path to a transparent GIF image
	var shim = 'x.gif';
	
	// RegExp to match above GIF image name
	var shim_pattern = /x\.gif$/i;
	
	var execute = function() 
	{ 
		if (root) 
		{
			root = document.getElementById(root);
			
		} else
		{
			root = document;
		}
		for (var i = root.all.length - 1, obj = null; (obj = root.all[i]); i--) 
		{
			// background pngs with png class
			if (obj.className.match(/\png/i) !== null && obj.currentStyle.backgroundImage != "") 
			{
				fixBackground(obj);
			}						
			// image elements with png class
			if (obj.className.match(/\png/i) !== null && obj.tagName=='IMG')
			{
				fixImage(obj);
			}
		}
	};

	var fixBackground = function(obj) 
	{
		var mode = 'scale';
		var bg	= obj.currentStyle.backgroundImage;
		bg = bg.replace(/%26/, '__AMP__'); //fix ampersand
		var src = encodeURI(bg.substring(5, bg.length-2));
		if (obj.currentStyle.backgroundRepeat == 'no-repeat') 
		{
			mode = 'crop';
		}
		obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + mode + "')";
		obj.style.backgroundImage = 'url('+shim+')';
	};

	var fixImage = function(img) 
	{
		var src = img.src;
		img.style.width = img.width + "px";
		img.style.height = img.height + "px";
		img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')";
		img.src = shim;
	};
	
	var addLoadEvent = function(func) 
	{
		var oldonload = window.onload;
		if (typeof window.onload != 'function') 
		{
			window.onload = func;
		} else 
		{
			window.onload = function() 
			{
				if (oldonload) 
				{
					oldonload();
				}
				func();
			};
		}
	};
	
	return {
		init: function() { 
			addLoadEvent(execute);
		},
		
		limitTo: function(el) {
			root = el;
		},
		
		run: function() {
			execute();
		}
	};
}();

if (document.all)
{
	pngfix.init();
}