// JavaScript Document
var Slider = Class.create({
	initialize: function(){
		this.thumbBody = $('thumbBody');
		this.gutter = $('scrollBar');
		this.content = $('scrollContent');
		this.mask = $('scrollBox');
		this.adj = $('topArrow').offsetHeight;
		this.markers = [];
		if(this.mask.offsetHeight < this.content.offsetHeight){
			var gutterHeight = this.gutter.offsetHeight - (this.adj*2);
			var thumbheight = (this.mask.offsetHeight * gutterHeight)/this.content.offsetHeight;
			this.thumbBody.style.height = thumbheight;
			controller = new Mover(this.gutter, this.content, this.mask);
			upArrow = new Arrow($('topArrow'), "up", this.adj);
			downArrow = ieVerLt7 ? new Arrow($('bottomArrow'), "down", gutterHeight + ((2*this.adj)-3)) : new Arrow($('bottomArrow'), "down", gutterHeight + this.adj);
			handle = new Handle($('thumb'));
		}else{
			this.gutter.style.visibility = 'hidden';
		}
		
	}
	
});

var Mover = Class.create({
	initialize: function(_gutter, _content, _mask){
		this.thumb = $('thumb');
		this.adj = $('topArrow').offsetHeight;
		this.gutterHeight = _gutter.offsetHeight - (2*this.adj) - this.thumb.offsetHeight;
		this.content = $('scrollContent');
		this.contentHeight = this.content.offsetHeight;
		this.maskHeight = _mask.offsetHeight;
		
	},
	
	scrollContent: function(thumbPos){
		startPos = this.adj;
		scaleA = this.gutterHeight;
		posA = thumbPos;
		scaleB = this.contentHeight - this.maskHeight;
		offsetB = this.xMultiply(scaleB, scaleA, posA )
		posB = this.xMultiply(scaleB, scaleA, posA);
		window.status = posB;
		this.content.style.top=-posB+"px";
	},

 	xMultiply: function(scaleB, scaleA, posA){
		var output = Math.round((scaleB*posA)/scaleA);
		return output;
	}
});

var Arrow = Class.create({
	initialize: function(element, direction, limit){
		var button = element;
		si = 1;
		count = 0;
		button.onmousedown = function(){
			function bump(direction, limit){
				count++;
				var gutterPos = $('scrollBar').offsetTop;
				
				var scrollPosition = $("thumb").offsetTop;
				
				if(scrollArr['lim'] +2 <= scrollPosition && scrollArr['dir'] == 'up'){
					$("thumb").style.top = (scrollPosition - 1) +"px";
					
				}else if(scrollArr['lim'] - $('thumb').offsetHeight - 2 >= scrollPosition && scrollArr['dir'] == 'down'){
					$("thumb").style.top = (scrollPosition + 1) +"px";
				}
				controller.scrollContent(scrollPosition - $('topArrow').offsetHeight);
				
			}
			scrollArr = {dir:direction, lim:limit}
			si = setInterval(bump, 20);		
		}
		button.onmouseup = function(){
			if(si){
				clearInterval(si);	
			}
		}
	}
});

var Handle = Class.create({
	initialize: function(thumb){
		thumb.onmousedown = function(e){
			handle.getInitVals(e);
			document.onmouseup = function(){
				document.onmousemove = null;
			}
			document.onmousemove = function(e){
				handle.setThumbPos(handle.upperLimit, handle.lowerLimit, handle.adj ,e);
			}
		}
	},
	
	setThumbPos:function(topLimit, bottomLimit, adjustment, e){
		var mouseY = handle.getMousePosition(e);
		//window.status = topLimit + " < " + mouseY//topLimit < mouseY && bottomLimit > mouseY;
		if(topLimit < mouseY && bottomLimit > mouseY){
			$('thumb').style.top = mouseY - topLimit + adjustment + "px";
			controller.scrollContent($('thumb').offsetTop - adjustment);
			//window.status = topLimit + " < " + mouseY  + " < " +  bottomLimit ;
		}
		
	},
	
	getMousePosition:function(e){
		var mousePos = !document.all ? e.pageY - this.scrollerPos : window.event.clientY;
		return mousePos;
	},
	
	getInitVals:function(e){
		this.thumbPos = $('thumb').offsetTop;//controller.findPos($('thumb'))[1];
		this.thumbHeight = $('thumb').offsetHeight;
		this.scrollerPos = $('scrollBar').offsetTop;
		this.scrollerHeight = $('scrollBar').offsetHeight;
		this.adj = $('topArrow').offsetHeight;
		
		this.travel = this.scrollerHeight - ((2*this.adj)+this.thumbHeight);
		
		this.initMouse = this.getMousePosition(e);
		this.mouseOffset = this.initMouse - this.thumbPos;
		
		this.minusBottom = this.thumbHeight - this.mouseOffset;
		this.plusTop = this.mouseOffset;
		this.upperLimit = this.adj + this.plusTop;
		this.lowerLimit = ieVerLt7 ? this.scrollerHeight - (3 + this.minusBottom) : this.scrollerHeight - (this.adj + this.minusBottom);
	}
					 
});