var ProtoScroll = Class.create({ 
	initialize: function(elem1, elem2, margin) {
		this.elem1=$(elem1);
		this.elem2=$(elem2);
		this.margin=margin;
		
		if ((!this.elem1) || (!this.elem2)) {
			return;
		}
		
		this.elem3=$(this.elem2.firstChild);
		
		if (this.elem3.getHeight()>this.elem2.getHeight()-10) {
			this.elem3.style.position="relative";
			this.scrolling=false;
			this.scrollDir=0;
			this.placeButtons();
			this.scroll=0;
			this.maxScroll=this.elem3.getHeight()-this.elem2.getHeight()+40;
			this.escolta();
		}
	},
	
	escolta: function() {
		if (this.scrolling) {
			if (this.scrollDir==0) {
				this.scroll=this.scroll-1;
				
				if (this.scroll<0) {
					this.scroll=0;
					this.scrolling=false;
				}
			} else {
				this.scroll=this.scroll+1;
				
				if (this.scroll>this.maxScroll) {
					this.scroll=this.maxScroll;
					this.scrolling=false;
				}
			}
			
			//this.elem3.setStyle("margin-top: -"+this.scroll+"px;");
			//this.elem3.setAttribute("style","margin-top: -"+this.scroll+"px");
			this.elem3.style.marginTop="-"+this.scroll+"px";
			
			new PeriodicalExecuter(function(pe) {
				    pe.stop();
				    this.escolta();
			}.bind(this), 0.01);

			
		}
	},
	
	placeButtons: function() {
		var dContButtons = new Element('div', { 'style':'background-color: #000000; filter:alpha(opacity=75); opacity: 0.75; margin: 5px 0px 0px '+this.margin+'px; text-align: right; padding: 5px;' });
		this.elem1.appendChild(dContButtons);
		var imgUp=new Element('img', {'src':'../img/upBig.png', 'style':'cursor: pointer;' });
		var imgDown=new Element('img', {'src':'../img/downBig.png', 'style':'cursor: pointer;' });
		
		dContButtons.appendChild(imgDown);
		dContButtons.appendChild(imgUp);
		
		imgUp.onmousedown=function() {
			this.scrolling=true;
			this.scrollDir=0;
			this.escolta();
		}.bind(this);
		
		imgUp.onmouseup=function() {
			this.scrolling=false;
		}.bind(this);
		
		imgDown.onmousedown=function() {
			this.scrolling=true;
			this.scrollDir=1;
			this.escolta();
		}.bind(this);
		
		imgDown.onmouseup=function() {
			this.scrolling=false;
		}.bind(this);
	}
});
