// Spritefunktioner:
function createSprite(loopAnimation) {
  this.currentFrame = 0;
  this.loopAnimation = loopAnimation;
  this.animationRunning = false;
  this.play = startSprite;
  this.pause = pauseSprite;
  this.stop = stopSprite;
  this.animateSprite = animateSprite;
  this.loadSprite = loadSprite;
  this.frameCount = this.fileName.length;
  this.loadSprite();
}

function startSprite() {
  if (visObj(this.id)) {
    if (!this.animationRunning) {
      this.animationRunning = true;
      this.timerID = setTimeout('getObject("'+this.id+'").animateSprite();',this.delay[this.currentFrame]);
    }
  }
  else {
     this.timerID = setTimeout('getObject("'+this.id+'").play()',25);
  }
}

function stopSprite() {
  this.animationRunning = false;
  this.currentFrame = 0;
}

function pauseSprite() {
  this.animationRunning = false;
}

function animateSprite() {
  var delay = 0;
  var cur = this.currentFrame;
  if (this.animationRunning) {
    if (cur < this.frameCount) {
      this.src = this.image[cur].src;
      delay = this.delay[cur];
      this.currentFrame++;
      this.timerID = setTimeout('getObject("'+this.id+'").animateSprite();',delay);
    }
    else if (this.loopAnimation) {
      this.currentFrame = 0;
      this.timerID = setTimeout('getObject("'+this.id+'").animateSprite();',delay);
    }
    try {
      var aEventName = this.id + '_onframe' + cur;
      if (typeof(aEventName) != 'undefined')
        eval(aEventName +'()');
    } catch(e) {
      // no error handling
    }
  }
}

function loadSprite() {
  this.image = new Array();
  for (var i=0; i < this.frameCount; i++) {
    this.image[i] = new Image();
    this.image[i].src = this.fileName[i];
  }
}


