var NewsSlider = new Class({
   Implements: Options,
	 options: {
        leftbutton:  'previous',
        rightbutton: 'next',
				itemclass:   'newsitem',
				perpage:     4,
				perclick:    1,
        width:       350
    },

		currentslide:1,
		pos:0,
		
		initialize: function(wrapper,options){
			this.setOptions(options);
			this.wrapper     = $(wrapper);
			this.leftbutton  = $(this.options.leftbutton);
			this.rightbutton = $(this.options.rightbutton);
			this.items       = this.wrapper.getElements('.' + this.options.itemclass );
			this.itemcount   = this.items.length;
			this.scroll      = new Fx.Scroll(this.wrapper, {offset:{'x':0, 'y':0} });
		  this.offset      = this.options.width;
			
			if (this.options.startrel) {
				for (var i=0; i < this.items.length; i++) {
						if (this.items[i].getAttribute('rel') == this.options.startrel) {
								this.starter = i;
								for (var i=1; i < (this.starter); i++) {  // i=1: scroll to middle news item
										if (this.currentslide <= (this.itemcount - this.options.perpage) ) {
												this.currentslide++;
												this.pos += this.offset;
										}
								}
								this.scroll.set(this.pos);
						}
				}
			}
			
			this.leftbutton.addEvent('click', this.domoveleft.bind(this));
			this.rightbutton.addEvent('click', this.domoveright.bind(this));
			
			this.setcss();
		},
		
		
		domoveleft: function(event){
				event = new Event(event).stop();
				for (var i=0; i < this.options.perclick; i++) {
						if(this.currentslide > 1) {
								this.currentslide--;	
								this.pos += -(this.offset);
						}
				}
				this.scroll.start(this.pos);
				this.setcss();
				// this.scroll.toLeft();
		},
		domoveright: function(event){
				event = new Event(event).stop();
				for (var i=0; i < this.options.perclick; i++) {
						if (this.currentslide <= (this.itemcount - this.options.perpage) ) {
								this.currentslide++;
								this.pos += this.offset;
						}
				}
				this.scroll.start(this.pos);
				this.setcss();
			//	this.scroll.toLeft();
		},
		
		setcss: function() {
				if (this.currentslide <= 1) {
						this.leftbutton.setAttribute("class","nobutton");
				} else {
						this.leftbutton.setAttribute("class","prevbutton");
				}
				
				
				if (this.currentslide > (this.itemcount - this.options.perpage)) {
						this.rightbutton.setAttribute("class","nobutton");
				} else {
						this.rightbutton.setAttribute("class","nextbutton");
				}				
		}
		
});

NewsSlider.implement(new Events);
NewsSlider.implement(new Options);
	