/*
	CSME PRO CustomScroller Class
*/

var CustomScroller = function( content , contentDir , scroller , bar , scrollerDir ) {
	//
	this.min = 0;
	this.max = 100;
	this.AutoBarUpdate = true;
	this.onupdate = null;
	this.autoHide = true;
	this.lock = false;
	this.hide = null;
	this.expend = false;
	//
	this.minExpendPre = 0.05;
	this.stopUpdate = false;
	this.obj = CustomScroller;
	this.content = content;
	this.contentDir = contentDir;
	this.scroller = scroller;
	this.bar = bar;
	this.scrollerDir = scrollerDir;
	var self = this; 
	this.barMove = false;
	this.update = true;
	this.scrollrblock = false;
	this.rtl = false;
	this.btn = false;
	this.btnDirection = "";
	this.btnSpeed = 0;		
	//
	this.SaveDoc_onmousemove = null;
	this.SaveDoc_onmouseup = null;
	this.SaveDoc_onselectstart = null;
	//
	
	// -------------------------------------------------------------------------------------------------------------------
	
	this.obj.prototype.expendBar = function() {
		if ( this.expend == true && this.content ) {
			var pre = 1;
			//this.minExpendPre
			switch(this.contentDir) {
				case "h":
					if ( this.content.scrollHeight > this.content.clientHeight ) {
						pre = this.content.clientHeight / this.content.scrollHeight ;
						if ( pre < this.minExpendPre ) pre = this.minExpendPre;
						this.bar.style.height = Math.round(this.scroller.clientHeight * pre) + "px";
						}
					break;
				case "w":
					if ( this.content.scrollWidth > this.content.clientWidth ) {
						pre = this.content.clientWidth / this.content.scrollWidth ;
						if ( pre < this.minExpendPre ) pre = this.minExpendPre;
						this.bar.style.width = Math.round(this.scroller.clientWidth * pre) + "px";
						}
					break;
				}
			}
		}
		
	// -------------------------------------------------------------------------------------------------------------------
	
	this.obj.prototype.scrollerClickd = function(m) {	
		switch(this.scrollerDir) {
			case "h":
				newTop = parseInt(this.bar.style.top);
				if ( this.GetOffsetY(this.scroller,m) > newTop ) {
					newTop += this.bar.clientHeight;
					if ( newTop > this.scroller.clientHeight - this.bar.clientHeight ) newTop = this.scroller.clientHeight - this.bar.clientHeight;
					} else {
					newTop -= this.bar.clientHeight;
					if ( newTop < 0 ) newTop = 0;
					}
				this.bar.style.top = newTop + "px";				
				break;
			case "w":
				newLeft = parseInt(this.bar.style.left);
				if ( this.GetOffsetX(this.scroller,m) > newLeft ) {
					newLeft += this.bar.clientWidth;
					if ( newLeft > this.scroller.clientWidth - this.bar.clientWidth ) newLeft = this.scroller.clientWidth - this.bar.clientWidth;
					} else {
					newLeft -= this.bar.clientWidth;
					if ( newLeft < 0 ) newLeft = 0;
					}
				this.bar.style.left = newLeft + "px";
				break;
			}	
			this.SetContentPos();
		}
	
	// -------------------------------------------------------------------------------------------------------------------
	
	this.scroller.onclick = function(e) {
		if (self.lock == true) return false;	
		if( window.attachEvent ){ e = event; } // For IE only
		if ( self.scrollrblock == false ) {
			self.update = false;
			self.scrollerClickd(e);
			self.update = true;
			} else {
			self.scrollrblock = false;
			}
		};

	// -------------------------------------------------------------------------------------------------------------------	
	
	this.obj.prototype.SaveDocFunc = function() {	
		this.SaveDoc_onselectstart = document.onselectstart;
		this.SaveDoc_onmouseup = document.onmouseup;
		this.SaveDoc_onmousemove = document.onmousemove;
		}
	
	this.obj.prototype.LoadDocFunc = function() {	
		document.onmousemove = this.SaveDoc_onmousemove;
		document.onmouseup = this.SaveDoc_onmouseup;
		document.onselectstart = this.SaveDoc_onselectstart;
		}
		
	// -------------------------------------------------------------------------------------------------------------------
	
	this.bar.onmousedown = function() {	
		if (self.lock == true) return false;
		
		self.barMove = true;
		self.update = false;
		self.scrollrblock = true;	
		self.SaveDocFunc();
		
		document.onselectstart = function() {
			return false;
			}
		
		document.onmouseup = function() {
			self.LoadDocFunc();
			self.barMove = false;
			self.update = true;
			self.scrollrblock = true;				
			};
			
		document.onmousemove = function(e) {
			if ( self.barMove == true ) {
				if( window.attachEvent ){ e = event; } // For IE only
				switch(self.scrollerDir) {
					case "h":
						var newTop = self.GetOffsetY(self.scroller,e) - parseInt(self.bar.clientHeight/2);
						if ( newTop > self.scroller.clientHeight - self.bar.clientHeight ) newTop = self.scroller.clientHeight - self.bar.clientHeight;
						if ( newTop < 0 ) newTop = 0;
						self.bar.style.top = newTop + "px";
						break;
					case "w":	
						var newLeft = self.GetOffsetX(self.scroller,e) - parseInt(self.bar.clientWidth/2);
						if ( newLeft > self.scroller.clientWidth - self.bar.clientWidth ) newLeft = self.scroller.clientWidth - self.bar.clientWidth;
						if ( newLeft < 0 ) newLeft = 0;
						self.bar.style.left = newLeft + "px";
						break;
					}	
				}
				self.SetContentPos();
			};			
			
		};
	
	// -------------------------------------------------------------------------------------------------------------------
	
	this.obj.prototype.SetContentPos = function() {		
		if ( this.content != null ) {
			var pre = this.GetPercents();
			//
			switch(this.contentDir) {
				case "h":
					pos = Math.round( pre * ( this.content.scrollHeight - this.content.clientHeight) );
					this.content.scrollTop = pos;
					break;
				case "w":
					pos = Math.round( pre * ( this.content.scrollWidth - this.content.clientWidth) );
					this.content.scrollLeft = pos;			
					break;
				}
			}	
			if ( this.onupdate && this.stopUpdate == false ) this.onupdate();
		}
		
	// -------------------------------------------------------------------------------------------------------------------
	
	this.obj.prototype.SetBarPos = function() {	
		if ( this.update == true && this.content != null && this.AutoBarUpdate == true ) {
			var pre = 0;	
			if (this.autoHide == true ) {
				if ( this.bar.style.display == "none" ) this.bar.style.display = "block";
				switch(this.contentDir) {
					case "h":
						if ( this.content.scrollHeight > this.content.clientHeight && this.scroller.style.display == "none" ) {
							this.scroller.style.display = "block";
							} else if ( this.content.scrollHeight <= this.content.clientHeight && this.scroller.style.display == "block" ) {
							this.scroller.style.display = "none";
							}
						break;
					case "w":
						if ( this.content.scrollWidth > this.content.clientWidth && this.scroller.style.display == "none" ) {
							this.scroller.style.display = "block";
							} else if ( this.content.scrollWidth <= this.content.clientWidth && this.scroller.style.display == "block" ) {
							this.scroller.style.display = "none";
							}				
						break;
					}
				if ( this.hide ) {
					if ( this.hide.style.display != this.scroller.style.display ) {
						this.hide.style.display = this.scroller.style.display;	
						}
					}
				} else {
				if ( this.scroller.style.display == "none" ) this.scroller.style.display = "block";
				switch(this.contentDir) {
					case "h":
						if ( this.content.scrollHeight > this.content.clientHeight && this.bar.style.display == "none" ) {
							this.bar.style.display = "block";
							} else if ( this.content.scrollHeight <= this.content.clientHeight && this.bar.style.display == "block" ) {
							this.bar.style.display = "none";
							}
						break;
					case "w":
						if ( this.content.scrollWidth > this.content.clientWidth && this.bar.style.display == "none" ) {
							this.bar.style.display = "block";
							} else if ( this.content.scrollWidth <= this.content.clientWidth && this.bar.style.display == "block" ) {
							this.bar.style.display = "none";
							}				
						break;
					}					
				}	
			//
			if ( this.scroller.style.display == "block" ) {	
				this.expendBar();
				switch(this.contentDir) {
					case "h":
						tmp = (this.content.scrollHeight - this.content.clientHeight);
						pre = ( tmp == 0 ? 0 : this.content.scrollTop / tmp );
						break;
					case "w":
						tmp = (this.content.scrollWidth - this.content.clientWidth);
						pre = ( tmp == 0 ? 0 : (this.content.scrollLeft ) / tmp );	
					//	if (this.rtl == true ) pre = 1 - pre;
						break;
					}
				//
				switch(this.scrollerDir) {
					case "h":
						pos = Math.round( pre * ( this.scroller.clientHeight - this.bar.clientHeight) );
						this.bar.style.top = pos+"px";						
						break;
					case "w":
						pos = Math.round( pre * ( this.scroller.clientWidth - this.bar.clientWidth) );
						this.bar.style.left = pos+"px";
						break;
					}						
				//	
				}			
			//
			if ( this.onupdate && this.stopUpdate == false ) this.onupdate();	
			} else if ( this.barMove == true && this.btn == true && this.btnDirection !="" && this.btnSpeed != 0 ) {
			//
			switch(this.btnDirection.toLowerCase() ) {
				case "h":
					speed2 = this.btnSpeed;
					var newTop = parseInt(this.bar.style.top) + speed2;
					if ( newTop < 0 ) newTop = 0;
					if ( newTop > this.scroller.clientHeight - this.bar.clientHeight ) newTop = this.scroller.clientHeight - this.bar.clientHeight;
					this.bar.style.top = newTop + "px";
					break;
				case "w":
					speed2 = ( this.rtl == true ? this.btnSpeed * -1 : this.btnSpeed );
					var newLeft = parseInt(this.bar.style.left) + speed2;
					if ( newLeft < 0 ) newLeft = 0;
					if ( newLeft > this.scroller.clientWidth - this.bar.clientWidth ) newLeft = this.scroller.clientWidth - this.bar.clientWidth;
					this.bar.style.left = newLeft + "px";
					break;
				}	
			this.SetContentPos();			
			//
			if ( this.onupdate && this.stopUpdate == false ) this.onupdate();
			}
		var self = this; 
		setTimeout(function(){ self.SetBarPos(); }, 10);		
		}	

	// -------------------------------------------------------------------------------------------------------------------	
		
	this.obj.prototype.GetRealLeft = function(el) {			
		xPos = el.offsetLeft;
		tempEl = el.offsetParent;
		while (tempEl != null) {
			xPos += tempEl.offsetLeft;
			tempEl = tempEl.offsetParent;
			}
			return xPos;
		}
		
	this.obj.prototype.GetRealTop = function(el) {	
		yPos = el.offsetTop;
		tempEl = el.offsetParent;
		while (tempEl != null) {
			yPos += tempEl.offsetTop;
			tempEl = tempEl.offsetParent;
	    		}
	    	return yPos;
		}

	this.obj.prototype.GetPageX = function(e,el) {		
		if (typeof( e.clientX ) == 'number') {
			var coord = e.clientX;
			if (el.scrollLeft) coord += el.scrollLeft;
			tempEl = el.parentNode;
			while (tempEl != null) {
				if (tempEl.scrollLeft) coord += tempEl.scrollLeft;
				tempEl = tempEl.parentNode;
	    		}	
			return coord;
			} else {
			return 0;
			}
		}		
		
	this.obj.prototype.GetPageY = function(e,el) {	
		if (typeof( e.clientY ) == 'number') {
			var coord = e.clientY;
			if (el.scrollTop) coord += el.scrollTop;
			tempEl = el.parentNode;
			while (tempEl != null) {
				if (tempEl.scrollTop) coord += tempEl.scrollTop;
				tempEl = tempEl.parentNode;
	    		}	
			return coord;
			} else {
			return 0;
			}
		}
		
	this.obj.prototype.GetOffsetX = function(el,e) {
		return this.GetPageX(e,el) - this.GetRealLeft(el);
		}	
			
	this.obj.prototype.GetOffsetY = function(el,e) {
		return this.GetPageY(e,el) - this.GetRealTop(el);
		}
	
	// -------------------------------------------------------------------------------------------------------------------	
		
	this.obj.prototype.GetPercents = function() {
		var pre = 0;
		switch(this.scrollerDir) {
			case "h":
				tmp = (this.scroller.clientHeight - this.bar.clientHeight);
				pre = ( tmp == 0 ? 0 : parseInt(this.bar.style.top) / tmp );				
				break;
			case "w":
				tmp = (this.scroller.clientWidth - this.bar.clientWidth);
				pre = ( tmp == 0 ? 0 : parseInt(this.bar.style.left) / tmp );
				break;
			}			
		return pre;
		}		
	
	this.obj.prototype.SetPercents = function(pre) {
		this.update = false;
		switch(this.scrollerDir) {
			case "h":
				pos = Math.round( pre * ( this.scroller.clientHeight - this.bar.clientHeight) );
				this.bar.style.top = pos+"px";						
				break;
			case "w":
				pos = Math.round( pre * ( this.scroller.clientWidth - this.bar.clientWidth) );
				this.bar.style.left = pos+"px";
				break;
			}	
		this.SetContentPos();
		this.update = true;		
		}
	
	this.obj.prototype.GetValue = function() {
		var pre = this.GetPercents();
		var val = ((this.max - this.min) * pre) + this.min;
		return val;
		}	
	
	this.obj.prototype.SetValue = function(val) {
		if (val > this.max ) val = this.max;
		if (val < this.min ) val = this.min;
		tmp = this.max - this.min;
		var pre = ( tmp == 0 ? 0 : (val - this.min) / tmp );
		this.SetPercents(pre);
		}
	
	// -------------------------------------------------------------------------------------------------------------------
	
	this.obj.prototype.mode = function(val) {
		if ( val.toLowerCase() == "rtl" ) {
			this.rtl = true;
			} else {
			this.rtl = false;
			}
		if ( this.content ) {
			if ( this.rtl == true ) {
				this.content.scrollLeft = this.content.scrollWidth - this.content.clientWidth - 1;
				} else {
				this.content.scrollLeft = 0;
				}
			}
		}
	
	// -------------------------------------------------------------------------------------------------------------------

	this.obj.prototype.button = function( btn , direction , speed ) {
		
		btn.onmousedown = function () {
			if (self.lock == true) return false;
			
			self.barMove = true;
			self.update = false;
			self.scrollrblock = true;	
			self.SaveDocFunc();

			self.btn = true;
			self.btnDirection = direction;
			self.btnSpeed = speed;
			
			document.onselectstart = function() {
				return false;
				}
			
			document.onmouseup = function() {
				self.btn = false;
				self.btnDirection = "";
				self.btnSpeed = 0;			
				self.LoadDocFunc();
				self.barMove = false;
				self.update = true;
				self.scrollrblock = false;				
				};

			btn.onmouseout = function() {
				self.btn = false;
				self.btnDirection = "";
				self.btnSpeed = 0;			
				self.LoadDocFunc();
				self.barMove = false;
				self.update = true;
				self.scrollrblock = false;				
				};				
				
			}
		
		}	
	
	// -------------------------------------------------------------------------------------------------------------------	
	
	this.scroller.style.position = "relative";
	if (!this.scroller.style.left) this.scroller.style.left = "0px";	
	if (!this.scroller.style.top) this.scroller.style.top = "0px";
	if (!this.scroller.style.cursor) this.scroller.style.cursor = "arraow";
	// if (!this.scroller.style.overflow) this.scroller.style.overflow = "hidden";	
	this.scroller.style.display = "block"
	
	this.bar.style.position = "absolute";	
	if (!this.bar.style.left) this.bar.style.left = "0px";	
	if (!this.bar.style.top) this.bar.style.top = "0px";	
	if (!this.bar.style.cursor) this.bar.style.cursor = "arraow";
	this.bar.style.display = "block";
		
	this.SetBarPos();

};
