/**
*	Bonanza Team - 2007
*	Papp Laszlo (papp.laszlo@bteam.hu)
*/

var Cookie = {
  set: function(name, value, daysToExpire) {
    var expire = '';
    if (daysToExpire != undefined) {
      var d = new Date();
      d.setTime(d.getTime() + (86400000 * parseFloat(daysToExpire)));
      expire = '; expires=' + d.toGMTString();
    }
    return (document.cookie = escape(name) + '=' + escape(value || '') + expire);
  },
  get: function(name) {
    var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]*)'));
    return (cookie ? unescape(cookie[2]) : null);
  },
  erase: function(name) {
    var cookie = Cookie.get(name) || true;
    Cookie.set(name, '', -1);
    return cookie;
  },
  accept: function() {
    if (typeof navigator.cookieEnabled == 'boolean') {
      return navigator.cookieEnabled;
    }
    Cookie.set('_test', '1');
    return (Cookie.erase('_test') === '1');
  }
};


var LogoSpinner = Class.create({

  initialize: function(element, scrollspeed) {
    this.spinPanel = $(element);
    this.spinPanel.makePositioned();
    this.scrollSpeed = scrollspeed;
    this.spinning = false;
    
    this.createSkeleton();
  
    this.setObservers();
  },


  createSkeleton: function() {
    
    // Save and clear the content
    var content = this.spinPanel.innerHTML;

    // Create the new skeleton
    var logosPanel = new Element('div');
    logosPanel.setStyle(  { position: 'absolute', overflow: 'hidden'});
    

    // Where the logos be.
    var panel1 = new Element('div');
    panel1.setStyle({ float: 'left', whiteSpace: 'nowrap'});
    panel1.innerHTML = content;
    logosPanel.appendChild(panel1);

    this.spinPanel.innerHTML = "";
    this.spinPanel.appendChild(logosPanel);
    

    // For modulus
    this.logosWidth = panel1.getWidth();
    this.logosHeight = panel1.getHeight();
    var width = this.logosWidth;
    this.panelWidth = this.spinPanel.getWidth();
    this.panelHeight = this.spinPanel.getHeight() || this.logosHeight;


    // If greater than the panel width, it will spin.
    if( this.logosWidth > this.panelWidth ) {
      var panel2 = new Element('div');
      panel2.setStyle({float: 'left'});
      panel2.innerHTML = content;
      width = width * 2;
      this.spinning = true;
      logosPanel.setStyle({left: this.getPosition()+'px'});
      logosPanel.appendChild(panel2);
    }
    
    this.spinPanel.setStyle({width: width+'px'});
    this.logosPanel = logosPanel;
  },


  setObservers: function() {
    var start = this.start_spinning.bindAsEventListener(this);
    var stop = this.stop_spinning.bindAsEventListener(this);
    
    Event.observe( this.spinPanel, 'mouseover', stop);
    Event.observe( this.spinPanel, 'mouseout', start);
  },

  setPosition: function(x) {
    this.logosPanel.setStyle({  left: -x + 'px',
                                clip: 'rect( 0px '
                                             + (this.panelWidth + x) +'px '
                                             + (this.panelHeight) +'px '
                                             + x +'px)',
                                      overflow: 'hidden'});
  },

  start_spinning: function(x) {
    this.x = this.x || 1;
    var spin = this.spin.bind(this);
    if( this.spinning == true) {
      this.spinner = new PeriodicalExecuter(spin, this.scrollSpeed);
    }
  },

  stop_spinning: function() {
    if(this.spinner) this.spinner.stop();
  },

  spin: function() {
    this.setPosition(this.x);
    this.x = (this.x + 1) % this.logosWidth;
  },

  getPosition: function(){
    if( Cookie.get('position') ) {
      this.x = parseInt(Cookie.get('position'));
    }
    else {
      this.x = this.x || Math.floor(Math.random()*this.logosWidth);
    }
    return this.x;
  },

  save_position: function(){
    Cookie.set('position', this.x, 0.1);
  }

})

var r_p_pos = 710;
var x_p_pos = 0;
function change_position(element, value) {
  var x = value * 100;
  r_p_pos = r_p_pos + x;
  x_p_pos = x_p_pos + x;
  element.setStyle({  left: -x_p_pos + 'px',
                      clip: 'rect( 0px '
                          + r_p_pos + 'px '
                          + '60px '
                          + x_p_pos +'px)',
                          overflow: 'hidden'});
}

