/*
Correctly handle PNG transparency in Win IE 5.5 or higher.
http://homepage.ntlworld.com/bobosola. Updated 02-March-2004
*/

function correctPNG(aNoChange){
	for(var i=0; i<document.images.length; i++){
		var img = document.images[i];
		var imgName = img.src;
		
		if (imgName.substring(imgName.length-3, imgName.length).toLowerCase() == "png"){
			var doUpdate = true;
			for(y=0; y<aNoChange.length; ++y){
				var regexp = new RegExp(aNoChange[y], "i");
				if(imgName.match(regexp)) doUpdate = false;
			}
			if(doUpdate){
				var imgID = (img.id) ? "id='" + img.id + "' " : ""
				var imgClass = (img.className) ? "class='" + img.className + "' " : ""
				var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
				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		
				var strNewHTML = "<span " + imgID + imgClass + imgTitle
				+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
				+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
				+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
				img.outerHTML = strNewHTML
				i = i-1
			}
		}
	}
}

function correctBGPNG(aNoChange){
	var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
	var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);
	for (var i = document.all.length - 1, obj = null; (obj = document.all[i]); i--) {
		if (itsAllGood && obj.currentStyle.backgroundImage.match(/\.png/i) != null) {
			if(obj !== document.body){
				var doUpdate = true;
				for(y=0; y<aNoChange.length; ++y){
					var regexp = new RegExp(aNoChange[y], "i");
					var sImgSrc = obj.currentStyle.backgroundImage.substr(5, obj.currentStyle.backgroundImage.length-7);
					if(sImgSrc.match(regexp)) doUpdate = false;
				}
				if(doUpdate){
					this.fnFixPng(obj);
					obj.attachEvent("onpropertychange", this.fnPropertyChanged);
				}
			}
		}
	}
}
	
function fnPropertyChanged() {
	if (window.event.propertyName == "style.backgroundImage") {
		var el = window.event.srcElement;
		if (!el.currentStyle.backgroundImage.match(/pngfix\.gif/i)) {
			var bg	= el.currentStyle.backgroundImage;
			var src = bg.substring(5,bg.length-2);
			el.filters.item(0).src = src;
			el.style.backgroundImage = "url(system/javascript/pngfix.gif)";
		}
	}
}
	
function fnFixPng(obj) {
	var bg	= obj.currentStyle.backgroundImage;
	var src = bg.substring(5,bg.length-2);
	obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')";
	obj.style.backgroundImage = "url(javascript/pngfix.gif)";
}

//hook to catch updating of content in iContent (jorge)
function attachHandles(aNoChange){	
	correctPNG(aNoChange);
	correctBGPNG(aNoChange);
}