// gives up and down scroll buttons to images, spans, ... named up_name, down_name, respectively.
// will keep the default scroll_box's style overflow if it encounters errors (so make overflow: auto;)

// usage: put this after the scrollbox div:  var div_scroll1 = new TextScroll('div_scroll1', 'scroll_box');
function TextScroll(id)
{
    this.id = id;
    this.scrollCursor = 0;
	this.scrollCursor2 = 0;
    this.speed = 10;
    this.timeoutID = 0;
    this.div_obj = null;

	{
        if (document.getElementById) {
		
            div_obj = document.getElementById(this.id);
			
            if (div_obj) {
                this.div_obj = div_obj;
                this.div_obj.style.overflow = 'hidden';
            }
           	
			var div_up_obj = $(this.id + '_scroll_up');
			var div_dn_obj = $(this.id + '_scroll_down');
			var div_left_obj = $(this.id + '_scroll_left');
			var div_fast_left_obj = $(this.id + '_scroll_fast_left');
			var div_right_obj = $(this.id + '_scroll_right');
			var div_fast_right_obj = $(this.id + '_scroll_fast_right');
			
			if (div_up_obj){   
				div_up_obj.addClassName('hover');
				div_up_obj.onmouseover = function() { eval(id + ".scrollUp();") };
				div_up_obj.onmouseout = function() { eval(id + ".stopScroll();") };
			}
			if (div_dn_obj) {
				div_dn_obj.addClassName('hover');
				div_dn_obj.onmouseover = function() { eval(id + ".scrollDown();") };
				div_dn_obj.onmouseout = function() { eval(id + ".stopScroll();") };
			}
			if (div_left_obj) {
				div_left_obj.addClassName('hover');
				div_left_obj.onmouseover = function() { eval(id + ".scrollLeft();") };
				div_left_obj.onmouseout = function() { eval(id + ".stopScroll();") };
			}
			if (div_fast_left_obj) {
				div_fast_left_obj.addClassName('hover');
				div_fast_left_obj.onclick = function() { eval(id + ".left();") };
			}
			if (div_fast_right_obj) {
				div_fast_right_obj.addClassName('hover');
				div_fast_right_obj.onclick = function() { eval(id + ".right();") };
			}
			if (div_right_obj) {
				div_right_obj.addClassName('hover');
				div_right_obj.onmouseover = function() { eval(id + ".scrollRight();") };
				div_right_obj.onmouseout = function() { eval(id + ".stopScroll();") };
            }
        }
    }

	this.stopScroll = function() {
        clearTimeout(this.timeoutID);
    }

	this.scrollUp = function() {
        if (this.div_obj) {
            this.scrollCursor = (this.scrollCursor - this.speed) < 0 ? 0 : this.scrollCursor - this.speed;
            this.div_obj.scrollTop = this.scrollCursor;
            this.timeoutID = setTimeout(this.id + ".scrollUp()", 60);
        }
    }

	this.scrollLeft = function() {
		
		if (this.div_obj) {
			
			this.scrollCursor2 += this.speed;
            this.div_obj.scrollLeft = this.scrollCursor2;
            if (this.div_obj.scrollLeft == this.scrollCursor2) {
            	this.timeoutID = setTimeout(this.id + ".scrollLeft()", 60);
            }
            else {	this.scrollCursor2 = this.div_obj.scrollLeft; }
            
        }
    }
	
	this.left = function() {
		
		if (this.div_obj) {
			if(this.div_obj.scrollLeft + 369 >= this.div_obj.width){
				this.div_obj.scrollLeft = this.scrollCursor2 + (this.div_obj.width - 369);
			} else {
				this.div_obj.scrollLeft = this.scrollCursor2 + 369;
			}
			this.scrollCursor2 = this.div_obj.scrollLeft; 
        }
	}
	
	
	this.scrollRight = function() {
		
        if (this.div_obj) {
            
			this.scrollCursor2 = (this.scrollCursor2 - this.speed) < 0 ? 0 : this.scrollCursor2 - this.speed;
            this.div_obj.scrollLeft = this.scrollCursor2;
            this.timeoutID = setTimeout(this.id + ".scrollRight()", 60);
			
        }
    }	
	
	this.right = function() {
		
		if (this.div_obj) {
			if(this.div_obj.scrollLeft - 369 <= 0){
				this.div_obj.scrollLeft =0;
			} else {
				this.div_obj.scrollLeft = this.scrollCursor2 - 369;
			}
			this.scrollCursor2 = this.div_obj.scrollLeft; 
        }
	}
	
	this.scrollDown = function() {
        if (this.div_obj) {
            this.scrollCursor += this.speed;
            this.div_obj.scrollTop = this.scrollCursor;
            if (this.div_obj.scrollTop == this.scrollCursor) {
            	this.timeoutID = setTimeout(this.id + ".scrollDown()", 60);
            }
            else {	this.scrollCursor = this.div_obj.scrollTop; }
        }
    }
	
	this.resetScroll = function() {
        if (this.div_obj) {
            this.div_obj.scrollTop = 0;
            this.scrollCursor = 0;
        }
    }
	
	this.scrollToDiv = function(block) {
		
		 //alert("height = " + block.offsetHeight + "\nwidth = " + block.offsetWidth + "\nleft = " + block.offsetLeft + "\ntop = " + block.offsetTop + "\nscrollTop = " + this.div_obj.scrollTop + "\nscrollerHeight = " + this.div_obj.offsetHeight);
		
		 var newScrollTop = block.offsetTop - ((this.div_obj.offsetHeight / 2) - ( block.offsetHeight / 2))
		 //alert(newScrollTop);
		 
		 if(this.div_obj) {
            
			if(newScrollTop > 0){
				if(newScrollTop > this.div_obj.scrollTop){
					//alert('up');
					//this.scrollUp();
					//while(newScrollTop > this.div_obj.scrollTop) setTimeout('', 60);
					//this.stopScroll();
				}
				
			}
			//alert('hh');
			//while(newScrollTop < this.div_obj.scrollTop){
				//alert('hh');
				//this.scrollUp();
			//}
			//this.stopScroll();
        }
	}
}


