/*
 
Correctly handle PNG transparency in Win IE 5.5 & 6.
author:
	Shaun Freeman.
	http://www.shaunfreeman.co.uk
	
based on:
	http://homepage.ntlworld.com/bobosola.
	http://clientside.cnet.com/wiki/cnet-libraries/01-browser-fixes

Use in:
	<head>
	
Options:
	el: element with the png to fix, defaults to document.body.
	cssImages: HTMLCollections, arrays of elements, elements, or strings as selectors with png's to fix, defauls to null.
	initializeOnLoad: set to fix png's on page load, defaults to true.
	
Example 1:

	<script type="text/javascript" src="mootools.js"></script>
	<!--[if IE]>
		<script type="text/javascript" src="pngfix.js"></script>
		<script>
			var supBrowser = (Browser.Engine.trident && !Browser.Engine.trident5 && document.body.filters) ? true : false;
			if (supBrowser) {
				window.addEvent('domready', function(){
					new pngFix({cssBgElements: ['#top']});
				});
			}
		</script>
	<![endif]-->

Example 2:

	<script type="text/javascript" src="mootools.js"></script>
	
	<script>
		var supBrowser = (Browser.Engine.trident && !Browser.Engine.trident5 && document.body.filters) ? true : false;
		if (supBrowser) {
			window.addEvent('domready', function(){
				new Asset.javascript('pngfix.js');
			}).chain(function(){
				new pngFix({cssBgElements: ['#top']});
			});
		}
	</script>

var pngFix = new Class({
	Implements: [Options],
	options: {
		el: document.body,
		cssBgElements: null,
		initializeOnLoad: true
	},
	initialize: function(options) {
		if (options) this.setOptions(options);
		if (this.options.initializeOnLoad) this.fixPNGElements(this.options.el);
		if (this.options.cssBgElements) this.bgImgFix(this.options.cssBgElements);
	},
	fixPNGElements: function(element) {
		var allImages = $(element).getElements('img');
		allImages.each(function(img) {
			this.fixPNGImg(img);
		}.bind(this));
	},
	fixPNGImg: function(img) {
		var imgProps = img.getProperties('id', 'src', 'title', 'alt', 'align');
		if (imgProps.src.test('.png', 'i')) {
			var imgStyles = img.getStyles();
			var imgDisplay = 'inline-block';
			if (imgProps.align == 'left') var imgFloat = 'left';
			if (imgProps.align == 'right') var imgFloat = 'right';	
			if (img.getParent().getProperty('href')) var imgCursor = 'hand';
			var replacement = new Element('span', {
				'id': (imgProps.id) ? imgProps.id : '',
				'class': (img.className) ? img.className : '',
				'title': (imgProps.title) ? imgProps.title : (imgProps.alt) ? imgProps.alt : '',
				'styles': {
					'display': imgDisplay,
					'width': img.getWidth() + 'px',
					'height': img.getHeight() + 'px',
					'cursor': imgCursor,
					'float': (imgFloat) ? imgFloat : '',
					'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader (src='" + imgProps.src + "', sizingMethod='scale');"	
				},
				'src': imgProps.src
			}).setStyles(imgStyles).cloneEvents(img).replaces(img);
		}
	},
	bgImgFix: function(bgElements) {
		var cssImages = $$(bgElements);
		cssImages.each(function(img) {
			var imgURL = img.getStyle('background-image');
			if (imgURL.test(/\((.+)\)/)){
				img.setStyles({
					'background-image': 'none',
					'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod='crop', src=" + imgURL.match(/\((.+)\)/)[1] + ")"
				});
			};
		});
	}
});
var noPNGSupport = (document.body.filters) ? true : false;

if (noPNGSupport) new pngFix({cssBgElements: ['#top']});
*/

/*
Script: fixpng.js

Dependancies:
	 mootools - <Moo.js>, <String.js>, <Array.js>, <Function.js>, <Element.js>, <Dom.js>
	
Author:
	Aaron Newton, <aaron [dot] newton [at] cnet [dot] com>

		Function: fixPNG
		this will make transparent pngs show up correctly in IE. This function 
		is based almost entirely on the function found here: 
		<http://homepage.ntlworld.com/bobosola/pnginfo.htm>
		
		Arguments:
		el - the image element (or id) or dom element with a background image (or id) to fix
		
		Note: 
		there is an instances of this already set to fire onDOMReady that
		will fix any png files with the class "fixPNG". This means any producer
		can just give the class "fixPNG" to any img tag and they are set BUT, the
		ping will look wrong until the DOM loads, which may or may not be noticeable.
		
		The alternative is to embed the call right after the image like so:
		
		><img src="png1.png" width="50" height="50" id="png1">
		><img src="png2.png" width="50" height="50" id="png2">
		><script>
		>	$$('#png1', '#png2').each(function(png) {fixPNG(png);});
		>	//OR
		>	fixPNG('png1');
		>	fixPNG('png2');
		></script>
*/

function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6.
{
   var arVersion = navigator.appVersion.split("MSIE")
   var version = parseFloat(arVersion[1])
   if ((version >= 5.5) && (document.body.filters)) 
   {
      for(var i=0; i<document.images.length; i++)
      {
         var img = document.images[i]
         var imgName = img.src.toUpperCase()
         if (imgName.substring(imgName.length-3, 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
         }
      }
   }    
}
//window.attachEvent("onload", correctPNG);
