////////////////////////////
//
//  javascript scroll
//
//  elCSiga
//
////////////////////////////


function Scroller(targetdiv_id, parentdiv_id, upbutton_id, downbutton_id, scrollercookie) {
    
    ////// animation settings //////

 	var this_scroller = this;

	this.timeOut = 40;           // in milliseconds
	this.delta_onmouseover = 1;  // mouseover - in pixels
	this.delta_onpress = 5;      // onpress -   in pixels 
	this.continuous = true;      // 
	
	
	if (scrollercookie == "")
    this.current_top = 0;
  else
	  this.current_top = parseInt(readCookie("scroller_"+targetdiv_id+"_topPosition",0));

	this.current_speed = 0;
	
	
	
	this.targetdiv_id = targetdiv_id;
	this.targetdiv = document.getElementById(targetdiv_id);
    this.parentdiv = document.getElementById(parentdiv_id);
	this.upbutton = document.getElementById(upbutton_id);
	this.downbutton = document.getElementById(downbutton_id);
  
  ///////////////////////////////////////////////////////

	this.setSpeed = function(speed){
		if (this_scroller.current_speed == 0) {
			this_scroller.current_speed = speed;
			this_scroller.doScroll();
		}
		else 
			this_scroller.current_speed = speed;
		
	}
	this.stop = function(){
		this_scroller.current_speed = 0;
	}	
	this.reset = function(speed){
		this_scroller.current_speed = 0;
		this_scroller.current_top = 0;
	}
	
    ///////////////////////////////////////////////////////

	this.doScroll = function(){
	
		if (this_scroller.current_speed != 0) {
			
			this_scroller.current_top += this_scroller.current_speed;
			if (this_scroller.current_top >= 0) {
				this_scroller.current_top = 0;
				this_scroller.disableButton(this_scroller.upbutton);
			} else
				this_scroller.enableButton(this_scroller.upbutton,1);
							
			bottom = - (this_scroller.targetdiv.offsetHeight-this_scroller.parentdiv.offsetHeight);
			
			if (this_scroller.current_top <= bottom) {
				this_scroller.current_top = bottom;
				this_scroller.disableButton(this_scroller.downbutton);
			} else
				this_scroller.enableButton(this_scroller.downbutton,-1);				
							
			this_scroller.targetdiv.style.top = this_scroller.current_top+"px";
      if (scrollercookie != "")
        createCookie("scroller_"+this_scroller.targetdiv_id+"_topPosition",this_scroller.current_top);
				
			if (this_scroller.continuous)
				setTimeout( function(){this_scroller.doScroll();}, this_scroller.timeOut);
			else
				this_scroller.current_speed = 0;
		}
	}
	
    ///////////////////////////////////////////////////////

    this.enableButton = function(b,direction){
		b.style.backgroundColor = "#ffffff";
		b.style.cursor = "pointer";
		
		b.onmouseover = function () {
			this_scroller.setSpeed(+direction*this_scroller.delta_onmouseover);
		}
		b.onmouseout = function ()  {
			this_scroller.stop();
		}
		b.onmousedown = function () {
			this_scroller.setSpeed(+direction*this_scroller.delta_onpress);
		}
		b.onmouseup = function ()   {
			if (this_scroller.current_speed != 0)
				this_scroller.setSpeed(+direction*this_scroller.delta_onmouseover);
		}
    }
    this.disableButton = function(b){
		b.style.backgroundColor = "#646464";
		b.style.cursor = "default";

		
		b.onmouseover = "";
		b.onmouseout = "";
		b.onmousedown = "";
		b.onmouseup = "";
    }

    ///////////////////////////////////////////////////////
	
	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.checkSize = function(){
	
			if (this_scroller.current_top >= 0) {
				this_scroller.current_top = 0;
				this_scroller.disableButton(this_scroller.upbutton);
			} else
				this_scroller.enableButton(this_scroller.upbutton,1);
							
			bottom = - (this_scroller.targetdiv.offsetHeight-this_scroller.parentdiv.offsetHeight);
			
			if (this_scroller.current_top <= bottom) {
				this_scroller.current_top = bottom;
				this_scroller.disableButton(this_scroller.downbutton);
			} else
				this_scroller.enableButton(this_scroller.downbutton,-1);				
	
	
      this_scroller.targetdiv.style.top = this_scroller.current_top+"px";
	
	
	
	
	
	
		/*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();
	
	//////////////////////////////////////////
	

}

//cookies

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name, defaultvalue) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return defaultvalue;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
