var NewsScroller=Class.create();
NewsScroller.prototype={
	initialize:function(){
		this.newsboxes = [];
		this.isMoving=false;
		this.curPos=0;
		this.boxwidth=208;
		this.x = 0;
		this.minheight=0;
		this.autostart=false;
		this.autoSpeed=6000;
		//this.prefix = '-'+Math.round(Math.random() * 10000)+'-';
		//this.holder = '';
	},
	setUp:function() {
		if($('newsscroller-content')) {
			this.x = 0;
			$A($$('#newsscroller-content li')).each(function(elm) {
				nsb.newsboxes.push(elm);
				if(parseInt(elm.getStyle('height')) >= nsb.minheight) {
					nsb.minheight = parseInt(elm.getStyle('height'));
				}
				var lpos = (nsb.x * nsb.boxwidth)+'px';
				elm.setStyle({'position':'absolute','left':lpos});
				nsb.x++;
			});
			$('newsscroller-content').setStyle({'height':this.minheight+'px'});
			var left = '<a href="javascript:void(0);" id="newsscroller-controlls-left" class="newsscroller-controlls-left" onclick="nsb.mouseClick(\'-\'); return false;" ><img src="/objects/fixed/transp.gif" alt="Vänster" /></a>';
			var middle='';
			for(x=0;x<this.newsboxes.length;x++) {
				if(x==0) {
					middle += '<img src="/objects/fixed/transp.gif" id="ns-'+x+'" class="newsscroll-position newsscroll-current" alt="nsmid" />'
				}
				else {
					middle += '<img src="/objects/fixed/transp.gif" id="ns-'+x+'" class="newsscroll-position" alt="nsmid" />'
				}
			}
			var right = '<a href="javascript:void(0);" id="newsscroller-controlls-right" class="newsscroller-controlls-right" onclick="nsb.mouseClick(\'+\'); return false;" ><img src="/objects/fixed/transp.gif" alt="Höger" /></a>';
			if(this.newsboxes.length>0) {
				$('newsscroller-controlls').update(left+''+middle+''+right);
				this.run();
			}
		}
	},
	next:function() {
		if(this.isMoving) {
			return false;	
		}
		this.isMoving=true;
		var cbox = this.newsboxes[this.curPos];
		var nbox;
		if(this.curPos == 0) {
			nbox = this.newsboxes[(this.newsboxes.length-1)];
			this.curPos=(this.newsboxes.length-1);
			nbox.setStyle({left:(0-this.boxwidth)+'px','position':'absolute'});
		}
		else {
			nbox = this.newsboxes[(this.curPos-1)];
			--this.curPos;
		}
		this.updatePositions();
		nbox.setStyle({left:(0-this.boxwidth)+'px'});
		new Effect.Move(cbox,{x: +(this.boxwidth),duration:1});
		new Effect.Move(nbox,{x: +(this.boxwidth),duration:1});
		this.acceptMove();
		this.run();
	},
	prev:function() {
		if(this.isMoving) {
			return false;	
		}
		this.isMoving=true;
		var cbox = this.newsboxes[this.curPos];
		var nbox;
		if(this.curPos == (this.newsboxes.length - 1)) {
			nbox = this.newsboxes[0];
			this.curPos = 0;
		}
		else {
			nbox = this.newsboxes[(this.curPos+1)];
			this.curPos++;
		}
		this.updatePositions();
		nbox.setStyle({'position':'absolute'});
		nbox.setStyle({left:this.boxwidth+'px'});
		new Effect.Move(cbox,{x: -(this.boxwidth),duration:1});
		new Effect.Move(nbox,{x: -(this.boxwidth),duration:1});
		this.acceptMove();
		this.run();
	},
	acceptMove:function() {
		setTimeout(function() {nsb.isMoving=false},1200);
	},
	updatePositions:function() {
		for(x=0;x<this.newsboxes.length;x++) {
			$('ns-'+x).removeClassName('newsscroll-current');
		}
		$('ns-'+(this.curPos)).addClassName('newsscroll-current');
	},
	run:function() {
		if(this.autostart) {
			setTimeout(function() {nsb.prev();},this.autoSpeed);
		}
	},
	mouseClick:function(dir) {
		this.autostart=false;
		if(dir == '-') {
			this.next();
		}
		else {
			this.prev();
		}
	}
}
var nsb = new NewsScroller();
Event.observe(window,'load',function() {
	nsb.autostart=true;
	nsb.setUp();
});
