/*=================================================//
// AJAX powered corss browser marquee              //
//-------------------------------------------------//
// Author:    Daniel Schroeder                     //
// E-Mail:    info@gravitymedia.de                 //
// Version:   0.2                                  //
//=================================================*/

var Marquee = Class.create({

 initialize: function(id) {
  if(!$(id)) return;
  // PE Object for updates
  this.updatePe = null;
  // New info for update available
  this.update = false;
  // PE Object for movement
  this.movePe = null;
  // moving or not
  this.move = true;
  // Marquee container
  this.marquee = $(id);
  // (Text)Infos do be moved
  this.info = this.marquee.innerHTML.stripScripts().strip();
  // Available width
  this.availableWidth = this.marquee.getWidth();
  // Hide parts of inner container, which can not be displayed
  this.marquee.setStyle({overflow: 'hidden'});
  // Clear innerHTML
  this.marquee.update();
  // Inner container
  this.inner = new Element('div');
  // Set styles for single line
  this.inner.setStyle({position: 'absolute', left: '0px', top: '0px', whiteSpace: 'nowrap'});
  // Insert (Text)Infos
  this.inner.update(this.info);
  // Insert inner container into marquee container
  this.marquee.insert(this.inner);
  // Get real width of single line
  this.realWidth = this.inner.getWidth();
  // Offset
  this.offset = 0;
 },
 
 stopByMouseOver: function() {
  this.marquee.observe('mouseover', function() { this.move = false; }.bind(this));
  this.marquee.observe('mouseout',  function() { this.move = true;  }.bind(this));
 },
 
 setPeriodicalUpdater: function(url, parameters, interval) {
  this.updatePe = new PeriodicalExecuter(function(pe) {
   new Ajax.Request(url, {
    parameters: parameters,
    onSuccess: function(transport) {
     var info = transport.responseText.stripScripts().strip();
     if(info.length != 0 && this.info != info) {
      this.info   = info;
      this.update = true;
     }
    }
   }.bind(this));
  }.bind(this), interval);
 },
 
 removePeriodicalUpdater: function() {
  if(this.updatePe instanceof PeriodicalExecuter)
   this.updatePe.stop();
  this.updatePe = null;
 },
 
 _update: function() {
  this.update = false;
  this.offset = 0;
  this.inner.update(this.info);
  this.realWidth = this.inner.getWidth();
 },
 
 oszilate: function(speed) {
  speed = (speed) ? speed : 0.02;
  var moveleft = true;
  this.movePe = new PeriodicalExecuter(function(pe) {
   if(this.update)
    this._update();
   if(this.move) {
    if(moveleft) {
     if(this.offset > (this.availableWidth - this.realWidth))
      this.offset--;
     else
      moveleft = false;
    } else {
     if(this.offset < 0)
      this.offset++;
     else
      moveleft = true;
    }
    this.inner.setStyle({left: this.offset + 'px'});
   }
  }.bind(this), speed);
 },
 
 rotate: function(speed) {
  speed = (speed) ? speed : 0.02;
  this.movePe = new PeriodicalExecuter(function(pe) {
   if(this.update)
    this._update();
   if(this.move) {
    if((this.offset * -1) < this.realWidth)
     this.offset--;
    else
     this.offset = this.availableWidth;
    this.inner.setStyle({left: this.offset + 'px'});
   }
  }.bind(this), speed);
 },

 loop: function(speed, trail) {
  speed = (speed) ? speed : 0.02;
  trail = (trail) ? trail : '';
  this.movePe = new PeriodicalExecuter(function(pe) {
   if(this.update)
    this._update();
   if(this.move) {
    if(this.offset == 0) {
     this.inner.insert(trail);
     this.realWidth = this.inner.getWidth();
     if(this.availableWidth > this.realWidth) {
      for(var i = this.realWidth; i < (2 * this.availableWidth) + 10; i += this.realWidth) {
       this.inner.insert(this.info);
       this.inner.insert(trail);
      }
     } else {
      this.inner.insert(this.info);
      this.inner.insert(trail);
     }
    }
    if((this.offset * -1) >= this.realWidth)
     this.offset = -1;
    else
     this.offset--;
    this.inner.setStyle({left: this.offset + 'px'});
   }
  }.bind(this), speed);
 },
 
 stop: function() {
  if(this.movePe instanceof PeriodicalExecuter)
   this.movePe.stop();
 }

});

