Slideshow_2WP = Class.create();
Object.extend(Slideshow_2WP.prototype, 
{
	initialize: function(el) {
		this.container = $(el);
		if (this.container == undefined) return 0;
		this.elements = this.container.select("div.slideshow_2_item");
		this.scrollable = this.container.down("div.slideshow_2_container");
		this.leftscroller =  this.container.down("div.slidearrleft");
		this.rightscroller =  this.container.down("div.slidearrright");
		
		this.scrollable.setStyle({width:150*this.elements.length+"px"});
		
		this.currentWidth = 0;
		this.elements.each((function(fli) {
			this.currentWidth += 150;
		}).bind(this));
		this.currentscrollvalue = 0;
		
		this.checkscrollactivation = function() {
			if (this.currentscrollvalue < 0 && this.currentWidth > 150) {
				this.leftscroller.observe("mouseover", this.boundstartleft, false);
				this.leftscroller.observe("mouseout", this.boundstopleft, false);
				this.leftscroller.addClassName("slidearrlefton");
			} else {
				Event.stopObserving(this.leftscroller, 'mouseover', this.boundstartleft);
				Event.stopObserving(this.leftscroller, 'mouseout', this.boundstopleft);
				this.leftscroller.removeClassName("slidearrlefton");
			}
			
			if (Math.abs(this.currentscrollvalue) + 150 < this.currentWidth && this.currentWidth > 150) {
				this.rightscroller.observe("mouseover", this.boundstartright, false);
				this.rightscroller.observe("mouseout", this.boundstopright, false);
				this.rightscroller.addClassName("slidearrrighton");
			} else {
				Event.stopObserving(this.rightscroller, 'mouseover', this.boundstartright);
				Event.stopObserving(this.rightscroller, 'mouseout', this.boundstopright)
				this.rightscroller.removeClassName("slidearrrighton");
			}
		}
		
		this.startrightscroll = function() {
			if (this.intervalrightscroll != undefined) return;
			this.scrollright();
			this.intervalrightscroll = setInterval(this.scrollright.bind(this), 1000)
		}
		
		this.stoprightscroll = function() {
			clearInterval(this.intervalrightscroll);
			this.intervalrightscroll = undefined;
			this.checkscrollactivation();
		}
		
		this.scrollright = function() {
			if (this.scrolling) return;
			
			this.currentscrollvalue -= 150;
			if (Math.abs(this.currentscrollvalue) + 150 > this.currentWidth) {
				this.currentscrollvalue = 150 - this.currentWidth;
				this.stoprightscroll();
			} else {
				this.scrolling = true;
				new Effect.Move(this.scrollable, {x:this.currentscrollvalue, y:0, duration: 0.5, mode: 'absolute', afterFinish:(function(){this.scrolling=false}).bind(this)});
			}
		}
				
		this.startleftscroll = function() {
			if (this.intervalleftscroll != undefined) return;
			this.scrollleft();
			this.intervalleftscroll = setInterval(this.scrollleft.bind(this), 1000)
		}
		
		this.stopleftscroll = function() {
			clearInterval(this.intervalleftscroll);
			this.intervalleftscroll = undefined;
			this.checkscrollactivation();
		}
		
		this.scrollleft = function() {
			if (this.scrolling) return;
			
			this.currentscrollvalue += 150;
			if (this.currentscrollvalue > 0) {
				this.currentscrollvalue = 0;
				this.stopleftscroll();
			} else {
				this.scrolling = true;
				new Effect.Move(this.scrollable, { x:this.currentscrollvalue, y:0, duration: 0.5, mode: 'absolute', afterFinish:(function(){this.scrolling=false}).bind(this)});
			}
		}
		
		this.boundstartright = this.startrightscroll.bind(this);
		this.boundstopright = this.stoprightscroll.bind(this);
		this.boundstartleft = this.startleftscroll.bind(this);
		this.boundstopleft = this.stopleftscroll.bind(this);
		
		this.checkscrollactivation();
		
	}
	
});