function AdRotator(adsdir, adsary, imoary, maxnum, interval, fonprogress, fonpreloaded) {
  this.sAdsdir = adsdir;
  this.nInterval = interval;
  
  this.fOnProgress = fonprogress;
  this.fOnPreloaded = fonpreloaded;

  this.nImages = 0;
  this.nProcessed = 0;
  this.nLoaded = 0;
    
  if (adsary.length != imoary.length) adsary.length = imoary.length;
  this.Ads = adsary;
  this.Ims = imoary;
  this.Rts = [];
  
  for (var i = 0; i < this.Ads.length; i++) {
    //this.Rts.push([0,0]);
    this.Rts[this.Rts.length] = [0,0];
  }
  
  for (var i = 0; i < maxnum; i++) {
    for (var j = 0; j < this.Ads.length; j++) {
      if (this.Ads[j].length > i) {
        this.nImages++;
      }
    }
  }
  
  var self;
  this.start = function () {
    self = this;
    this.oInterval = window.setInterval(rotate, this.nInterval*1000);
    for (var i = 0; i < maxnum; i++) {
      for (var j = 0; j < this.Ads.length; j++) {
        if (this.Ads[j].length > i) {
          //this.Ads[j][i].push(this.Ims[j]);
          this.Ads[j][i][this.Ads[j][i].length] = this.Ims[j];
          this.preload(this.Ads[j][i], this.Ims[j], j);
        }
      }
    }
  };

  function rotate() {
    if (!document.images) return;
    for (var i = 0; i < self.Ads.length; i++) {
      if (self.Rts[i][0] > 1) { 
        self.Rts[i][1]++; 
        if (self.Rts[i][1] >= self.Rts[i][0]) self.Rts[i][1] = 0;
        document.images[self.Ims[i]].src = self.Ads[i][self.Rts[i][1]][3].src; 
        self.Ads[i][self.Rts[i][1]][3].lnko.value = self.Ads[i][self.Rts[i][1]][3].wsn;
      }
    }
  }

  this.stop = function () {
    window.clearInterval(this.oInterval);
    self = undefined;
  };
}

AdRotator.prototype.preload = function(ad, im, ri) {
  // create new Image object and add to array
  var oImage = new Image;
  
  // set up event handlers for the Image object
  oImage.onprogress = AdRotator.prototype.onprogress;
  oImage.onload = AdRotator.prototype.onload;
  oImage.onerror = AdRotator.prototype.onerror;
  oImage.onabort = AdRotator.prototype.onabort;

  // assign pointer back to this.
  oImage.oAdRotator = this;
  oImage.bLoaded = false;
  oImage.wsn = ad[1];
  oImage.ari = ri;
  oImage.id = im;
  oImage.name = im;
  oImage.lnko = eval('document.forms.frmads.'+im+'_lnk');
  
  // assign the .src property of the Image object
  oImage.src = this.sAdsdir+ad[0];

  //ad.push(oImage);
  ad[ad.length] = oImage;
}

AdRotator.prototype.oncomplete = function() {
  if (this.fOnPreloaded != null) {
    this.fOnPreloaded(this.Ads, this.nLoaded);
  }
}

AdRotator.prototype.onprogress = function() {
  this.nProcessed++;
  if (this.nProcessed == this.nImages) {
    this.oncomplete();
  }
  if (this.fOnProgress != null) {
    this.fOnProgress(this.Ads, this.nImages, this.nProcessed, this.nLoaded);
  }
}

AdRotator.prototype.onload = function() {
   this.bLoaded = true;
   this.oAdRotator.nLoaded++;
   this.oAdRotator.Rts[this.ari][0]++; 
   this.oAdRotator.onprogress();
}

AdRotator.prototype.onerror = function() {
   this.bError = true;
   this.oAdRotator.onprogress();
}

AdRotator.prototype.onabort = function() {
   this.bAbort = true;
   this.oAdRotator.onprogress();
}

function addirect(wsn) {
  if (wsn == '') { return; }
  window.open('addirector.php?wsn='+wsn);
}