//fix all pngs when dom is ready
onDomReady(fixAllPngs);

//Loop through all images and apply filter
function fixAllPngs()
{
	for (var i = 0; i < document.images.length; i++)
   {	
	   fixPng2(document.images[i]);
   }
}

//Apply alpha filter to an image
function fixPng(image)
{
	//fixPng_method1(image);	
}

//Apply alpha filter to an image
function fixPng2(image)
{
	fixPng_method2(image);	
}

//Preload blank image
if (typeof blankImg == 'undefined')
{
	var blankImg = '../../Resources/JS/blank.gif';
}

function applyFilter(image, s, m)
{
	var filters = document.body.filters;
	var f = 'DXImageTransform.Microsoft.AlphaImageLoader';

	if (filters[f])
	{
		filters[f].enabled = s ? true : false;
		if (s)
		{
			filters[f].src = s
			filters[f].sizingMethod = m
		}
	}
	else if (s) image.style.filter = 'progid:'+f+'(src="'+s+'",sizingMethod="'+m+'")';
}

function fixPng_method2(image)
{
	// Assume IE7 is OK.
	if (!/MSIE (5\.5|6\.)/.test(navigator.userAgent) || (event && !/(image.background||image.src)/.test(event.propertyName))) return;
	var filters = document.body.filters;

	var bgImg = image.currentStyle.backgroundImage || image.style.backgroundImage;
	
	if (image.tagName == 'IMG')
	{
		if ((/\.png$/i).test(image.src))
		{
			if (image.currentStyle.width == 'auto' && image.currentStyle.height == 'auto')
			{
				image.style.width = image.offsetWidth + 'px';
			}
			applyFilter(image, image.src, 'scale');
			image.src = blankImg;
		}
		else 
		{
			if (image.src.indexOf(blankImg) < 0) 
			{
				//filt(image);
			}
			else
			{
				if (bgImg && bgImg != 'none')
				{
					if (bgImg.match(/^url[("']+(.*\.png)[)"']+$/i))
					{
						var s = RegExp.$1;
						if (image.currentStyle.width == 'auto' && image.currentStyle.height == 'auto')
						image.style.width = image.offsetWidth + 'px';
						image.style.backgroundImage = 'none';
						applyFilter(image, s, 'crop');
						// IE link fix.
						for (var n = 0; n < image.childNodes.length; n++)
						if (image.childNodes[n].style) image.childNodes[n].style.position = 'relative';
					}
					else
					{
						applyFilter(image);
					}
				}
			}
		}
	}
}

function fixPng_method1(image)
{
	var arVersion = navigator.appVersion.split("MSIE")
	var version = parseFloat(arVersion[1])
	
	if ((version >= 5.5) && (document.body.filters)) 
	{
	  var img = image;
	  var imgName = img.src.toUpperCase()
	  if (imgName.substring(imgName.length-4, imgName.length) == ".PNG")
	  {
		 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
	  }
   }
}

