/* copyright (c) Robert E Walker 2006 */

/* Replaces .png files with AlphaImageLoader filter in IE. */
/* Replaces .png background images with .gif equivalent. */

/********************************/
// Initialization

// pathToRoot specifies the relative-to-root path
// for the file calling this script
if ('undefined' == typeof(pathToRoot))
{
 var pathToRoot = '';
}// if 

/********************************/
function swapAllPng()
{
 swapBackgroundPng();
 swapImgPng();
}// function swapAllPng

/********************************/
function swapImgPng()
{
 try
 {
  if (isIEWin())
  {
   // Search all image (<img>) elements and replace each
   // .png with style filter.
   var images = document.images;
   var image;
   var imageSrc;
   for (var index=0; index < images.length; index++)
   {
	image = images[index];
	imageSrc = image.src;
	if (null != imageSrc.match(/\.png$/i))
	{
	 // Replace this image.
	 // A transparent gif is put in as a place holder.
	 image.src = pathToRoot + 'images/transparent.gif';
	 // Set the size to stretch the gif.
	 image.style.width = image.width  + 'px';
	 image.style.height = image.height + 'px';
	 // Then the filter is used to put in the .png.
	 image.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + imageSrc + "', sizingMethod='scale')";
	}// if
   }// for
  }// if
 }
 catch (error)
 {
  alert ('swapImgPng: ' + error.name +': '+ error.message);
 }// try
}// function swapImgPng


/********************************/
function swapBackgroundPng()
{
 try
 {
  if (isIEWin())
  {
   // Search all stylesheeets and replace each background-image
   // .png with a .gif with the same name.
   // WARNING: js cannot determine the existance of files,
   // this script blindly replaces the reference to .png files
   // with a reference to .gif files of the same name regardless
   // of whether the .gif file exists.
   var styleSheets = document.styleSheets;
   var rules;
   var rule;
   var imageSrc;
   for (var index1=0; index1 < styleSheets.length; index1++)
   {
    // Note: stardard compliant browsers support cssRules object,
    // while IE Win uses rules object. As this script is set to run
    // only on IE Win this check is not entirely necessary. But it
    // will eliminate one gotcha if it is extended in the future.
	if (!(rules = styleSheets[index1].cssRules))
	{
     rules = styleSheets[index1].rules
	}// if
	for (var index2=0; index2 < rules.length; index2++)
	{
	 rule = rules[index2];
	 if (imageSrc = rule.style.backgroundImage)
	 {
	  // If the rule has a background .png, this will replace it with a .gif.
	  rule.style.backgroundImage = imageSrc.replace(/.png/, '.gif');
	 }// if
	}// for
   }// for
  }// if
 }
 catch (error)
 {
  alert ('swapBackgroundPng: ' + error.name +': '+ error.message);
 }// try
}// function swapBackgroundPng

/********************************/
function isIEWin ()
{
 var result = false;
 if (navigator)
 {
  var appName = navigator.appName;
  var appVersion = 0;
  // Check that it IS IE and NOT Opera (Opera can id itself as IE).
  if ((-1 != navigator.appVersion.indexOf("MSIE")) && (-1 == navigator.userAgent.indexOf("Opera")))
  {
   var temp = navigator.appVersion.split("MSIE");
   appVersion = parseFloat(temp[1]);
  }// if
  if (('Microsoft Internet Explorer' == appName) && (5.5 <= appVersion) && (7.0 > appVersion))
  {
   result = true;
  }// if
 }// if
 return result;
}// fucntion isIEWin