var nurgad = {
  varv: 'rgb(255, 255, 255)',
  fn: function(){
    if (!document.getElementById || !document.createElement || !Array.prototype.push)
      return;
    var n = [], i, j;
    for (i=0; i<3; i++){
      n[i] = document.createElement('DIV');
      n[i].className = 'n'+(i+1);

    }
    var div = document.getElementsByTagName('*'), list = [], o, e, taust, piirjoon = [], u;
    for (i in div)
      if (/(^|\s)nurgad(\s|$)/.test(div[i].className))
        list.push(div[i]);
    for (i = 0; i<list.length; i++){
      e = o = list[i];
      if (i == 0 || o.parentNode != list[i-1].parentNode){
        do {
          e = e.parentNode;
          taust = e.taust || nurgad.taust(e);
        } while (!taust && e != document.documentElement);
        o.parentNode.taust = taust || [255, 255, 255];
        taust = nurgad.taust(o) || nurgad.rgbList(o.style.backgroundColor = nurgad.varv);
        for (j=0; j<3; j++)
          piirjoon[j] = Math.round((taust[j]+o.parentNode.taust[j])/2);
      }
      e = document.createElement('DIV');
      do { e.appendChild(o.childNodes[0]); } while (o.childNodes.length > 0);
      o.appendChild(e);
      if(1==1){
      o.style.backgroundColor = 'transparent';
      e.style.backgroundColor = 'rgb('+taust.join(', ')+')';
      u = ['paddingTop', 'paddingRight', 'paddingBottom', 'paddingLeft'];
      for(j in u)
        u[j] = nurgad.stiil(o, u[j]) || '0px';
      e.style.padding = u.join(' ');
      o.style.padding = '0px';
      }
      for (j=2; j>=0; j--){
        e = n[j].cloneNode(false);
        e.style.backgroundColor = 'rgb('+taust.join(', ')+')';
        e.style.borderColor = 'rgb('+piirjoon.join(', ')+')';
        o.insertBefore(e, o.firstChild);
        o.appendChild(e.cloneNode(false));
      }
    }
    e = document.createElement('style');
    e.type = 'text/css';
    var css = '.n1, .n2, .n3 { font-size: 1px; border-style: solid; border-width: 0px 1px; height: 1px; overflow: hidden; }\n\
      .n1 { margin: 0px 2px; border-width: 0px 2px; border-width: 0px 2px; }\n\
      .n2 { margin: 0px 1px; }\n\
      .n3 { height: 2px; }\n';
    if (e.styleSheet)
      e.styleSheet.cssText = css;
    else
      e.appendChild(document.createTextNode(css));
    document.getElementsByTagName('head')[0].appendChild(e);
  },
  taust: function (o){
    var x = o.style.backgroundColor || nurgad.stiil(o, 'backgroundColor');
    return !x || x == 'transparent' || x == 'inherit' || x == 'rgba(0, 0, 0, 0)' ? false : nurgad.rgbList(x);
  },
  rgbList: function (v){
    var n;
    if (v.substr(0, 1) == '#' && v.length == 4)
      v = v.substr(0, 2)+v.substr(1, 2)+v.substr(2, 2)+v.substr(3, 1);
    if (n = v.match(/^#([A-Fa-f\d]{2})([A-Fa-f\d]{2})([A-Fa-f\d]{2})$/))
      return [parseInt(n[1], 16), parseInt(n[2], 16), parseInt(n[3], 16)];
    else if (n = v.match(/^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$/))
      return [parseInt(n[1], 10), parseInt(n[2], 10), parseInt(n[3], 10)];
    else
      return [255, 255, 255];
  },
  stiil: function (o, s){
    if (o.currentStyle)
      return o.currentStyle[s];
    else if (window.getComputedStyle)
      return document.defaultView.getComputedStyle(o, null).getPropertyValue(s.replace(/([A-Z])/g, '-$1').toLowerCase());
    return false;
  }
};
