﻿function textScroller(strMasterContainer, strDocumentContainer, strArrowContainer) {
	
	this.masterobj = strMasterContainer;
	this.scrollerobj = strDocumentContainer;
	this.arrowobj = strArrowContainer;
	this.scrollheight = 0;
	this.movespeed = 1;
	this.movespeedfast = 6;
	this.intervalspeed = 1.0;
	this.bottompadding = 50;
	
	this.ileftpos = 0;
	this.timer = null;
	this.tempmove = 0;
	this.scheight = 0;
	this.sbtop;
	this.dragit = 0;
	this.fullscroll = 0;

  this.scrollDirection = 0;
}	

textScroller.prototype = {
 
	prepareScroller: function() { 
				this.scrollheight = parseInt(document.getElementById(this.masterobj).offsetHeight);
				if(document.getElementById(this.scrollerobj).offsetHeight-this.scrollheight>0) { 
					this.fullscroll = (parseInt(document.getElementById(this.scrollerobj).offsetHeight-this.scrollheight)*-1);
				}
				
				if(this.fullscroll < 0 && this.arrowobj != '') {
					document.getElementById(this.arrowobj).style.visibility = "visible";
				}
		},


    doScroll: function(instance) {  
				return function() {
					//alert(instance.fullscroll);
					if(instance.fullscroll==0) { clearInterval(instance.timer); }
					var o = document.getElementById(instance.scrollerobj);
					instance.ileftpos += (instance.scrollDirection==0 ? - instance.tempmove : + instance.tempmove);
					
					o.style.top = instance.ileftpos + "px";
					
					var progress = parseInt(100 - ((((instance.fullscroll-instance.bottompadding) - (parseInt(o.style.top))) / (instance.fullscroll-instance.bottompadding)) * 100));
					if(instance.scrollDirection==0) {
							if(parseInt(o.style.top)<parseInt(instance.fullscroll-instance.bottompadding)) {  
								clearInterval(instance.timer);
								o.style.top = parseInt(instance.fullscroll-instance.bottompadding) + "px";
							}
					}
					else {
							if(parseInt(o.style.top)>0) {
								clearInterval(instance.timer);
								o.style.top = 0 + "px";
							}
					}
			};
		},
		
		scrollUp: function() {	
				this.tempmove = this.movespeed;
				if(parseInt(document.getElementById(this.scrollerobj).style.top) < 0) {
					this.scrollDirection = 1;
					this.timer = setInterval(this.doScroll(this), this.intervalspeed*10);
				}
		},
	
		scrollDown: function () { 
				var o = document.getElementById(this.scrollerobj);
				this.tempmove = this.movespeed;
				if(parseInt(o.style.top) > parseInt((o.offsetHeight*-1) + this.bottompadding)) {
					this.scrollDirection = 0;
					this.timer = setInterval(this.doScroll(this), this.intervalspeed*10);
				}	
		},
		
		scrollThenCancel: function (v) {
			if(v=="up") {
				this.scrollUp();
			}
			else {
				this.scrollDown();
			}
			setTimeout("function() { cancelScroll(); }", 750);
		},
		
		alterSpeed: function(faster) {
				this.tempmove = (faster==1 ? this.movespeedfast : this.movespeed);
		},
		
    cancelScroll: function() { 
        clearInterval(this.timer); 
    } 
}; 	


	function wheel(event) {
    var delta = 0;
    if (!event) 
            event = window.event;
    if (event.wheelDelta) { 
            delta = event.wheelDelta/120;
            if (window.opera)
                    delta = -delta;
    } else if (event.detail) { 
            delta = -event.detail/3;
    }
    if (delta)
            handle(delta);
    if (
    event.preventDefault)
    event.preventDefault();
		event.returnValue = false;
		}

function handle(delta) {
	var o = document.getElementById("divDocumentContainer1");
	if(delta < 0) {
			if(parseInt(o.style.top) > parseInt(o.offsetHeight*-1) + document.getElementById("divMasterContainer1").offsetHeight) {
				tempmove = delta * -10
				o.style.top = parseInt(o.style.top) - tempmove + "px";
				scroller1.ileftpos = parseInt(o.style.top) - tempmove;
			}
	}
	else {
		if(parseInt(document.getElementById("divDocumentContainer1").style.top) < 0) {
		 	tempmove = delta * -10
			o.style.top = parseInt(o.style.top) - tempmove + "px";
			scroller1.ileftpos = parseInt(o.style.top) - tempmove;
		} else {
			scroller1.ileftpos = 0;
			o.style.top = "0px";
		}
	}
}