// JavaScript Document

function bannerdan(){

  this.timer = 2;
  this.bannerNum = 0;// -1 = random

  this.banners = [];
  this.binding = null;
  this.timeout = null;

  this.add = function(){
    this.banners[this.banners.length] = [arguments[0], arguments[1]];
  }

  this.bind = function(){
    if(typeof arguments[0] == 'string')
      this.binding = document.getElementById(arguments[0]);
    else
      this.binding = arguments[0];
    this.rotate();
  }

  this.rotate = function(){
    if( ! this.empty())
      return;
    var showNum, tmpA = document.createElement('a'), tmpImg = document.createElement('img');

    if(this.bannerNum < 0)
      showNum = Math.floor(Math.random()*this.banners.length);
    else
      showNum = this.bannerNum=(++this.bannerNum >= this.banners.length)?0:this.bannerNum;

    tmpA.href = this.banners[showNum][0];
    tmpImg.src = this.banners[showNum][1];
    tmpA.appendChild(tmpImg);
    this.binding.appendChild(tmpA);
  }

  this.empty = function(){
    if(this.binding == null)
      return false;
    while(this.binding.hasChildNodes())
      this.binding.removeChild(this.binding.firstChild);
    return true;
  }

  this.startTimer = function(){
    this.stopTimer();
    this.timeout = window.setInterval(
      (function(x){
        return function(){
          x.rotate();
        }
      })(this), this.timer*1000);
  }

  this.stopTimer = function(){
    if(this.timeout != null)
      window.clearInterval(this.timeout);
    this.timeout = null;
  }
}

