////////////////////////////
//
//  mrx-nek speckó scroll
//
//  elCSiga
//
////////////////////////////


function Scroller2(targetdiv_id, parentdiv_id, upbutton_id, downbutton_id) {
    
    ////// animation settings //////

	this.timeOut = 40;       
	this.delta = 313;      
	
    ////// constructor //////

 	var this_scroller = this;

    this.targetdiv = document.getElementById(targetdiv_id);
    this.parentdiv = document.getElementById(parentdiv_id);
	this.upbutton = document.getElementById(upbutton_id);
	this.downbutton = document.getElementById(downbutton_id);
	
	if (this.targetdiv  == null) return;
	if (this.parentdiv  == null) return;
	if (this.upbutton   == null) return;
	if (this.downbutton == null) return;
	
	this.targetdiv.style.position = "absolute";
	this.parentdiv.style.overflow = "hidden";
	this.targetdiv.style.top = "0px";
	this.targetdiv.style.height = "auto";
	
	this.current_top = 0;
	this.targetpos = 0;  
	
		
	this.doScroll = function(){
	
    
	    if (-this_scroller.targetpos > 0) 
			this_scroller.enableButton(this_scroller.upbutton,1);
		else
			this_scroller.disableButton(this_scroller.upbutton);
			
	    if (-this_scroller.targetpos < this_scroller.targetdiv.offsetHeight-this_scroller.delta) 
			this_scroller.enableButton(this_scroller.downbutton,-1);				
		else
			this_scroller.disableButton(this_scroller.downbutton);				
		
	
	    d = this_scroller.targetpos - this_scroller.current_top;
	    if (Math.abs(d) < 3) {
			this_scroller.current_top = this_scroller.targetpos;
			this_scroller.targetdiv.style.top = this_scroller.current_top+"px";
		}
		else {
		    if (d<0)
				this_scroller.current_top += Math.floor(d/4);
			else
				this_scroller.current_top += Math.ceil(d/4);
				
			this_scroller.targetdiv.style.top = this_scroller.current_top+"px";
			setTimeout( function(){this_scroller.doScroll();}, this_scroller.timeOut);
		}
	}
	
	this.enableButton = function(b,direction){
		b.style.backgroundColor = "#ffffff";
		b.style.cursor = "pointer";
		
		b.onclick = function () {
			this_scroller.targetpos += this_scroller.delta * direction;
			this_scroller.doScroll();
		}

    }
    this.disableButton = function(b){
		b.style.backgroundColor = "#646464";
		b.style.cursor = "default";
		
		b.onclick = "";
    }
	
	
	this.checkSize = function(){
		if (this.targetdiv.offsetHeight <= this.parentdiv.offsetHeight) {
			this_scroller.disableButton(this_scroller.upbutton);
			this_scroller.disableButton(this_scroller.downbutton);
			return false;
		}
		else {
			this_scroller.disableButton(this_scroller.upbutton);
			this_scroller.enableButton(this_scroller.downbutton,-1);
			return true;
		}
  	    //alert(this.targetdiv.offsetHeight +" :: "+ this.parentdiv.offsetHeight);
	}
		
	this.checkSize();
	


	
	//////////////////////////////////////////
	

}
