var widget = widget || {};

widget.Ticker = new Class({
  element: null,
  options: {seconds: 10, direction: 1, slots: 0},
  sizes: {},
  current: -1,
  timer: null,

  initialize: function(element, options) {
    this.element = $(element);
    this.wrapper = this.element.getParent();

    this.sizes = {
      element: parseInt(this.element.getStyle('height')),
      wrapper: parseInt(this.wrapper.getStyle('height'))
    };
 
    Object.extend(this.options, options || {});
    
    this.next();
		    
  },

	slots: function() {
		return Math.floor(parseInt(this.element.getStyle('height')) / parseInt(this.wrapper.getStyle('height')));
	},

  mod: function(num, mod) {
    return (mod + (num % mod)) % mod;
  },
  
  next: function(stop) {
    clearTimeout(this.timer);
    this.set(this.current + this.options.direction);
    
    if(stop != true)
      this.timer = setTimeout(this.next.bind(this), this.options.seconds * 1000);
  },

  prev: function(stop) {
    clearTimeout(this.timer);
    
    this.set(this.current - this.options.direction);
    
    if(stop != true)
      this.timer = setTimeout(this.prev.bind(this), this.options.seconds * 1000);
  },
  
  set: function(index) {
    this.current = this.mod(index, this.slots());
    this.element.tween('top', this.current * this.sizes.wrapper * -1);
  }
  
});

