/**
 * $id: fixpngalpha.php,v 1.1 2004/08/08 18:49:35 jariberg Exp $
 *
 * Copyright (c) 2004 Jari Berg Jensen. All rights reserved.
 * Written by08 Aug 2004 by Jari Berg Jensen.
 *
 * ------------------------------------------------------------------
 * Licensed under the terms of the GNU Lesser General Public License:
 *     http://www.opensource.org/licenses/lgpl-license.php
 * ------------------------------------------------------------------
 *
 * Version: 1.1
 * Modified: 08 Aug 2004
 *
 * For further information visit:
 *     http://www.razormotion.com/software/fixpngalpha/
 *
 * VERSION HISTORY
 * ---------------
 *   v1.0
 *      - Initial version
 *   v1.1
 *      - Fixed some code (not bugs) and general code cleanup
 *      - Optimized code in fixPngInputs
 *
 * ABOUT
 * -----
 *
 *   Internet Explorer 5+ have limited support for PNG 32bit files with Alpha Channels.
 *   This code presents a fix for the problem by using 'alpha filter' and 'transparent gif' tricks.
 *
 *   fixPngAlpha is pure javascript code that will change images and all form elements of type='image'.
 *   The fix is done in such a way that the original objects stays intact. (ie. <img STAYS an <img)
 *   The only change is that elements are modified by adding 'alpha filter' and 'transparent gif'
 *   For more information - see the source below!
 *
 * FEATURES
 * --------
 *
 *   - All elements that are modified stays the same. Ie. same object type.
 *   - No change in your code necessary - only <script src='fixpngalpha.js'></script> is required!
 *   - You can use onMouseOver to change images to other PNG-Alpha files.
 *   - Imagemaps are supported without any special code because they are ordinary images with usemap=''
 *   - fixPngAlpha attach an eventhandler to processed objects that gets called if 'src' is changed!
 *   - All handling of images and other supported elements are performed automatically.
 *   - Very fast and add no noticeable overhead to Internet Explorer. Discreet flickering might occur!
 *   - fixPngAlpha is only active when running in Internet Explorer 5+ on Windows.
 *
 * EXAMPLE SOURCECODE
 * ------------------
 *
 *   NOTE! See 'demo.htm' for a working example!
 *
 * <html>
 * 		<head>
 * 			<script src="fixpngalpha.js" language="javascript"></script>
 * 		</head>
 * 		<body>
 *
 * 			** fixPngAlpha Supports images and input elements of type 'image' <br /><br />
 *
 * 			<img src="AlphaChannel.PNG" border="0" style="border:4px solid #ff0000;" />
 *
 * 			<input type="image" src="AlphaButton.PNG" />
 *
 * 		</body>
 * </html>
 *
 */

/**
 * IMPORTANT!
 * Remember to change 'fixPngTransparent1x1' to path for any 1x1 transparent GIF image!
 */
var fixPngTransparent1x1 = "transparent1x1.gif";

/**
 * Fix PNG image
 */
function fixPngImage(e) {
	var target = 0;
	if (e.src && e.width) {target = e;} // image instance
	else {
		if (!e) var e = window.event;
		if (e.target) target = e.target;
		else if (e.srcElement) target = e.srcElement;
		if (target.nodeType == 3) {target = target.parentNode;} // defeat Safari bug
	}
	if (target) {
		// fix image resizing problem when src is changed
		var img = new Image();
		img.src = target.src;
		if (img.width > 1) {
			//target.style.cssText += ";"+"filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale src='"+src+"');";
			target.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale src='"+img.src+"');";
			target.src = fixPngTransparent1x1;
			target.width = img.width;
			target.height = img.height;
		}
	}
}
/**
 * Loop through all PNG images and fix them accordingly
 */
function fixPngImages() {
	for (var i=0; i<document.images.length; i++) {
		var img = document.images[i];
		var filename = img.src.toLowerCase();
		if (filename.substring(filename.length-3, filename.length) == "png") {
			fixPngImage(img);
			img.attachEvent("onload", fixPngImage);
		}
	}
}
/**
 * Loop through all form elements of type 'image' (type='image') and fix them accordingly
 */
function fixPngInputs(p, f) {
	if (!p && document.forms[f]) {
		p = document.forms[f];
	}
	var children = p.children;
	if (children) {
		for (var i=0; i<children.length; i++) {
			var child = children[i];
			if ((child.type == 'image') && (child.src)) {
				fixPngImage(child);
			}
		}
	}
	if (f < document.forms.length) {
		fixPngInputs(0, f + 1);
	}
}
/**
 * Check if browser is Internet Explorer 5+ running on Windows platform
 */
function checkFixPngAlphaSupport() {
	var ua = navigator.userAgent.toLowerCase();
    var msie = ua.indexOf('msie') + 1;
	var win = ua.indexOf('win') + 1;
	var ver = 0;
	if (msie && win) {
		ver = ua.charAt(msie + 4);
	}
	return ver > 4 ? 1 : 0;
}
/**
 * Fix PNG Alpha entry function
 *
 * IMPORTANT!
 * Do not call this! Function will be called by the onLoad eventhandler
 */
function fixPngAlpha() {
	fixPngImages();
	fixPngInputs(0, 0);
}
/**
 * Attach Fix PNG code to onLoad eventhandler of the (<body onload='fixPngAlpha()'>)
 */
if (checkFixPngAlphaSupport()) {
	window.attachEvent("onload", fixPngAlpha);
}
