/*

Correctly handle PNG transparency in Win IE 5.5 and 6.
http://homepage.ntlworld.com/bobosola.
Improvements by Frank Denis <j at pureftpd dot org>
This extended version includes imagemap and input image functionality.
It also requires a 1px transparent GIF.

*/

if (typeof(String.prototype.escapeHTML) === 'undefined') {
	String.prototype.escapeHTML = function() {
		return this.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');   
	}
}
if (typeof(String.prototype.escapeHTMLWithQuotes) === 'undefined') {
	String.prototype.escapeHTMLWithQuotes = function() {
		return this.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').
		replace('/\'/g','&#039;').replace('/"/g','&quote;');   
	}
}

function find_img_inputs(oParent)
{
	var oChildren = oParent.children
	if (!oChildren) {
		return;
	}
	for (var i=0; i < oChildren.length; i++) {
		var oChild = oChildren(i);
		if ((oChild.type == 'image') && (oChild.src)) {
			var origSrc = oChild.src;
			oChild.src = strGif;
			oChild.style.filter = strFilter + "(src='" + origSrc.escapeHTMLWithQuotes() + "')";
		}
		find_img_inputs(oChild);
	}
}

function godofix() {
	var strGif = "transparentpixel.gif";
	var strFilter = "progid:DXImageTransform.Microsoft.AlphaImageLoader";
	var arVersion = navigator.appVersion.split("MSIE");
	var version = parseFloat(arVersion[1]);
	var uncomplete = true;
	var todo = Array();
	var j = 0;
	var png_rx = new RegExp("(^|\\s)png(\\s|$)");

	if (version < 5.5 || !(document.body.filters)) {
		return false;
	}
	for (var i = 0; i < document.images.length; i++) {
		var img = document.images[i];
		var classnames = img.className;
		if (classnames.length > 0 && (classnames === "png" || classnames.match(png_rx))) {
			var imgName = img.src.toUpperCase();	
			if (imgName.substring(imgName.length - 4, imgName.length) == ".PNG") {
				todo[j++] = img;
			}
		}
	}	
	if (typeof(document._globals) === 'undefined') {
		document._globals = new Object();
	}
	document._globals.apply_iepngfix = function() {
		var todo = arguments.callee.todo;
		var j = arguments.callee.j;
		var alldone = true;
		while (j-- > 0) {
			img = todo[j];
			if (img === null) {
				continue;
			}
			if (img.complete === false) {
				alldone = false;
				continue;
			}
			var imgID = (img.id) ? "id=\"" + img.id + "\" " : "";
			var imgClass = (img.className) ? "class=\"" + img.className + "\" " : "";
			var imgTitle = (img.title) ? "title=\"" + img.title.escapeHTMLWithQuotes() + "\"" :
			"title=\"" + img.alt.escapeHTMLWithQuotes() + "\"";
			var imgStyle = "display:inline-block;" + img.style.cssText;
			if (img.align == "left") imgStyle = "float:left;" + imgStyle;
			if (img.align == "right") imgStyle = "float:right;" + imgStyle;
			if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;
			if (img.useMap) {
				strAddMap = "<img style=\"position:relative; left:-" + img.offsetWidth + "px;"
				+ "height:" + img.offsetHeight + "px;width:" + img.offsetWidth +"\" "
				+ "src=\"" + strGif.escapeHTMLWithQuotes()
				+ "\" usemap=\"" + img.useMap.escapeHTMLWithQuotes()
				+ "\" border=\"" + img.border + "\">";
			}
			var strNewHTML = "<span " + imgID + imgClass + imgTitle
			+ " style=\"" + "width:" + img.offsetWidth + "px; height:" + img.offsetHeight + "px;"
			+ imgStyle + ";" + "filter:" + strFilter
			+ "(src='" + img.src.escapeHTMLWithQuotes() + "', sizingMethod='crop');\"></span>";
			if (img.useMap) {
				strNewHTML += strAddMap;
			}
			img.outerHTML = strNewHTML;
			todo[j] = null;
		}
		if (alldone === false) {
			window.setTimeout('document._globals.apply_iepngfix();', 100);
		} else {
			todo = null;
			document._globals.apply_iepngfix = null;
			document._globals = null;
			for (i = 0; i < document.forms.length; i++) {
				find_img_inputs(document.forms(i));
			}		
		}
		return alldone;
	}
	document._globals.apply_iepngfix.todo = todo;
	document._globals.apply_iepngfix.j = j;
	document._globals.apply_iepngfix();
}

window.attachEvent('onload', function() { godofix(); });

