var mtjs_dd_relateds=new Array(),
    mtjs_dd_related=null;

function mtjs_dd_related_class(path){
  this.index=mtjs_dd_relateds.push(this)-1;
  if (this.index===0){
    this.readypile=new Array();
    this.bgreg=new RegExp(/BACKGROUND[^:]*:[^;]*url\([^)]+\.[Pp][Nn][Gg]\)/);
    this.selectors='';
    this.interval=100;
    this.cycles=0;
    this.maxcycles=30;
    this.head=document.getElementsByTagName('head')[0];
    this.path=path;
    this.search();
  } else {
    throw "mtjs_dd_related_class: mtjs_dd_related is already set.";
  }
}    

mtjs_dd_related_class.prototype.add_script=function(){
  var newjs=document.createElement('script');
  this.head.appendChild(newjs);
  newjs.setAttribute('type', 'text/javascript');
  return newjs;
};

mtjs_dd_related_class.prototype.search=function(){
  var sheetiter,
      rulelist,
      inneriter;

  if ((navigator.userAgent.search("MSIE[ \t]+6\.")!=-1)&&(typeof(document.styleSheets)!='undefined')) {
    
    this.includer();
      
    for(sheetiter=0;sheetiter<document.styleSheets.length;sheetiter++){
      rulelist=document.styleSheets[sheetiter].rules;
      for (inneriter=0;inneriter<rulelist.length;inneriter++){
        if (rulelist[inneriter].style.cssText.toString().search(this.bgreg)!=-1){
          if (this.selectors.length>0){
            this.selectors+=', ';
          }
          this.selectors+=rulelist[inneriter].selectorText;
        }
      }
    }

    this.injector('selectors');
  }
};

mtjs_dd_related_class.prototype.injector=function(which){
  if (this.readypile.push(which)==2){
    this.add_script().text='DD_belatedPNG.fix("'+this.selectors+'");';
  }
};

mtjs_dd_related_class.prototype.callgen=function(){
  return 'mtjs_dd_relateds['+this.index+'].tap();';
};

mtjs_dd_related_class.prototype.includer=function(){
  this.add_script().setAttribute('src',this.path);
  setTimeout(this.callgen(), this.interval);
};

mtjs_dd_related_class.prototype.tap=function(){
  if ((typeof(DD_belatedPNG)!='undefined')&&(typeof(DD_belatedPNG.fix)!='undefined')){
    this.injector('script');
  } else {
    if (++this.cycles<this.maxcycles){
      setTimeout(this.callgen(), this.interval);      
    }
  }
};  

mtjs_dd_related=new mtjs_dd_related_class('/js/DD_belatedPNG.js');
