//browser
var ua=$.browser;
//prevents text selection if using scrollbar

	var shield=0;
	$(document).mousedown(function(){if(shield==1)return false;});
	
//timer object and functions
	var ti = {delay:'',timer:''};

//scrollbar object
	var sb={x0:0,y0:0,x:0,y:0,h:0,top:0,dy_t:0,dy_b:0,scalar:0};
//tracks mouse position
	var track=function(event) {
			//set current position
			sb.x=event.clientX;
			sb.y=event.clientY;
			var dif=sb.y-sb.y0;
			//up movement
			if (dif<0){
				//boundary condition
				if (-dif>sb.dy_t||sb.dy_t==0){
					$('#scrollbar').css('top','20px');
					$('#scrollbar_content').css('top','0px');
					return;
				}
				else {
					$('#scrollbar').css('top',sb.top+dif+'px');
					$('#scrollbar_content').css(
					'top',-(Number($('#scrollbar').css('top').replace('px',''))-20)*sb.scalar+'px');
					return;
				}	
			}
			//down movement
			if (dif>0){
				//boundary condition
				if (dif>sb.dy_b||sb.dy_b==0){
					$('#scrollbar').css('top',300-sb.h+'px');
					$('#scrollbar_content').css('top',298-$('#scrollbar_content').height()+'px');
					return;
					}
				else {
					$('#scrollbar').css('top',sb.top+dif+'px');
					$('#scrollbar_content').css(
					'top',-(Number($('#scrollbar').css('top').replace('px',''))-20)*sb.scalar+'px');
					return;
				}
			}
	}
//arrow functions
	function hold_d() {
			ti.timer=setInterval("move_d()",20);
		}
	function hold_u() {
			ti.timer=setInterval("move_u()",20);
		}
		
	function move_d() {
		sb.top=Number($('#scrollbar').css('top').replace('px',''));
		if(sb.top>=300-sb.h)return;
		$('#scrollbar').css('top',sb.top+1+'px');
		$('#scrollbar_content').css(
		'top',-(Number($('#scrollbar').css('top').replace('px',''))-20)*sb.scalar+'px');
	}
	function move_u() {
		sb.top=Number($('#scrollbar').css('top').replace('px',''));
		if(sb.top<=20)return;
		$('#scrollbar').css('top',sb.top-1+'px');
		$('#scrollbar_content').css(
		'top',-(Number($('#scrollbar').css('top').replace('px','')-20))*sb.scalar+'px');
	}
	
	function move_down(){
		ti.delay=setTimeout("hold_d()",500);
		shield=1;
		$(document).bind('mouseup',function(){
			shield=0;
			clearTimeout(ti.delay);
			clearInterval(ti.timer);
			$(document).unbind('mouseup');
		});
		move_d();
	}
	function move_up(){
		ti.delay=setTimeout('hold_u()',500);
		shield=1;
		$(document).bind('mouseup',function(){
			shield=0;
			clearTimeout(ti.delay);
			clearInterval(ti.timer);
			$(document).unbind('mouseup');
		});
		move_u();
	}
	function stop_move(){
		clearTimeout(ti.delay);
		clearInterval(ti.timer);
	}
	function scroll_jump(event){
		var offset=$('#scrollbar_side').offset().top;
		sb.y=event.clientY;
		
		if(sb.y<offset+20)return;
		if(sb.y>offset+180)return;
		
		var adj=$('#scrollbar').height()/2+20;
		var limit_u=offset+adj;
		var limit_d=200+offset-adj;
		
		if(sb.y<limit_u){
			$('#scrollbar').css('top','20px');
			$('#scrollbar_content').css('top',-(Number($('#scrollbar').css('top').replace('px','')-20))*sb.scalar+'px');
			return;
		}
		if(sb.y>limit_d){
			$('#scrollbar').css('top',300-sb.h+'px');
			$('#scrollbar_content').css('top',298-$('#scrollbar_content').height()+'px');
			return;
		}
		$('#scrollbar').css('top',sb.y-offset-$('#scrollbar').height()/2+'px');
		$('#scrollbar_content').css('top',-(Number($('#scrollbar').css('top').replace('px','')-20))*sb.scalar+'px');
		return;
	}
//end track mouse position
$(function(){
	if(ua.msie&&ua.version<9){
		$('#scrollbar_container').css('width','221px');
	}
	if(ua.mozilla && (ua.version.slice(0,5)=='1.9.0' || ua.version.slice(0,5)=='1.9') && ua.version.slice(6,8)<=18){
		$('#scrollbar_container').css('width','221px');
	}
//map size of scrollbar to size of scrollbar_content div
	//min size=20,max size=160
	//ratio of container to content
	var ratio=($('#scrollbar_container').height()-22)/$('#scrollbar_content').height();
	if(ratio>=1){$('#scrollbar').css('height','160px');}
	if(ratio<=.1){$('#scrollbar').css('height','20px');}
	if(ratio>.1&&ratio<1){$('#scrollbar').css('height',Math.round($('#scrollbar').height()*ratio)+'px');}
	//height of scrollbar and top css value
	sb.h=$('#scrollbar').height();
	//set scalar for mapping content height to scrollable distance
	sb.scalar=($('#scrollbar_content').height()-298)/(280-sb.h);
//clicking scrollbar function
	$('#scrollbar').mousedown(
	function(event){
			shield=1;
			//set initial click position
			sb.x0=event.clientX;
			sb.y0=event.clientY;
			//set sb.top for reference top value
			sb.top=Number($('#scrollbar').css('top').replace('px',''));
			//distance in pixels between top of scrollbar and top arrow
			sb.dy_t=sb.top-20;
			//distance in pixels between bottom of scrollbar and bottom arrow
			sb.dy_b=300-sb.h-sb.top;				
			$(document).bind('mousemove',track);
			$(document).bind('mouseup',function(){
				shield=0;
				$(document).unbind('mousemove',track);
				$(document).unbind('mouseup');
			});
	});
//arrow buttons functions
	//down arrow
	
	$('#arrow_bottom').bind('mousedown',move_down);
	$('#arrow_bottom').bind('mouseup',stop_move);
	$('#arrow_bottom').bind('mouseout',stop_move);

	//up arrow

	$('#arrow_top').bind('mousedown',move_up);
	$('#arrow_top').bind('mouseup',stop_move);
	$('#arrow_top').bind('mouseout',stop_move);

//jump-to function
	if(!ua.msie){
		$('#scrollbar_side').bind('click',scroll_jump);
	}
	
});
