/*  #################################################
  
  Newsticker
  -------------------------------------------------
	company:	DACHCOM digital AG
	Author:	Kai Tallafus <kai.tallafus@gmx.at>
	date:		2009-09
	www:		www.dachcomdigital.com
  	
/*	################################################# */

var Ticker = Class.create({
	
  setOptions: function(options) {
		this.options = Object.extend({
			stepWidth: 1, /* How much pixel to move on each step */
			interval: 0.01 /* Make a step each 0.01 sec */
		}, options || {});
	},
	
	initialize: function(el,options){
		this.setOptions(options);
		this.viewport = $(el);
		this.el = this.viewport.down();
		this.items = this.el.select('li');
		
		this.left = this.viewport.getWidth();
		this.w = 0;
		this.h = 0;

		this.h = this.el.getHeight();
		this.items.each(function(li,index) {
			this.w += li.getWidth() + 1;
		}.bind(this));

    this.el.setStyle({
			position: 'absolute',
			width: this.w+'px',
			height: this.h+'px',
			left: this.left+'px',
			zIndex: '0'
		});
		
		this.el.observe('mouseover', function(){
      this.pause();
    }.bind(this));
		this.el.observe('mouseout', function(){
      this.resume();
    }.bind(this));
		
		this.startScroll();
	},
	
	pause: function() {
    if (this.run) {
      this.scroller.stop();
	    this.run = false;
	    this.scroller = null;
	  }
	},
	
	resume: function() {
    if (!this.run) {
  	  this.startScroll();    
    }
	},
	
	startScroll: function() {
	 	this.run = true;
		this.scroller = new PeriodicalExecuter(function(pe) {
      this.scroll();
    }.bind(this), this.options.interval);
	},
	
	scroll: function() {
	  this.left-=this.options.stepWidth;
    if ((this.left + this.w) < 0) {
      this.left = this.viewport.getWidth();
    }
    this.el.setStyle({
			left: this.left+'px'
		});
  }
	
});
