//==============================================================
var Animator = {
  basedir:             false,
  imgURLs:             false,
  nLeft:               false,
  dwell:               false,
  imgIndex:            1,
  disable:             'false',

  // method-------------------------------------------
  init: function() {
    Animator.readConfig();

    if (Animator.disable == 'false') {
      Animator.startAnimation();
    }
  },
  //---------------------------------------------------

  // method-------------------------------------------
  readConfig: function() {
    Animator.basedir = document.getElementById("animator_basedir").getAttribute("value");
    var imagesStr = document.getElementById("animator_images").getAttribute("value");
    Animator.dwell = parseInt(document.getElementById("animator_dwell").getAttribute("value") * 1000);

    if (document.getElementById("animator_disable")) {
      Animator.disable = document.getElementById("animator_disable").getAttribute("value");
    }
  
    Animator.imgURLs = imagesStr.split(',');
    Animator.nLeft = Animator.imgURLs.length;

    for (var i=0; i < Animator.imgURLs.length; i++) {
      Animator.imgURLs[i] = Animator.basedir + Animator.imgURLs[i];
      Animator.imgURLs[i] = Animator.imgURLs[i].replace(/\s+/, '');
    }
    
  },
  //---------------------------------------------------

  // method-------------------------------------------
  startAnimation: function() {
    imgArr = new Array();
    for (var i=0; i < Animator.imgURLs.length; i++) {
      imgArr[i] = new Image();
      imgArr[i].onload = Animator.startTimer;
      imgArr[i].src = Animator.imgURLs[i];
    }
  },
  //---------------------------------------------------

  // method-------------------------------------------
  startTimer: function() {
     Animator.nLeft--;
     if (Animator.nLeft == 0) {
       setInterval('Animator.changeImage()', Animator.dwell );
     }
  },
  //---------------------------------------------------

  // method-------------------------------------------
  changeImage: function() {
    var index = Animator.imgIndex;
    document.images["animator"].src = Animator.imgURLs[index];         
    var newIndex = index + 1;
    if (newIndex > Animator.imgURLs.length - 1) { newIndex = 0; }
    Animator.imgIndex = newIndex;
  }
  //---------------------------------------------------

}
//==============================================================

EventUtil.addEventHandler(window, 'load', Animator.init);