// sm-iepng.js - IE PNG Alpha Fix - Copyright (c) 2007 ScriptingMagic.com

/**********************************************************

  IE PNG Alpha Transparency Fix
  Version 1.4 - 1/1/2007
  Copyright 2005-2007 ScriptingMagic.com

  Get the latest version for free from
  http://www.scriptingmagic.com/

**********************************************************/

// Create a global ScriptingMagic so we've got our own namespace
if (typeof ScriptingMagic == 'undefined') ScriptingMagic = {};

// Default fixPNG does nothing so it may be called without
// checking browser type or version
ScriptingMagic.fixPNG = function(){};

(function(){
 
    // Only required for IE
    if (!document.all || window.opera) return;
    
    var version = parseFloat(navigator.appVersion.match(/MSIE (\d.*)/)[1]);
    
    // IE 7 finally supports PNG alpha channels! :)
    if ((version < 5.5) || (version >= 7))
    	return;
    	
    // Create the CSS rules for the backdrop DIV
    if (document.styleSheets.length == 0)
    	document.body.appendChild(document.createElement('style'));

    // These rules rely on the IE specific "expression" to keep the
    // backdrop DIV positioned behind the original image and the same
    // size as the original image
    document.styleSheets[document.styleSheets.length - 1].addRule('.sm-iepng-backdrop',	'position:absolute;' +
    						'z-index:expression(this.nextSibling.currentStyle.zIndex);' +
    						'top:expression(this.nextSibling.offsetTop);' +
    						'left:expression(this.nextSibling.offsetLeft);' +
    						'height:expression(this.nextSibling.offsetHeight);' +
    						'width:expression(this.nextSibling.offsetWidth);' +
    						'filter:expression("progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'"+this.nextSibling.src.replace(/\'/g,"%27")+"\')");');
    
    // Make this function available from the outside so
    // that it can called for dynamically created images
    window.ScriptingMagic.fixPNG = function(element)
    {
    
        // Make the original image transparent
        element.style.filter = 'alpha(opacity=0)';

        // Create the backdrop DIV
        var div = document.createElement('div');
        div.className = 'sm-iepng-backdrop';
        
        // If the image's position is "static" the backdrop DIV
        // will cover it so we change the image position to
        // "relative" which should make no difference for layout
        // unless the parentNode is floated. Unfortunately, we
        // can't tell if the parentNode is floated to correct
        // the problem :(
		if (element.currentStyle.position.match(/^static$/i))
			element.style.position = 'relative';

        // Insert the backdrop DIV after the original image
        // The CSS for the backdrop DIV requires the backdrop DIV
        // to immediately follow the original image in the DOM
        element.parentNode.insertBefore(div, element);

        div = null;
    };

    var elements = [], i, c, element, type;
    
    // Add all IMGs to our list of elements to check
    var images = document.getElementsByTagName('img');
    for (i = 0, c = images.length; i < c; i++)
        elements[elements.length] = images[i];

    // Add all INPUTs with a type of "image" to our list
    // of elements to check
    var inputs = document.getElementsByTagName('input');
    for (i = 0, c = inputs.length; i < c; i++)
    {
        element = inputs[i];
        if (element.type && element.type.match(/^image$/i))
            elements[elements.length] = element;
    }

    // Go through all elements we've selected and fix
    // the ones with an sm:iepng attribute set
    for (i = 0, c = elements.length; i < c; i++)
    {
        
        element = elements[i];
        var attribute = element.getAttribute("sm:iepng");
        if (!attribute)
	        continue;
        
        switch (attribute)
        {
        		case 'no': case 'false': case '0':
        			break;
        		default:
	            window.ScriptingMagic.fixPNG(element);
        }
    }

    // Do as much as we can to encourage IE to free memory
    version = elements = i = c = element = type = images = inputs = null;
})();

ScriptingMagic.fixPNG.version = 1.4;
ScriptingMagic.fixPNG.release = new Date('Jan 1, 2007');
