var Lysavis = Class.create({
	
	initialize : function(id, speed, pause, centerVertically) {		
		this.banner = $(id);
		this.speed = speed;
		this.speeds = [10, 20, 30]; // px/sekund [langsom, mellem, hurtig]
		this.pxPerSec = this.speeds[this.speed];
		this.pause = pause;
		this.centerVertically = (centerVertically == 1);
		this.parentWidth = this.banner.up().getWidth();
		this.bannerWidth = this.banner.getWidth();
		this.distance = this.parentWidth + this.bannerWidth;
		this.banner.setStyle({display: 'block'});
		this.moveBannerToStart();
		this.startAnim();
	},
	
	moveBannerToStart : function() {
		this.banner.setStyle({
			top: 0,
			left: this.parentWidth + 'px'
		});
		if ( this.centerVertically ) {
			this.banner.setStyle({
				top: Math.round((this.banner.up().getHeight() - this.banner.getHeight())/ 2) + 'px'
			});
		}
	},
	
	startAnim : function() {
		var d = new Date();
		this.startTime = d.getTime();
		this.pe = new PeriodicalExecuter(this.loop.bind(this),0.01);
	},
	
	loop : function() {
		var d = new Date();
		var elapsedTime = d.getTime() - this.startTime;
		var x = Math.round(this.parentWidth - elapsedTime/1000 * this.pxPerSec );
		//this.banner.update(x);
		if (x < -(this.bannerWidth)) {
			this.pe.stop();
			this.moveBannerToStart();
			setTimeout(this.startAnim.bind(this), this.pause * 1000);
		} else {
			this.banner.setStyle({
				left: x + 'px'
			});
		}
	}	
})
