/* -----------------------------------------------
   PNG IE Hack - v.2
   (c) 2006 www.haan.net
   contact: jeroen@haan.net
   You may use this script but please leave the credits on top intact.
   Please inform us of any improvements made.
   When usefull we will add your credits.
  ------------------------------------------------ */

/* usage:

<script type="text/JavaScript" src="/js/pngFix2.js"></script>
<script type="text/javascript">
window.onload = function()
{
	imgTrans();
	cssTrans();
}
</script>

or alternatively

<html>
<head>
<!--[if gte IE 5.5]>
<![if lt IE 7]>
<script type="text/JavaScript" src="/js/pngFix2.js">
</script>
<![endif]>
<![endif]-->
</head>
<body>
...content goes here...
<!--[if gte IE 5.5]>
<![if lt IE 7]>
<script type="text/javascript">
imgTrans();
cssTrans();
</script>
<![endif]>
<![endif]-->
</body>
</html>


*/

// Note on the sizingMethod property:
// crop, Clips the image to fit the dimensions of the object.
// image, is Default. Enlarges or reduces the border of the object to fit the dimensions of the image.
// scale, Stretches or shrinks the image to fill the borders of the object.
// see comments below where it is used

// Note that you need a transparant GIF as well!

function imgTrans() // correctly handle PNG "img element" transparency in Win IE 5.5 or 6.
{
	var appNum = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
	var IE = (appNum != null && Number(appNum[1]) >= 5.5 && Number(appNum[1]) < 7 );
	if(IE)
	{
		for(var i=0; i<document.images.length; i++)
		{
			var img = document.images[i]
			var imgName = img.src.toLowerCase()
	
			if (imgName.indexOf('.png')>0)
			{
				img.style.width = img.width;
				img.style.height = img.height;
				// here's the the sizingMethod property 1 of 2
				img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true', src='" + img.src + "', sizingMethod='crop');";
				img.src = '/images/1pixel.gif';
			}
		}
	}
}

function cssTrans() // correctly handle PNG "backgroundImage" transparency in Win IE 5.5 or 6.
{
	var appNum = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
	var IE = (appNum != null && Number(appNum[1]) >= 5.5 && Number(appNum[1]) < 7 );
	if(IE)
	{
		var css = document.styleSheets;
		// loop stylesheets
		for(var i=0;i<css.length;i++)
		{
			// Note that this is IE only, in other browsers use cssRules
			var styles = css[i].rules;
			// loop styles
			for(var j=0;j<styles.length;j++)
			{
				if(styles[j].style.backgroundImage&&(styles[j].style.backgroundImage.indexOf('.png')>0))
				{
					var attribute = styles[j].style.backgroundImage;
					image = attribute.substr(4,attribute.length-5);
					// here's the the sizingMethod property 2 of 2
					styles[j].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true', src='" + image + "', sizingMethod='crop')";
					styles[j].style.backgroundImage = 'none';
				}
			}
		}
	}	
}
