NewsWP = Class.create();
Object.extend(NewsWP.prototype, 
{
	initialize: function(el) {
		this.container = $(el);
		if (this.container == undefined) return 0;
		
		
		this.elements = this.container.select("div.newsitem");
		this.scrollable = this.container.down("div.newscontainer");
		this.leftscroller =  this.container.down("div.newsarrleft");
		this.rightscroller =  this.container.down("div.newsarrright");
		
		this.scrollable.setStyle({width:360*this.elements.length+"px"});
		
		this.currentWidth = 0;
		this.elements.each((function(fli) {
			this.currentWidth += 360;
		}).bind(this));
		
		this.currentscrollvalue = 0;
		
		/*if(this.elements.length%2 != 0){
			this.currentWidth += 180;
		}*/
		

		this.checkscrollactivation = function() {
			if (this.currentscrollvalue < 0) {
				this.activateArrowLeft ();
			} else {
				this.autoscrolldir = 1;
				Event.stopObserving(this.leftscroller, 'click', this.boundstartleft);
				Event.stopObserving(this.leftscroller, 'mouseupoutside', this.boundstopleft);
				this.leftscroller.removeClassName("newsarrlefton");
			}
			
			if (Math.abs(this.currentscrollvalue) + 360 < this.currentWidth) {
				this.activateArrowRigth();
			} else {
				this.autoscrolldir = -1;
				Event.stopObserving(this.rightscroller, 'click', this.boundstartright);
				Event.stopObserving(this.rightscroller, 'mouseupoutside', this.boundstopright)
				this.rightscroller.removeClassName("newsarrrighton");
			}
		}
		
		this.activateArrowLeft = function() {
			this.leftscroller.observe("click", this.boundstartleft, false);
			this.leftscroller.observe("mouseupoutside", this.boundstopleft, false);
			this.leftscroller.addClassName("newsarrlefton");
		}
		
		this.activateArrowRigth = function() {
			this.rightscroller.observe("click", this.boundstartright, false);
			this.rightscroller.observe("mouseupoutside", this.boundstopright, false);
			this.rightscroller.addClassName("newsarrrighton");
		}
		
		this.autoscroll = function() {
			if (this.autoscrolldir > 0) {
				this.scrollright();
			} else {
				this.scrollleft();
			}
			this.checkscrollactivation();
		}
		
		this.autoscrolldir = 1;
		this.intervalautoscroll = setInterval(this.autoscroll.bind(this), 7000);
		
		this.startrightscroll = function() {
			if (this.intervalrightscroll != undefined) return;
			clearInterval(this.intervalautoscroll);
			this.activateArrowLeft();
			this.scrollright();
		}
		
		this.stoprightscroll = function() {
			clearInterval(this.intervalrightscroll);
			this.intervalrightscroll = undefined;
			this.checkscrollactivation();
		}
		
		this.scrollright = function() {
			if (this.scrolling) return;
			
			this.currentscrollvalue -= 360;
			if (Math.abs(this.currentscrollvalue) + 360 > this.currentWidth) {				
				this.currentscrollvalue = 180 - 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)});
				if (Math.abs(this.currentscrollvalue) + 360 >= this.currentWidth) {
					this.currentscrollvalue = 360 - this.currentWidth;
					this.stoprightscroll();
				}
			}
		}
				
		this.startleftscroll = function() {
			if (this.intervalleftscroll != undefined) return;
			clearInterval(this.intervalautoscroll);
			this.checkscrollactivation();
			this.activateArrowRigth();
			this.scrollleft();
		}
		
		this.stopleftscroll = function() {
			clearInterval(this.intervalleftscroll);
			this.intervalleftscroll = undefined;
			this.checkscrollactivation();
		}
		
		this.scrollleft = function() {
			if (this.scrolling) return;			
			this.currentscrollvalue += 360;
			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)});
				if (this.currentscrollvalue >= 0) {
					this.currentscrollvalue = 0;
					this.stopleftscroll();
				}
			}
		}
		
		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();
		
	}
	
});