
// generic namespacing for WLD functions, in case it's not already been set up
var WLD = WLD || {};

/**
 * IE 5.5-6 are dumb dumb dumb and don't like alpha transparency PNGs.
 * This is a couple of functions which make everything good in the world; 
 */

WLD.PngFix = {
	ie_version: parseFloat(navigator.appVersion.split("MSIE")[1]),
	filter_applied: false,
	apply_filter: function() {
		// don't bother if we're not using IE
		if (WLD.PngFix.filter_applied || !(WLD.PngFix.ie_version >= 5.5 && WLD.PngFix.ie_version < 7)) { return; }
		// find all elements that potentially need fixing
		$$('.apply-png-fix').each( function(elem) { WLD.PngFix.run_filter(elem); });
		// say that we've done some filtering
		WLD.PngFix.filter_applied = true;
	},
	run_filter: function(elem) {
		// build up the new attributes
		var img = {};
		img.id = (elem.id) ? "id='" + elem.id + "' " : "";
		img.title = (elem.title) ? "title='" + elem.title + "' " : "";
		img.style = "display:inline-block;" + elem.style.cssText;
		// strip out any class info which relates to the png fix
		img.klass = (elem.className) ? elem.className : "";
		img.klass = img.klass.split(/apply-png-fix/).join(" ");
		if (img.klass.length) { img.klass = "class='" + img.klass + "' "; }
		// also set floated info
		if (elem.align == "left")  { img.style = "float:left;" + img.style;  }
		if (elem.align == "right") { img.style = "float:right;" + img.style; }
		if (elem.parentElement && elem.parentElement.href) { img.style = "cursor:hand;" + img.style; }
		// build up the new tag
		var new_img_html = "<span " + img.id + img.klass + img.title
		//+ " style=\"" + "width:" + elem.width + "px; height:" + elem.height + "px;" + img.style + ";"
		+ " style=\"" + "width:64px; height:64px;" + img.style + ";"
		+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
		+ "(src=\'" + elem.src + "\', sizingMethod='scale');\"></span>";
		// replace the img tag with the new span
		elem.outerHTML = new_img_html;
	}
}

// run the filter on page load
Event.observe(window, 'load', function() { WLD.PngFix.apply_filter(); });
