/*jslint white: true, browser: true, devel: true, onevar: true, undef: true,
         nomen: true, eqeqeq: true, plusplus: true, bitwise: true, newcap: true,
         regexp: false, immed: true, indent: 2 */

/*global YAHOO, window */

var f, filt, blankImg, doIEPNGFix, doIEPNGFixImgFrom, els_to_fix, i, iLen;
if (YAHOO.env.ua.ie <= 6) {
  els_to_fix = ['logo', 'search', 'sidebar_content', 'content_bd'];
  f = window.f;
  filt = window.filt;
  blankImg = window.blankImg;
  doIEPNGFix = window.doIEPNGFix;
  doIEPNGFixImgFrom = window.doIEPNGFixImgFrom;

  if (!f) {
    f = 'DXImageTransform.Microsoft.AlphaImageLoader';
  }

  if (!filt) {
    filt = function (e, s, m) {
      if (e.filters[f])
      {
        e.filters[f].enabled = s ? true : false;
        if (s) {
          e.src = s;
          e.sizingMethod = m;
        }
      }
      else if (s) {
        e.style.filter = [
          'progid:', f, '(src="', s, '",sizingMethod="', m, '")'
        ].join("");
      }
    };
  }

  if (!blankImg) {
    blankImg = 'spacer.gif';
  }

  if (!doIEPNGFix) {
    doIEPNGFix = function (e) {
      var bgImg, s, n;
      if (e) {
        // Assume IE7 is OK.
        if (!/MSIE (5\.5|6\.)/.test(navigator.userAgent)) {
          return;
        }
        bgImg = e.currentStyle && e.currentStyle.backgroundImage ||
                e.style && e.style.backgroundImage ||
                null;

        if (e.tagName === 'IMG') {
          if ((/\.png$/i).test(e.src)) {
            if (e.currentStyle.width === 'auto' &&
                e.currentStyle.height === 'auto') {
              e.style.width = e.offsetWidth + 'px';
            }
            filt(e, e.src, 'scale');
            e.src = blankImg;
          }
          else if (e.src.indexOf(blankImg) < 0) {
            filt(e);
          }
        }
        else if (bgImg && bgImg !== 'none') {
          if (bgImg.match(/^url[("']+(.*\.png)[)"']+$/i)) {
            s = RegExp.$1;
            if (e.currentStyle.width === 'auto' &&
                e.currentStyle.height === 'auto') {
              e.style.width = e.offsetWidth + 'px';
            }
            e.style.backgroundImage = 'none';
            filt(e, s, 'crop');
            // IE link fix.
            for (n = 0; n < e.childNodes.length; n += 1) {
              if (e.childNodes[n].style) {
                e.childNodes[n].style.position = 'relative';
              }
            }
          }
          else {
            filt(e);
          }
        }
      }
    };
  }

  if (!window.doIEPNGFixImgFrom) {
    doIEPNGFixImgFrom = function (rootEle) {
      var i, ilen,
          imgs = rootEle.getElementsByTagName('IMG');
      for (i = 0, ilen = imgs.length; i < ilen; i += 1) {
        doIEPNGFix(imgs[i]);
      }
    };
  }

  for (i = 0, iLen = els_to_fix.length; i < iLen; i += 1) {
    doIEPNGFixImgFrom(document.getElementById(els_to_fix[i]));
  }
}
