// Universal transparent-PNG enabler for MSIE/Win 5.5+
// http://dsandler.org
// From original code: http://www.youngpup.net/?request=/snippets/sleight.xml
// and background-image code: http://www.allinthehead.com/retro/69
// also:
//  * use sizingMethod=crop to avoid scaling PNGs (who would do such a thing?)
//  * only do this once, to make it compatible with CSS rollovers
//
// Amended/correct/enhanced JVG 6/9/2007
// Added pathTOGIF
// Make sure you set a width/height for background divs
//
// Amended JVG 8/28/2007
// Added getPathToGIF function to get src from graphics/ folder
// Make sure spacer.gif is in the graphics/ folder!
//
// Note that when you use AlphaImageLoader, it will always align top left to the parent object
// So when using 32bit PNGs as backgrounds, they will always position left regardless of backgroundPosition
// when this hack is applied for IE 6.
//
// Read more: http://www.satzansatz.de/cssd/tmp/alphatransparency.html
//
// Note that IE<7 has a bug that doesn't expose backgroundPosition to javascript
// Instead read currentStyle.backgroundPositionX and backgroundPositionY and
// set with style.backgroundPositionX and backgroundPositionY
//
// Determine whether this bg is suitable for Alpha filter
// Only PNGs that are top|left aligned w/ no repeat should be filtered
// Otherwise show the transparent GIF that you should always make from the PNG
// Always use lowercase names for internet files :)
//

if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
	window.attachEvent("onload", enableAlphaImages);
}

function getPathToGIF(src) {
	var matches = src.match(/^(.+\/graphics\/)(.+)$/, src);
	if (matches) {
		pathToGIF = matches[1] + 'spacer.gif';
		return (pathToGIF);
	}
	else {
		alert('Cannot locate spacer.gif!');
		return (false);
	}
}

function enableAlphaImages(){
	var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
	var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5 && Number(rslt[1]) < 7 );
	if (itsAllGood) {
		for (var i=0; i<document.all.length; i++){
			var obj = document.all[i];
			var bg = obj.currentStyle.backgroundImage;
			var bgPositionX = obj.currentStyle.backgroundPositionX;
			var bgPositionY = obj.currentStyle.backgroundPositionY;
			var bgRepeat = obj.currentStyle.backgroundRepeat;
			var img = document.images[i];
			if (bg && bg.match(/\.png/i) != null) {
				var src = bg.substring(5,bg.length-2);
				var pathToGIF = getPathToGIF(src);
				if ( ((bgPositionX == '0%') || (bgPositionX == 'left')) && ((bgPositionY == '0%') || (bgPositionY == 'top')) && (bgRepeat == 'no-repeat') ) {
					// Image can be filtered
					obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+src+"', sizingMethod='crop')";
					obj.style.backgroundImage = "url("+pathToGIF+")";
				}
				else {
					// Image cannot be filtered - exchange for GIF
					src = src.replace(/.png$/i,'.gif');
					obj.style.backgroundImage = "url("+src+")";
					obj.style.backgroundPositionX = bgPositionX;
					obj.style.backgroundPositionY = bgPositionY;
					obj.style.backgroundRepeat = bgRepeat;
				}
			}
			else if (img && img.src.match(/\.png$/i) != null) {
				var src = img.src;
				var pathToGIF = getPathToGIF(src);
				img.style.width = img.width + "px";
				img.style.height = img.height + "px";
				img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+src+"', sizingMethod='crop')";
				img.src = pathToGIF;
			}
		}
	}
}
