/****************************************************************
//
// Copyright: Holiday Media 2006
//
****************************************************************/
var PanningClass = Class.create();

PanningClass.prototype = {

  initialize: function(element) {
    this.element = $(element);
    this.default_options = Object.extend({
      speed: 20,        // timeout between moves; set it lower to increase speed
      move: 10,         // movement at one step in pixel
      multiplier: 1
    }, arguments[1] || {});

    this.timer = null;
    this.scrollByUser = false;
    this.scrollByEffect = false;
    this.jumpLeft        = this.default_options.panWidth/2 - this.default_options.imageWidth/1.5;
    this.jumpRight       = this.default_options.panWidth/2 - this.default_options.imageWidth*1.5;

    // Initialize options
    this.options = Object.extend(this.default_options, arguments[1] || {});
    
    // Initialize background layer
    Element.setStyle( this.element, { left: this.getNewLeft(parseInt(this.element.style.left)) + "px" } );
  },
  panRight: function(){
  	if (!this.scrollByUser) return;
  	var left = this.getNewLeft( parseFloat(this.element.style.left) );
    Element.setStyle( this.element, { left: left + "px" } );
  	this.timer = setTimeout( this.panRight.bind(this), this.options.speed );
  },
  panLeft: function() {
  	if (!this.scrollByUser) return;
  	var left = this.getNewLeft( parseFloat(this.element.style.left) );
    Element.setStyle( this.element, { left: left + "px" } );
  	this.timer = setTimeout( this.panLeft.bind(this), this.options.speed );
  },

  start: function(direction) {
    this.direction = direction;
    this.options = Object.extend(this.default_options, arguments[1] || {});
    this.scrollByUser = true;
    if (this.direction == 'left') {
      this.panLeft();
    }else{
      this.panRight();
    }
  },
  stop: function(){
    clearTimeout(this.timer);
    this.scrollByUser = false;
  },

  panTo: function(x, elements, callback) {
    var curLeft = this.getCumulativeLeft(x);
    var newLeft = (1000/2);
    var offset = newLeft - curLeft;

    this.scrollByEffect = true;
    var duration = 0;
    if (Math.abs(offset) < 10) {
      duration = 0;
    }else if (Math.abs(offset) < 100) {
      duration = 0.4;
    }else{
      duration = 0.8;
    }
    var panner = this;

    doEffect( this.element, {
      x: offset,
      y: 0,
      mode: 'relative',
      duration: duration,
      afterFinish: function(effect) {
        panner.scrollByEffect = false;
        if (callback) callback();
      }
    } );

    $A(elements).each( function(element) {
      doEffect( element, {
        x: parseInt( offset * (9/7) ),
        y: 0,
        mode: 'relative',
        duration: duration
      } );
    } );
  },

  getNewLeft: function(left) {
    if (this.scrollByUser) {
      if (this.direction == "left")
        left += parseFloat( this.options.move * this.options.multiplier );
      else
        left -= parseFloat( this.options.move * this.options.multiplier );
    }

    if (left >= this.jumpLeft)
      left -= this.options.imageWidth;
    else if (left <= this.jumpRight)
      left += this.options.imageWidth;

    return parseInt(left);
  },
  getCumulativeLeft: function(left) {
    var myLeft = parseInt(this.element.style.left);
    while (myLeft < 0) myLeft += this.default_options.imageWidth;
    return (myLeft + parseInt(left)) % this.default_options.imageWidth;
  }

};


var PanningCollection = Class.create();

PanningCollection.prototype = {
  initialize: function() {
    this.objects = arguments;
  },
  start: function(direction, options) {
    if (this.objects[0].scrollByEffect) return;
    $A(this.objects).each( function(object) {
      //options.multiplier *= 4;
      object.start(direction, options);
    } );
  },
  stop: function() {
    $A(this.objects).each( function(object) {
      object.stop();
    } );
  }
  /*panTo: function(x, callback) {
    this.objects[0].panTo(x, [this.objects[1].element], callback);
  }*/
};


// Wrapper around Effect.Move to reuse the effect with new options
var effectcache = {};
function doEffect(element, param) {
  var id;
  id = (typeof(element) == "object") ? element.id : element;
  if (effectcache[id] && effectcache[id].panningEffect) {
    // Re-use effect
    var options = Object.extend(param, {});
    effectcache[id].panningEffect.start(options);
  }else{
    // Create new effect
    effectcache[id] = {};
    effectcache[id].panningEffect = new Effect.Move(element, param);
  }
}