/*
 * ds-sleight: universal transparent-PNG enabler for MSIE/Win 5.5+
 * version 1.0.1 (2007)
 * by Daniel Sandler, http://dsandler.org, dsandler at that domain too
 * see also: http://dsandler.org/wp/archives/2007/08/22/ds-sleight
 * 
 * From original code: http://www.youngpup.net/?request=/snippets/sleight.xml
 * and background-image code: http://www.allinthehead.com/retro/69
 * ...plus the following enhancements:
 *  # use sizingMethod=crop to avoid scaling PNGs (who would do such a thing?)
 *  # only do this once, to make it compatible with CSS rollovers
 * 
 * Instructions for use:
 *  1. load ds-sleight.js in your page
 *  2. create a 1x1 pixel, completely transparent GIF and store it at
 *     /images/spacer.gif (or change the spacerPath variable below to
 *     point at such an image)
 *  3. enjoy
 *
 * 
 * Updates by GymGlish:
 * 2008-11-25 : If background-image is repeated, do 'scale' instead of 'crop'
 * 2008-11-25 : Split big function into smaller and don't miss any images in the loop.
 * 2009-02-02 : On objects that have 'doNotMakeAlphaImage' as class, we don't enabled transparency filter.
 */


var doNotCallDsSleightOnLoad = doNotCallDsSleightOnLoad || false;
// if doNotCallDsSleightOnLoad is true we don't add onload event for IE

if ( (! doNotCallDsSleightOnLoad)
        && navigator.platform == "Win32" 
	&& navigator.appName == "Microsoft Internet Explorer" 
	&& window.attachEvent)
{
	window.attachEvent("onload", enableAlphaImages);
}
var spacerPath = 'http://www.richmorning.com/richmorning/images/transparent-1x1.gif';

function enableAlphaImages(){
    // If browser is IE, loop through all the tags of the document and enable the AlphaImageLoader filter on all PNG background-images and images.

    var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
    var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);
    if (itsAllGood)
        for (var i=0; i<document.all.length; i++)
            makeAlphaImage( document.all[i] );
}


function enableAlphaImageById(id){
    // enable the AlphaImageLoader filter on the tag obj which has for id 'id'
    obj = document.getElementById(id);
    if (obj)
        enableAlphaImage(obj);
}


function enableAlphaImage(obj){
    // If browser is IE, enable the AlphaImageLoader filter on the tag obj. This allows us to specify a specific tag instead of looping through all the tags of the document.

    if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer"){
        var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
        var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);
        if (itsAllGood)
            makeAlphaImage(obj);
    }
}


function makeAlphaImage(obj){
    // Make png image transparent for IE by using the  AlphaImageLoader filter 


    // Get the class names
    classNames = obj.className.split(' ');

    for (i=0; i < classNames.length; i++){
        if (classNames[i] == 'doNotMakeAlphaImage'){
            // Special class name, we don't apply alpha image
            return
        }
    }

    // if obj is a PNG image (img tag)
    if (obj.tagName == 'IMG' && obj.src.match(/\.png$/i) != null) {
        var src = obj.src;
        obj.style.width = obj.width + "px";
        obj.style.height = obj.height + "px";
        obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+src+"', sizingMethod='crop')"
        obj.src = spacerPath;
    }
    else
    {
        var bg = obj.currentStyle.backgroundImage;
        // if obj has background image and it's a png
        if (bg && bg.match(/\.png/i) != null) {
            var img = bg.substring(5,bg.length-2);
            var offset = obj.style["background-position"];
                // If background is repeat we scale image instead of crop 
                var sizing = /^repeat/.test(obj.currentStyle.backgroundRepeat) ? 'scale' : 'crop';
            obj.style.filter =
            "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+img+"', sizingMethod='" + sizing + "')";
            obj.style.backgroundImage = "url('"+spacerPath+"')";
            obj.style["background-position"] = offset; // reapply
        }
    }
}
