// JavaScript Document

window.onload = function(){
	
	menu();
	
	box=document.getElementById("boxContent");
	contentSlide=document.getElementById("content");
	cursor=document.getElementById("scrollBtn");
	cursorTrack=document.getElementById("scrollerTrack");
	disabled=false;
	
	maxScroll = contentSlide.offsetHeight-box.offsetHeight;
	if(maxScroll < 0){
		maxScroll = 0;
		disabled=true;
	}
	
	ratio = Math.round(Math.abs((cursorTrack.offsetHeight-cursor.offsetHeight)/maxScroll)*100)/100;
	delta = 0;
	dragging = false;
	scrollStep = 20;
	
	if (!disabled){
		
		if(window.addEventListener) box.addEventListener('DOMMouseScroll',slide,false);
		box.onmousewheel = slide;
		
		cursor.onmousedown = function(){
			document.onmousemove = startDragHandle;
			return false;
		};
		document.onmouseup = function(){
			document.onmousemove = null;
			if(dragging) stopDragHandle();
		};
		cursor.onselectstart = function(){return false;}
	}

	else{
		document.getElementById('scrollerTrack').removeChild(document.getElementById('scrollBtn'));
	} 
}

function startDragHandle(e){
	if(!e) var e = window.event;
	if(e.pageY) tmpY=(e.pageY-cursorTrack.offsetTop)-((cursor.offsetHeight*1.75)+50);
	else tmpY=((e.clientY-cursorTrack.offsetTop)-(cursor.offsetHeight*1.75))+document.documentElement.scrollTop-50;
	
	if(tmpY<0 ) tmpY=0;
	else if(tmpY > (box.offsetHeight-cursor.offsetHeight)) tmpY=(box.offsetHeight-cursor.offsetHeight);	
	
	moveHandle();
	dragging = true;
}

function stopDragHandle(){
	dragging = false;
	cursorY = tmpY;
	cursor.style.top=cursorY+"px";
}

function moveHandle(){
	cursor.style.top=tmpY+"px";
	currPos=parseInt((tmpY*-1)/ratio);
	contentSlide.style.top=currPos+"px";
}

function slide(event){
	if(!event) event = window.event;
	
	if(event.wheelDelta) delta=event.wheelDelta/120;
	else if(event.detail) delta=-event.detail/3;
	
	if(delta > 0) scrollUp();
	else if(delta<0) scrollDown();
	
	if(event.preventDefault)
		event.preventDefault();
	event.returnValue = false;
}

function scrollUp(){
	if(contentSlide.style.top!="0px" && contentSlide.style.top != 0){
		currPos = contentSlide.offsetTop;
		currPos=parseInt(currPos)+scrollStep;
		if(currPos>0) currPos=0;
		contentSlide.style.top=currPos+"px";
		updateScroller();
	}
}

function scrollDown(){
	if(parseInt(contentSlide.style.top) > "-"+(contentSlide.offsetHeight-box.offsetHeight) || !contentSlide.style.top){
		currPos = contentSlide.offsetTop;
		currPos=parseInt(currPos)-scrollStep;
		if(currPos<-(contentSlide.offsetHeight-box.offsetHeight)){
			currPos = -(contentSlide.offsetHeight-box.offsetHeight);
		}
		contentSlide.style.top=currPos+"px";
		updateScroller();
	}
}

function updateScroller(){
	cursorPos=parseInt((currPos*-1)*ratio);
	cursor.style.top=cursorPos+"px";
}


