var Galleri = Class.create({
	
	initialize : function(id, images, visSomLysavis, pxPerSec, visScrollbar) {				
		this.id = id;
		this.images = images;
		this.visSomLysavis = visSomLysavis == 1;
		this.pxPerSec = pxPerSec;
		this.visScrollbar = visScrollbar == 1;
		this.banner = $('galleri_' + this.id + '_innerContainer');
		this.container = this.banner.up();
		this.startPoint = 0;
		Event.observe(document,'dom:loaded',this.modifyDom.bindAsEventListener(this));		
		if (this.visSomLysavis)	{ this.startLysavis(); }
	},
	
	modifyDom : function() {
		$$('#galleri_'+this.id+' .galleri_billede').each(function(a) {			
			a.href = '';		
			a.observe('click', this.aClicked.bindAsEventListener(this));
		}.bind(this));
		if (this.visSomLysavis && !this.visScrollbar) {
			this.container.setStyle({
				overflow: 'hidden'
			});
			//Event.observe(window,'click', this.windowClick.bindAsEventListener(this));
		}
		if (this.visSomLysavis && this.visScrollbar) {
			this.container.observe('mousedown', this.containerMousedown.bindAsEventListener(this));
			//Event.observe(window,'click', this.windowClick.bindAsEventListener(this));			
		}
	},
	
	containerMousedown : function() {		
		if (!this.paused) {
			this.pause();
		}
	},
	
	windowClick : function() {
		if (this.paused) {
			this.unPause();
		}
	},
	
	aClicked : function(event) {	
		var a = event.element();
		
		if (!a.hasClassName('galleri_billede')) {
			a = a.up('.galleri_billede');
		} 
		a.blur();
		var billedeId = a.id.substring(16);		
		event.stop();
		this.pause();
		var width = this.images[billedeId]['width'];
		var height = parseInt(this.images[billedeId]['height']);
		if (this.images[billedeId]['text'] != '') { height += 30; }
		if (width > screen.availWidth) { width = screen.availWidth; }
		if (height > screen.availHeight) { height = screen.availHeight; }
		//if (this.imageWin && !this.imageWin.closed) { this.imageWin.focus()};
		this.imageWin = window.open('/hp/visGalleriBillede?billede='+billedeId, 'galleri_billede',"height="+height+",width="+width+',menubar=0,status=0,toolbar=0,location=0');
		this.imageWin.focus();
		clearInterval(this.interval);
		this.interval = setInterval(this.setImageWinParent.bind(this), 500);
						
	},
	
	setImageWinParent : function() {
		if (this.imageWin.setGalleriObject) {
			this.imageWin.setGalleriObject(this);
			clearInterval(this.interval);
		}
	},
	
	pause : function() {				
		this.paused = true;
	},
	
	unPause : function() {	
			
		if (this.visScrollbar) {
			this.startPoint = -this.container.scrollLeft;
		} else {			
			this.startPoint = this.banner.positionedOffset().left;
		}
		var d = new Date();
		this.startTime = d.getTime();
		this.paused = false;		
	},
	
	startLysavis : function() {				
		var d = new Date();
		this.startTime = d.getTime();
		this.pe = new PeriodicalExecuter(this.loop.bind(this),0.01);
		this.paused = false;		
		this.parentWidth = this.banner.up().getWidth();
		this.bannerWidth = this.banner.getWidth();
		this.moveBannerToStart();		
		Event.observe(window, 'focus', this.windowFocus.bindAsEventListener(this));
		//Event.observe(window, 'blur', this.windowBlur.bindAsEventListener(this));
	},
	
	windowFocus : function() {					
		if (this.paused) {
			this.unPause();
		}
	},
	
	windowBlur : function() {	
				
		if (!this.paused) {
			this.pause();
		}
	},
	
	loop : function() {
		if (!this.paused) {
			var d = new Date();
			var elapsedTime = d.getTime() - this.startTime;
			var x = Math.round(this.startPoint - elapsedTime/1000 * this.pxPerSec );
			//this.banner.update(x);
			if (!this.paused) {
				if (this.reachedEnd(x)) {
					this.pe.stop();
					this.moveBannerToStart();
					this.startLysavis();			
				} else {
					if (this.visScrollbar) {
						this.container.scrollLeft = -x;
					} else {
						this.banner.setStyle({
							left: x + 'px'
						});
					}
				}
			}
		}
	},
	
	reachedEnd : function(x) {
		if (this.visScrollbar) {
			return x < -(this.bannerWidth) + this.parentWidth - this.pxPerSec * 2;
		} else {
			return x < -(this.bannerWidth);
		}		
	},
	
	moveBannerToStart : function() {
		if (this.visScrollbar) {			
			this.startPoint = 0;
		} else {
			this.startPoint = this.parentWidth;
		}
	}
	
});
