NewsShow = Class.create();

Object.extend(NewsShow.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.newscontrolleft");

		this.rightscroller =  this.container.down("div.newscontrolright");
                
		

	var mainwidth = document.getElementById("newsshow").clientWidth ;
	mainwidth -= 30;

		this.scrollable.setStyle({width:mainwidth*this.elements.length+"px"});

                
		


	if ( node == null )
	var node = document;
        var tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
        var maxheight = 0;
	var pattern = new RegExp('(^|\\\\s)newsitem(\\\\s|$)');
	//larghezza contenuto dinamica
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			els[i].style.width= mainwidth+"px";
			j++;
		}
	}
	//altezza dinamica
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
                        if (els[i].clientHeight>maxheight){
                            maxheight = els[i].clientHeight;
                        }
			j++;
		}
	}
        /*maxheight = 250;*/

        document.getElementById("mascheranews").style.height =  maxheight+"px";

		this.currentWidth = 0;

		this.elements.each((function(fli) {

			this.currentWidth += mainwidth;

		}).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("newscontrollefton");

			}

			

			if (Math.abs(this.currentscrollvalue) + mainwidth < 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("newscontrolrighton");

			}

		}

		

		this.activateArrowLeft = function() {

			this.leftscroller.observe("click", this.boundstartleft, false);

			this.leftscroller.observe("mouseupoutside", this.boundstopleft, false);

			this.leftscroller.addClassName("newscontrollefton");

		}

		

		this.activateArrowRigth = function() {

			this.rightscroller.observe("click", this.boundstartright, false);

			this.rightscroller.observe("mouseupoutside", this.boundstopright, false);

			this.rightscroller.addClassName("newscontrolrighton");

		}

		

		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 -= mainwidth;

			if (Math.abs(this.currentscrollvalue) + mainwidth > this.currentWidth) {				

				this.currentscrollvalue = mainwidth - 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) + mainwidth >= this.currentWidth) {

					this.currentscrollvalue = mainwidth - 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 += mainwidth;

			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();

		

	}

	

});

new NewsShow('newsshow');

