﻿var objActiveScroller;
var mouseX = 0;
var mouseY = 0;
var mouseY2 = 0;
	
function sourceCheck(e){
	getMousePos(e);
	var evt = e || window.event;
	if(objActiveScroller!=null) {
		objActiveScroller.moveSlider(objActiveScroller);
	}
}

function cancelDrag() {
	document.onselectstart = document.documentElement.onselectstart = new Function ( "return true;" )
	if(objActiveScroller!=null) {
		objActiveScroller.moving = false;
		objActiveScroller.blnIsDragging = false;
		document.onmousedown = function() { return true; }
	}
}

function getMousePos(e) {
	var el=(typeof event!=='undefined')? event.srcElement : e.target
  var posx = 0; var posy = 0; 
  if (!e) var e = window.event; 
  if (e.pageX || e.pageY) { 
      posx = e.pageX; 
      posy = e.pageY; 
  } 
  else if (e.clientX || e.clientY) { 
      posx = e.clientX; 
      posy = e.clientY; 
  } 
  mouseX = posx; mouseY = posy;
  mouseY2 = posy;
} 
	
function textScroller(strID) {
	
	this.masterobj = 'divMasterContainer' + strID;
	this.scrollerobj = 'divDocumentContainer' + strID;
	this.arrowobj = 'divScrollArrows' + strID;
	this.scrollbar = "divScrollbar" + strID;
	this.scrollslider = "divScrollSlider" + strID;
	
	this.scrollheight = 0;
	this.movespeed = 1;
	this.movespeedfast = 6;
	this.intervalspeed = 1.0;
	this.bottompadding = 50;
	
	this.scrollbarheight = 0;
	this.scrollsliderheight = 0;
	this.scrollsliderminheight = 0;
	
	this.calcbarheight = 0;
	this.calcbartop = 0;
	this.calcbarbottom = 0;
	
	this.ileftpos = 0;
	this.timer = null;
	this.tempmove = 0;
	this.scheight = 0;
	this.sbtop;
	this.dragit = 0;
	this.fullscroll = 0;
	
	this.moving = false;
  this.scrollDirection = 0;
  this.documentOnMouseMove = null;
 	this.mouseX = 0;
 	this.mouseY = 0;
 	this.xDelta = 0;
 	this.yDelta = 0;
 	this.blnIsDragging = false;
 	
}	

