/* ie-png.js */
/* this script enable an alpha channel of 24bit PNG in IE6 by this only read. */

// Set 1x1px alpha gif.
var gif = 'images/null.gif';

// Setting end.

var env = new Object;
env.ua = navigator.userAgent.toLowerCase();
env.win = (env.ua.indexOf('windows') != -1)? 1: 0;
env.ie = (!window.opera && env.ua.indexOf('msie') != -1)? Number(env.ua.charAt(env.ua.indexOf('msie ') + 5)): 0;

var url = new Object;
url.host = 'http://' + window.location.host;
url.path = window.location.pathname.replace(/^(.*?)\/[^\/]*$/,'$1') + '/';

function setOnload(func){
  (window.addEventListener)? window.addEventListener('load',func,false):
  (window.attachEvent)? window.attachEvent('onload',func):
  window.onload = func;
}

var checkPNG = {   // background, background-image CSS only.
  CSS: function() {
    if(!env.win || env.ie > 6 || env.ie < 5) return false;
    var path = new Array();
    var num = 0;
    var node = document.getElementsByTagName('head')[0].childNodes;
    for(var i=0;i<node.length;i++){
      if(node[i].nodeName.match(/^style$/i)) path[num] = url.host + url.path;
      else if(node[i].nodeName.match(/^link$/i) && node[i].getAttribute('rel') && node[i].getAttribute('rel').match(/stylesheet/i)){
        var href = node[i].getAttribute('href');
        if(href.match(/^\//)) path[num] = url.host + href;
        else if(!href.match(/^https?:\/\//)) path[num] = url.host + url.path + href.replace(/^(([^\/]*\/)*).*?$/,'$1');
        else path[num] = href;
      }
      if(path[num]) num++;
    }
    for(var i=0;i<document.styleSheets.length;i++){
      var rule = document.styleSheets[i].rules;
      for(var j=0;j<rule.length;j++) {
        var png = checkStyle(rule[j],'background');
        if(!png) png = checkStyle(rule[j],'backgroundImage');
        if(!png) continue;
        png = (png.match(/^\//))? url.host + png: (!png.match(/^https?:\/\//i))? path[i] + png: png;
        rule[j].style['filter'] = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=' + png + ',sizingMethod=crop)';
      }
    }
    function checkStyle(obj,propaty){
      var value = obj.style[propaty];
      if(!value || !value.match(/\.png/i)) return false;
      var png = value.replace(/^.*?url\(["']*([^"']+\.png)["']*\).*$/i,'$1');
      obj.style[propaty] = '';
      return (!png.match(/^[^"']+\.png$/i))? '': png;
    }
  },
  HTML: function(){   // '<img>' elements only.
    if(!env.win || env.ie > 6 || env.ie < 5) return false;
//    var gif = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=='; // this line is unused.
    var img = document.getElementsByTagName('img');
    for(var i=0;i<img.length;i++){
      var src = img[i].getAttribute('src');
      if(!src.match(/\.png/i)) continue;
      if(!src.match(/^https?:\/\//i)) src = url + src;
      img[i].setAttribute('src',gif);
      img[i].style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=' + src + ',sizingMethod=scale)';
    }
  }
}

function onloads(){
  checkPNG.HTML();
}

checkPNG.CSS();

setOnload(onloads);
