﻿/***********************************************************************
 * As you may have guessed by the name, this is a script to work around
 * the many, many, many, many, MANY problems of IE's copmatability with
 * standards.  Did I mention there's many of them?
 ***********************************************************************/

function IeHacks(){
	/* PNG Hack Configuration */
	var PngConfig = {
		FixClass:"transparent-png",
		TransparentGif:"images/transparent.gif",
		MinVersion:5.5,
		MaxVersion:6,
		
		Regex:/\.png$/i
	};
	
	var Browser = {
		HasDetected: false,
		IsIE: false,
		Version: 0
	}
	 
	function DetectBrowser() {
		if(Browser.HasDetected) return;
		Browser.HasDetected = true;
		if(Browser.IsIE = (navigator.appName == 'Microsoft Internet Explorer')){
			var regex = /MSIE ([0-9]{1,}[\.0-9]{0,})/;
			if(regex.exec(navigator.userAgent)!=null){
				Browser.Version = parseFloat(regex.$1);
			}
			
		}
	}

	function PngFix() {
		//Only applies to IE 5.5-6 and filters are required.
		if(!Browser.IsIE) return;
		if(Browser.Version < PngConfig.MinVersion || Browser.Version > PngConfig.MaxVersion) return;
		if(!document.body.filters) return;
		//Do the work
		var img = document.getElementsByTagName("img");
		for(var i = 0; i < img.length; i++){		//loop all images
			if(PngConfig.Regex.test(img[i].src)){	//is a PNG
				var regex = new RegExp("\\b(" + PngConfig.FixClass + ")\\b");
				if(regex.test(img[i].className)){	//uses the class we're fixing
					//explicitly force a width and height
					img[i].style['width'] = img[i].width + "px";
					img[i].style['height'] = img[i].height + "px";
					//apply the DirectX filter *shudder*
					img[i].style['filter'] = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'" + img[i].src + "\', sizingMethod='scale')";
					//and put a transparent gif in place
					img[i].src = PngConfig.TransparentGif;
				}
			}
		}
	}
	
	//Applies the fixes	
	DetectBrowser();
	PngFix();
}
