/*
 * iepng.js
 *
 * Automatically alter all PNG background styles so that
 * partial transparency works in MS IE 6 and earlier.
 *
 * This script should be enclosed in an IE conditional comment
 * that is parsed only by IE 6 and earlier, e.g.:
 *   <!--[if lte IE 6]>
 *     <script type="text/javascript" src="iepng.js"></script>
 *   <![endif]-->
 *
 * (c) Copyright 2006 Future Medium Pty Ltd - http://www.futuremedium.com.au/
 *
 * @author scampbell
 * @author jmundy
 */


// preserve existing onload code
var wOnload = window.onload;
window.onload = function() {
	if (wOnload != null) {
		wOnload();
	}
	init_ie_png();
}


// regex to extract image path from url(...) directive
var BG_IMG_PATH = /url\(([^\)]*)\)/;

// debugging counters
var examinedElements = 0;
var changedImgs = 0;
var changedBgs = 0;

/**
 * Iterates through all document elements, searching for PNG
 * images, and replacing those backgrounds with the MS
 * proprietary alpha-channel filter.
 */
function init_ie_png() {

	var elems = document.body.getElementsByTagName("*");

	// IE 5.5 and earlier don't recognise the wildcard argument, so fallback to document.all
	if (elems.length == 0) {
		elems = new Array();
		for (elId in document.all) {
			elems.push(document.all[elId]);
		}
	}

	for (var ii = 0; ii < elems.length; ii++) {
		var elem = elems[ii];

		examinedElements++;

		if (elem.nodeName == "IMG") {
			__handlePNGImage(elem);
		}
		if (elem.currentStyle != null && elem.currentStyle.backgroundImage != "") {
			__handlePNGBackground(elem);
		}
	}

	//window.status = "init_ie_png: Applied IE alpha filter to " + changedImgs + " image(s) and " + changedBgs + " background(s). (" + examinedElements + " elements examined)";
}


/**
 * Parses an image element to see if it has a .PNG extension, and
 * if it does, enables partial transparency.
 *
 * The image's source is set to blank, and the MS alpha-filter
 * is then applied to the image.
 *
 * @param img The image element to inspect.
 */
function __handlePNGImage(img) {
	// check for .png extension
	var imgSrc = img.src;
	if (imgSrc.toLowerCase().indexOf(".png") == -1) {
		return false;
	}
  
  
  
	// "hide" the image
	//img.style.width = "100px";
	//img.style.height = "100px";
  
  // JM hack, assumes there is spacer.gif in the images directory
  
  img.src = img.src.replace(/images\/(.*?)\.png/g, "images/spacer.gif");
  
  /*
		@fixme: The above leaves a 1x1 px artifact. Ideally want to set the src to 'null', but that causes the
    	broken image icon to appear. An alternative solution is to use a spacer.gif, but the issue is finding
    	the image relative to the current path (script could be used at any level of the hierarchy). Another
    	alternative is to replace the image entirely with a <div>, but then stylesheet rules don't get applied
    	to the element anymore.
    */

	img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\"" + imgSrc + "\", sizingMethod=\"image\")";

	changedImgs++;
}


/**
 * Parses an element with a background image to see if the background
 * has a .PNG extension, and if it does, enables partial transparency
 * on that background.
 *
 * @param elem The element to inspect.
 */
function __handlePNGBackground(elem) {
	// check for .png extension
  
  return;
  
	var imgStyle = elem.currentStyle.backgroundImage;
	if (imgStyle.toLowerCase().indexOf(".png") == -1) {
		return false;
	}
	var bgImg = imgStyle.replace(BG_IMG_PATH, "$1");

	// get unquoted image path
	if (bgImg.indexOf("\"") != -1) {
		bgImg = bgImg.substr(1, bgImg.length-2);
	}

	// get sizing method -- image can either be scaled or not (background-repeat can't be emulated)
	var repeatStyle = elem.currentStyle.backgroundRepeat;
	var bgSizing = (repeatStyle == "no-repeat") ? "crop" : "scale";

  
	// insert MS filter code
	elem.style.background = "none";
	elem.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\"" + bgImg + "\", sizingMethod=\"" + bgSizing + "\");";

	changedBgs++;
}