/*
**
** PNG Fix for IE5/6
**
** Fixes transparency in both regular images (<IMG.../>) and background images
** Background position is taken into account, but positioning/layering is not perfect
**
** Source of script is unknown.
**
** Caret CMS www.caret.net
**
*/
var pngfixStyle = "absolute";
pngfix=function() {
    var supported = /MSIE [56]/.test(navigator.userAgent) && navigator.platform == "Win32";
    if (!supported) return;

    var els=document.getElementsByTagName('*');
    var ip=/\.png/i;
    var i=els.length;
    while(i-- >0) {
        var el=els[i];
        var es=el.style;
        if(el.src && el.src.match(ip) && !es.filter) {
            es.height=el.height;
            es.width=el.width;
            es.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+el.src+"',sizingMethod='crop')";
            /* el.src=clear; */
            el.src="/icons/small/blank.gif";
        } else {
            if (el.currentStyle) {
                var elb=el.currentStyle.backgroundImage;
                var elbp=el.currentStyle.backgroundPosition;
                if(elb.match(ip)) {
                    var path=elb.split('"');
                    var bgpx = el.currentStyle.backgroundPositionX;
                    var bgpy = el.currentStyle.backgroundPositionY;
                    var rep=(el.currentStyle.backgroundRepeat=='no-repeat') ? 'crop' :'scale';
                    if (bgpx+bgpy == "") {
                      es.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+path[1]+"',sizingMethod='"+rep+"')";
                    } else {
                      var bgrep=(el.currentStyle.backgroundRepeat=='repeat-y') ? 'scale-y' :'none';
                      pngFixDiv(pngFixDivPosX(el),pngFixDivPosY(el),el.offsetWidth,el.offsetHeight,path[1],bgpx,bgpy,bgrep,el.parentNode);
                    }
                    es.height=el.clientHeight+'px';
                    es.backgroundImage='none';
                    es.offsetRight = 0;
                    var elkids=el.getElementsByTagName('*');
                    if (elkids){
                        var j=elkids.length;
                        if(el.currentStyle.position!="absolute") {
                            es.position='static';
                        }
                        while (j-- >0) { if(!elkids[j].style.position) { elkids[j].style.position="relative"; } }
                    }
                }
            }
        }
    }
}

function pngFixDiv(x,y,w,h,src,px,py,rep,parelm) {
    var div = document.createElement("DIV");
    div.style.position = "absolute";
    div.style.left     = x;
    div.style.top      = y;
    div.style.width    = w;
    div.style.height   = h;
    /* div.style.zIndex   = -1; */
    var img = document.createElement("IMG");
    img.src = src;
    img.style.position = "absolute";
         if (px == "right") { img.style.right     =  0; }
    else if (px == "left" ) { img.style.left      =  0; }
    else                    { img.style.left      = px; }
         if (py == "top"    ) { img.style.top     =  0; }
    else if (py == "bottom" ) { img.style.bottom  =  0; }
    else                      { img.style.top     = py; }
    img.style.width = img.width;
    img.style.height = img.height;
    if (rep == 'scale-x') {
      img.style.width = w;
    } else if (rep == 'scale-y') {
      div.style.zIndex = 1;
      img.style.height = h;
    }
    img.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+img.src+"',sizingMethod='scale')";
    img.src="/icons/small/blank.gif";
    div.appendChild(img);
    if (rep == 'none') {
       if (pngfixStyle == "relative") {
           div.style.left = x-pngFixDivPosX(parelm) +'px';
           div.style.top  = y-pngFixDivPosY(parelm) +'px';
           parelm.appendChild(div);
       } else {
           document.getElementsByTagName("body")[0].appendChild(div);
       }
    } else {
       // parelm.appendChild(div);
       // parelm.style.position = 'relative';
       // div.style.top = 0; div.style.left = 0;
       // parelm.insertBefore(div,parelm.firstChild);
       // document.getElementsByTagName("body")[0].insertBefore(div,parelm);
       document.getElementsByTagName("body")[0].appendChild(div);
    }
    return;
}

/*
** Next 2 functions are from:
** http://blog.firetree.net/2005/07/04/javascript-find-position/
** which are adapted from functions from QuirksMode
*/
function pngFixDivPosX(obj)
{
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
            curleft += obj.offsetLeft;
            if(!obj.offsetParent)
                break;
            obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
}

function pngFixDivPosY(obj)
{
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
            curtop += obj.offsetTop;
            if(!obj.offsetParent)
                break;
            obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
}

     if (window.addEventListener) //DOM method for binding an event
     { window.addEventListener("load", pngfix, false) }
else if (window.attachEvent) //IE exclusive method for binding an event
     { window.attachEvent("onload", pngfix) }
else if (document.getElementById) //support older modern browsers
     { window.onload=pngfix }