textScroller.prototype = {
 
	prepare: 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);
				}
				this.bottompadding = 0;
				if(this.fullscroll < 0 && this.arrowobj != '') {
					document.getElementById(this.arrowobj).style.visibility = "visible";
					document.getElementById(this.scrollbar).style.visibility = "visible";
					document.getElementById(this.scrollslider).style.visibility = "visible";
				
					this.scrollbarheight = this.g(this.scrollbar).offsetHeight;
					this.scrollsliderminheight = parseInt(this.scrollbarheight * 0.1);
					this.scrollsliderheight = this.g(this.scrollerobj).offsetHeight / this.g(this.masterobj).offsetHeight;
					
					var percent = this.g(this.scrollerobj).offsetHeight / this.g(this.masterobj).offsetHeight;
					this.scrollsliderheight = this.p(this.g(this.scrollbar).offsetHeight / percent);
					
					if(this.scrollsliderheight < this.scrollsliderminheight) {
						this.scrollsliderheight = this.p(this.scrollsliderminheight);
					} else {
						this.scrollsliderheight = this.p(this.scrollsliderheight);
					}
					this.g(this.scrollslider).style.height = this.scrollsliderheight + "px";
				} else {
					this.g(this.scrollslider).style.visibility = "hidden";
				}
			
				this.g(this.scrollerobj).onmouseover = this.addWheelHandler(this);
				this.g(this.scrollerobj).onmouseout = this.removeWheelHandler(this);
				
				this.g(this.scrollslider).onmousedown = this.beginDrag(this);
				this.g(this.scrollslider).onmouseup = this.endDrag(this);
				this.calcbarheight = this.scrollbarheight - this.scrollsliderheight;
				this.calcbartop = this.p(rypos(this.scrollbar) + (this.scrollsliderheight / 2));
				this.calcbarbottom = this.p(rypos(this.scrollbar) + this.g(this.scrollbar).offsetHeight - (this.scrollsliderheight / 2));
				this.g(this.scrollslider).style.top = rypos(this.scrollbar) + "px";
				
				this.g(this.scrollslider).style.left = rxpos(this.scrollbar) + parseInt(this.g(this.scrollbar).offsetWidth / 2) - parseInt(this.g(this.scrollslider).offsetWidth / 2) + "px";
				
				objActiveScroller = this;
				
		},
		
		beginDrag: function(instance) {
			return function() {
				instance.mouseX = mouseX;
				instance.mouseY = mouseY;
				instance.yDelta = mouseY - rypos(instance.scrollslider);
				instance.blnIsDragging = true;
				objActiveScroller = instance;
				
				//document.onselectstart = document.documentElement.onselectstart = new Function ( "return false;" )
				document.onselectstart = function() { return false; }
				document.onmousedown = function() { return false; }
				
				if(window.captureEvents) {
					instance.moving = true;
				}
				else {
					instance.moving = true;
				}
			}
		},
		endDrag: function(instance) {
			return function() {
				//document.onselectstart = document.documentElement.onselectstart = new Function ( "return true;" )
				document.onselectstart = function() { return true; }
				document.onmousedown = function() { return true; }
				instance.blnIsDragging = false;
				
				if(window.captureEvents) {
					instance.moving = false;
				}
				else {
					instance.moving = false;
				}
			}
		},
		
		movehandler: function(instance) {
			return function() {
				getMousePos(e);
				instance.moveSlider(instance);
			}
		},
		
		mousepos: function(e,ee) {
			getMousePos(e);
		},
		
		moveSlider: function(instance) {
			if(!instance.moving) { return; }
			if(instance.moving) {
				instance.g(instance.scrollslider).style.top = mouseY - instance.yDelta + "px";
			}
			if(rypos(instance.scrollslider) < rypos(instance.scrollbar)) {
				instance.g(instance.scrollslider).style.top = rypos(instance.scrollbar) + "px";
				instance.g(instance.scrollerobj).style.top = "0px";
				instance.endDrag(instance);
			}
			
			if(rypos(instance.scrollslider) > (rypos(instance.scrollbar) + (instance.scrollbarheight - instance.scrollsliderheight))) {
				instance.g(instance.scrollslider).style.top = rypos(instance.scrollbar) + (instance.scrollbarheight - instance.scrollsliderheight) + "px";
				instance.g(instance.scrollerobj).style.top = (instance.g(instance.scrollerobj).offsetHeight - instance.g(instance.masterobj).offsetHeight) * -1 + "px";
				instance.endDrag(instance);
			}
			var intBottom = instance.calcbarbottom - instance.calcbartop;
			var intRelative = rypos(instance.scrollslider) + parseInt(instance.g(instance.scrollslider).offsetHeight / 2) - instance.calcbartop;
			
			var percent = (1 - ((intBottom - intRelative) / intBottom));
			instance.ileftpos = instance.p((instance.g(instance.scrollerobj).offsetHeight * percent * -1) + (instance.g(instance.masterobj).offsetHeight * percent));
			instance.g(instance.scrollerobj).style.top = instance.ileftpos + "px"; //instance.p((instance.g(instance.scrollerobj).offsetHeight * percent * -1) + (instance.g(instance.masterobj).offsetHeight * percent)) + "px";
		},
		
    doScroll: function(instance) {
				return function() {
					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";
					//alert("test");
					var progress = parseInt(100 - ((((instance.fullscroll-instance.bottompadding) - (parseInt(o.style.top))) / (instance.fullscroll-instance.bottompadding)) * 100));
					var sliderBarTop = ((instance.calcbarbottom - instance.calcbartop) * (progress/100)) + rypos(instance.scrollbar) + ((instance.g(instance.scrollslider).offsetHeight / 2) * 0);
					if ("" + sliderBarTop != "NaN")
					{
					instance.g(instance.scrollslider).style.top = sliderBarTop + "px";
					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.ileftpos = parseInt(document.getElementById(this.scrollerobj).style.top);
					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 ( isNaN( parseInt(o.style.top) ) ) o.style.top = 0 + "px";
				//document.title = "parseInt(o.style.top): " + parseInt(o.style.top) + ", parseInt((o.offsetHeight*-1) + this.bottompadding): " + parseInt((o.offsetHeight*-1) + this.bottompadding);
				if(parseInt(o.style.top) > parseInt((o.offsetHeight*-1) + this.bottompadding)) {
					this.ileftpos = parseInt(o.style.top);
					this.scrollDirection = 0;
					this.timer = setInterval(this.doScroll(this), this.intervalspeed*10);
					//document.title += "after";
				}	
		},
		
		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);
		},
		g: function(v) {
			return document.getElementById(v);
		},
		p: function(v) {
			return parseInt(v);
		},
    
    cancelScroll: function() { 
        clearInterval(this.timer); 
    },
    
    addWheelHandler: function(instance) {
    	return function() {
    		if(instance.blnIsDragging) {
    			return;
    		}
	    	if(window.addEventListener) {
					window.addEventListener('DOMMouseScroll', instance.wheelHandler, false);
				}
				window.onmousewheel = document.onmousewheel = instance.wheelHandler;
				objActiveScroller = instance;
	    }
  	},
  	
  	removeWheelHandler: function(instance) {
    	return function() {
	    	if(window.removeEventListener) {
					window.removeEventListener('DOMMouseScroll', instance.wheelHandler, false);
				}
				window.onmousewheel = document.onmousewheel = "";
	    }
  	},
  	
  	wheelHandler: function(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)
	            objActiveScroller.wheelHandlerMoveDelta(delta);
	    if (
	    event.preventDefault)
	    event.preventDefault();
			event.returnValue = false;
  	},
  	
  	wheelHandlerMoveDelta: function(delta) {
	

	//this.masterobj = 'divMasterContainer' + strID;
	//this.scrollerobj = 'divDocumentContainer' + strID;

				if ( g(this.scrollerobj).scrollHeight < g(this.masterobj).offsetHeight ) return;


				var o = document.getElementById(objActiveScroller.scrollerobj);
				if(delta < 0) {
						if(parseInt(o.style.top) > parseInt(o.offsetHeight*-1) + document.getElementById(objActiveScroller.masterobj).offsetHeight) {
							tempmove = delta * -10
							o.style.top = parseInt(o.style.top) - tempmove + "px";
							objActiveScroller.ileftpos = parseInt(o.style.top) - tempmove;
						} else {
							objActiveScroller.ileftpos = parseInt(o.offsetHeight*-1) + document.getElementById(objActiveScroller.masterobj).offsetHeight;
							o.style.top = parseInt(o.offsetHeight*-1) + document.getElementById(objActiveScroller.masterobj).offsetHeight + "px";
						}
				}
				else {
					if(parseInt(document.getElementById(objActiveScroller.scrollerobj).style.top) < 0) {
					 	tempmove = delta * -10
						o.style.top = parseInt(o.style.top) - tempmove + "px";
						objActiveScroller.ileftpos = parseInt(o.style.top) - tempmove;
					} else {
						objActiveScroller.ileftpos = 0;
						o.style.top = "0px";
					}
				}
				var progress = parseInt(100 - ((((objActiveScroller.fullscroll-objActiveScroller.bottompadding) - (parseInt(o.style.top))) / (objActiveScroller.fullscroll-objActiveScroller.bottompadding)) * 100));
				var sliderBarTop = ((objActiveScroller.calcbarbottom - objActiveScroller.calcbartop) * (progress/100)) + rypos(objActiveScroller.scrollbar) + ((objActiveScroller.g(objActiveScroller.scrollslider).offsetHeight / 2) * 0);
				objActiveScroller.g(objActiveScroller.scrollslider).style.top = sliderBarTop + "px";
  	}
}; 	