/*
Required:
	- scrollBox.elems.frame;  		  // The viewing window of the box you want to scroll.
	- scrollBox.elems.slider; 		  // The slider that will be changing position

Optional: 
	- scrollBox.elems.up;    	 	  // The up button.
	- scrollBox.elems.down;  		  // The down button.
	- scrollBox.options.mouseWheel;   // Set to 'false' if scrolling with mousewheel is not wanted. Defaults 'true'.
	- scrollBox.options.scrollSpeed;  // How fast the animation of the scrolling/clicking is called (time interval).
	- scrollBox.options.wheelSpeed;   // How many pixels the box scrolls on mousewheel.
	- scrollBox.options.increment;    // How many pixles the box scrolls based on click of up/down buttons.
	- scrollBox.options.addDisabled;  // Allows class 'disabled' to be added to up/down buttons for styling. Defaults 'true'.
*/

function scrollBox() {
	var self = this;
	this.topPos = 0;
	this.boxHeight = 0;
	this.windowHeight = 0;
	this.scrollDistance = 0;
	this.clickSpeed = null;
	this.options = {scrollSpeed: 20, wheelSpeed: 15, increment: 2, mouseWheel: true, addDisabled: true};
	this.elems = {frame: null, slider: null, up: null, down: null};
	this.init = function () {
		if(this.elems.frame != null){
			this.topPos = 0;
			this.windowHeight = this.elems.frame.offsetHeight;
			this.boxHeight = this.elems.slider.offsetHeight;
			this.scrollDistance = -(this.boxHeight - this.windowHeight);
			this.elems.slider.style.top = this.topPos + 'px';
			this.options.clickSpeed = this.options.increment;
			if(this.elems.down && this.boxHeight > this.windowHeight){
				if(this.options.addDisabled){
				//	this.addDisabled(self.elems.up);
				//	this.removeDisabled(self.elems.down);
				}
				this.elems.down.onmousedown = function(event) {
				    var rightClick = self.rightClick(event);
				    if(rightClick != true){
					    self.options.increment = self.options.clickSpeed;					
					    self.slideDown = setInterval(function(){self.scrollDown();}, self.options.scrollSpeed);
			    	//	if(self.topPos > self.scrollDistance) {
			    	//		self.removeDisabled(self.elems.up);
			    	//	}
				    	return false;
				    }
				};
				this.elems.down.onmouseup = function() {
					clearInterval(self.slideDown);
					return false;
				};
				this.elems.down.onmouseout = function() {
					if(typeof(self.slideDown) != 'undefined') {
						clearInterval(self.slideDown);
					}
				};		
			}
			if(this.elems.up){
				this.elems.up.onmousedown = function(event) {
				    var rightClick = self.rightClick(event);
				    if(rightClick != true){	
				    	self.options.increment = self.options.clickSpeed;
			    		self.slideUp = setInterval(function(){self.scrollUp();}, self.options.scrollSpeed);
			    	//	if(self.topPos >= self.scrollDistance) {
			    	//		self.removeDisabled(self.elems.down);
			    	//	}
			    		return false;
			        }
				};
				this.elems.up.onmouseup = function() {
					if(typeof(self.slideUp) != 'undefined') {
						clearInterval(self.slideUp);
					}
				};
				this.elems.up.onmouseout = function() {
					if(typeof(self.slideUp) != 'undefined') {
						clearInterval(self.slideUp);
					}
				};
			} 
			if(this.options.mouseWheel && self.topPos > self.scrollDistance){
				self.initWheel();
			}
			if(this.boxHeight <= this.windowHeight && self.options.addDisabled){
				$(self.elems.up).addClass('off');
				$(self.elems.down).addClass('off');
				this.addDisabled(self.elems.up);
				this.addDisabled(self.elems.down);
			}
			else{
			    $(self.elems.up).addClass('on');
			    $(self.elems.down).addClass('on');
			    this.removeDisabled(self.elems.up);
			    this.removeDisabled(self.elems.down);
			}
		} 
	};
	this.initWheel = function() {
		if (this.elems.frame.addEventListener) {
			/** DOMMouseScroll is for mozilla. */
			this.elems.frame.addEventListener('DOMMouseScroll', self.scrollWheel, false);
		}
		/** IE/Opera. */
		this.elems.frame.onmousewheel = this.scrollWheel;
	};			
	this.scrollWheel = function(event){
		var delta = 0;
        if (!event){/* For IE. */
            event = window.event;
		}
        if (event.wheelDelta) { /* IE/Opera. */
            delta = event.wheelDelta/120;
        } 
		else if (event.detail) { /** Mozilla case. */
            delta = -event.detail/3;
        }
        if (delta) {
			if(delta < 0) {
				self.options.increment = self.options.wheelSpeed;
				self.scrollDown();
			//	if(self.topPos > self.scrollDistance) {
			//		self.removeDisabled(self.elems.up);
			//	}
			}
			else {
				self.options.increment = self.options.wheelSpeed;
				self.scrollUp();
			//	if(self.topPos >= self.scrollDistance) {
			//		self.removeDisabled(self.elems.down);
			//	}
			}
		}
        if (event.preventDefault) {
			event.preventDefault();
			event.returnValue = false;
		}
	};
	this.scrollDown = function() {
		if(self.topPos > self.scrollDistance) {
			self.topPos = self.topPos - self.options.increment;
			self.elems.slider.style.top = self.topPos + 'px';
		}
		else {
			self.elems.slider.style.top = self.scrollDistance + 'px';
			self.topPos = self.scrollDistance;
			clearInterval(self.slideDown);
		//	if(self.options.addDisabled){
		//		this.addDisabled(self.elems.down);
		//	}
		}
	};
	this.scrollUp = function() {
		if(self.topPos >= 0){
			self.elems.slider.style.top = '0px';
			clearInterval(self.slideUp);
		//	if(self.options.addDisabled){
		//		this.addDisabled(self.elems.up);
		//	}
		}
		else if(self.topPos >= self.scrollDistance) {
			self.topPos = self.topPos + self.options.increment;
			self.elems.slider.style.top = self.topPos + 'px';
		}
	};
	this.addDisabled = function(obj) {
		var currentClass = obj.className;
		if(currentClass == null || currentClass == ''){
			obj.className = 'disabled';
		}
		else if(!currentClass.match('disabled')){
			obj.className = currentClass + ' disabled';
		}
	};
	this.removeDisabled = function(obj) {
		var currentClass = obj.className;
		if(currentClass.match('disabled')){
			var newClass = currentClass.replace('disabled','');
			obj.className = newClass;
		}
	};
	this.rightClick = function(e) {
		var rightclick;
		if (!e){ 
			var e = window.event;
		}
		if (e.which) {
			var rightclick = (e.which == 3);
		}
		else if (e.button) {
			var rightclick = (e.button == 2);
			if(e.button == 3){
				rightclick = true;
			}
		}
		return rightclick;
	}
}