var _Rotators = [];

function AdRotator(DefaultInterval) {
  this.DefaultInterval = DefaultInterval
  this.ID = _Rotators.length;
  _Rotators[this.ID] = this;
  this.Urls = [];
  this.Intervals = [];
  this.Items = 0;
  this.Images = [];
  this.Anchors = [];

  this.addItem = function(ImageRef, Url, Interval) {
    if (!(Interval > 0)) {
      Interval = DefaultInterval;
    }

    this.Images[this.Items] = document.createElement("img");
    this.Images[this.Items].src = ImageRef;
    this.Images[this.Items].border = 0;

    this.Anchors[this.Items] = document.createElement("a");
    this.Anchors[this.Items].href = Url;
    this.Anchors[this.Items].target = '_blank';
//    this.Anchors[this.Items].appendChild(this.Images[this.Items]);

    this.Urls[this.Items] = Url;
    this.Intervals[this.Items] = Interval;

    this.Items++;
  }

  this.start = function(CanvasName) {
    if (this.Items == 0) {
      return;
    }

    this.displayItem(CanvasName, 0);
  }

  this.displayItem = function(CanvasName, CurrentItem) {
    var Canvas = document.getElementById(CanvasName);
    var Interval = this.Intervals[CurrentItem];
    var TempImage;

    TempImage = document.createElement("img");
    TempImage.border = 0;

    while (Canvas.hasChildNodes()) {
      Canvas.removeChild(Canvas.firstChild);
    }
    Canvas.appendChild(this.Anchors[CurrentItem]);

    while (this.Anchors[CurrentItem].hasChildNodes()) {
      this.Anchors[CurrentItem].removeChild(this.Anchors[CurrentItem].firstChild);
    }
    this.Anchors[CurrentItem].appendChild(TempImage);

    TempImage.src = this.Images[CurrentItem].src;

    CurrentItem++;
    if (CurrentItem == this.Items) {
      CurrentItem = 0;
    }
    setTimeout('_Rotators[' + this.ID + '].displayItem("' + CanvasName + '", "' + CurrentItem + '")', Interval)
  }
}

