/***********************************************************************************************************************
 *
 * - $Id: flashbanner.js 6759 2009-12-18 14:58:57Z mdehaan $
 *
 * Copyright Rovecom ICT BV
 *
 **********************************************************************************************************************/

var FlashBanner = Class.create({
  initialize: function(instanceName, numDivs, interval, randomize, fade, fadeSpeed, appear, appearSpeed, queue, animate) {
    this.instanceName = instanceName;
    this.numDivs = parseInt(numDivs);
    this.randomize = randomize || false;
    this.interval = parseInt(interval) * 1000 || 3000;
    this.appear = appear || 'none';
    this.fade = fade || 'none';
    this.fadeRandom = false;
    this.appearRandom = false;
    this.animate = animate;
    
    if (queue == 'true')
      this.queue = true;
    else
      this.queue = false;
  
    switch (fadeSpeed) {
      case 'high': this.fadeSpeed = 1.0; break;
      case 'low': this.fadeSpeed = 3.0; break;
      case 'normal': this.fadeSpeed = 2.0; break;
      default: alert("Snelheid '" + fadeSpeed + "' onbekend"); break;   
    }
    switch (appearSpeed) {
      case 'high': this.appearSpeed = 1.0; break;
      case 'low': this.appearSpeed = 3.0; break;
      case 'normal': this.appearSpeed = 2.0; break;
      default: alert("Snelheid '" + appearSpeed + "' onbekend"); break;
    }
    switch (fade) {
      case 'none': this.fade = 0; break;
      case 'fade': this.fade = 1; break;
      case 'slideup': this.fade = 2; break;
      case 'blindup': this.fade = 3; break;
      case 'shrink': this.fade = 4; break;
      case 'fold': this.fade = 5; break;
      case 'dropout': this.fade = 6; break;
      case 'squish': this.fade = 7; break;
      case 'puff': this.fade = 8; break;
      case 'switchoff': this.fade = 9; break;
      case 'random': this.fadeRandom = true; break;
      default: alert('Onbekend effect: ' + fade); break;
    }
    
    switch (appear) {
      case 'none': this.appear = 0; break;
      case 'appear': this.appear = 1; break;
      case 'slidedown': this.appear = 2; break;
      case 'blinddown': this.appear = 3; break;
      case 'grow': this.appear = 4; break;
      case 'random': this.appearRandom = true; break;
      default: alert('Onbekend effect: ' + appear); break;
    }
  },

  random: function() {
    return Math.floor(this.numDivs * Math.random());
  },
  
  
  next: function() {
    var currDiv, nextDiv, opts;
    
    
    do {
      var next;
      
      if (this.randomize == 'true') {
        next = this.random();
      } else if (this.current >= this.numDivs - 1) {
        next = 0;
      } else {
        next = this.current + 1;
      }
    } while (this.current == next);
   
    currDiv = $(this.instanceName + this.current);
    nextDiv = $(this.instanceName + next); 
    if (this.queue) {
      opts = { duration: this.fadeSpeed, queue: 'front'}; 
    } else {
      opts = { duration: this.fadeSpeed };
    }
    if (this.fadeRandom == true) {
      this.fade = Math.floor(9 * Math.random()) + 1;
    }
    switch(this.fade) {
      case 0: currDiv.hide(); break;
      case 1: currDiv.fade(opts);break;
      case 2: currDiv.slideUp(opts); break;
      case 3: currDiv.blindUp(opts); break;
      case 4: currDiv.shrink(opts); break;
      case 5: currDiv.fold(opts); break;
      case 6: currDiv.dropOut(opts); break;
      case 7: currDiv.squish(opts); break;
      case 8: currDiv.puff(opts); break;
      case 9: currDiv.switchOff(opts); break;
    }
        
    if (this.queue) {
      opts = { duration: this.appearSpeed, queue: 'end'} ;
    }
    else {
      opts = { duration: this.appearSpeed };
    }
    if (this.appearRandom == true) {
      this.appear = Math.floor(4*Math.random())+1;
    }
     switch(this.appear) {
      case 0: nextDiv.show(); break;
      case 1: nextDiv.appear(opts); break;
      case 2: nextDiv.slideDown(opts); break;
      case 3: nextDiv.blindDown(opts); break;
      case 4: nextDiv.grow(opts); break;
    }
      
    this.current = next;
    var self = this;
    setTimeout(function() { self.next(); }, this.interval);
  },
  
  start: function() {
    var startDiv = 0;
    if (this.randomize) startDiv = this.random();
    if (this.animate == 'false') {
      $(this.instanceName + startDiv).show();
      return;
    }
    this.current = startDiv;
    var currDiv = $(this.instanceName + this.current);
    
    if (this.numDivs > 0 && currDiv)  
      currDiv.show();
    
    if (this.numDivs > 1) {
      var self = this;
      setTimeout(function() { self.next(); }, this.interval);     
    }
  }
});
