var scrollWeight = .4; // scrollIncrement will be containerHeight * scrollWeight (based off height of viewport)
var scrollDuration = 500; //milliseconds
var scrollDelay = 0; /* milliseconds between scrolling */
var scrollLock = false;

$(function(){
	/**
	*	Each element with class 'custom-scroll-wrapper' will become scrollable 
	*
	*	Requirements:
	*   The div that is wrapped around the scrollable content is positioned absolute.
	*   The custom-scroll-wrapper gets a fixed height, position relative, and overflow:hidden.
	*   The fixed height comes from the parent of custom-scroll-wrapper, so that must have a fixed height already.
	**/
	$(window).load( function(){
	    initScrollers();
	});
});

function initScrollers(){
	$('.custom-scroll-wrapper').each( function(i){
	    var container = $(this);
	    if($.browser.msie && ($.browser.version.substr(0,1) == "7" || $.browser.version.substr(0,1) == "6")){
	    	//Fix Overflow
	    	container.css('overflow', 'hidden');
	    }
	    var containerHeight = container.parent().height();
	    container.height(containerHeight);
	    var scroll = $(this).children('.custom-scroll-content');
	    var scrollHeight = scroll.height();	    
	    var scrollIncrement = parseInt(containerHeight * scrollWeight);		    
	    var scrollBar = $(this).children('.custom-scroll-bar');
	    scrollBar.height(containerHeight - 30);
	    if (scrollHeight > containerHeight) {
	    	//Disable Anchor Links temporarily
	    	$('a', scroll).each(function(){
				  if(this.hash.length > 0){
				    if(this.hash.substring(0, 1) == '#'){
				    	$(this).click(function(){return false;});
				    }
				  }
	    	});
	    	
	    	//Show the Scroll Bar
	    	scrollBar.show();
	    	var minTop = containerHeight - scrollHeight;
			var newTop = 1; 
	    	
	    	// Scroll Bar
	    	scrollBar.slider('destroy').slider({
	    	    animate: true,
				orientation: "vertical",
				min: 0,
				max: scrollHeight - containerHeight,
				value: scrollHeight - containerHeight,
				slide: function(event, ui) {
				    newTop = 0 - (scrollHeight - containerHeight - ui.value);
				    if(event.originalEvent.type == 'mousemove'){
				        // Move Div with Mouse
				        scroll.css("top", newTop + 'px');
				    }else{
				        // Animate Div to new Slide Position
						scroll.stop().animate({ top: newTop + 'px'}, scrollDuration, "linear", function(){
							scrollLock = false;
						});
					}				
				}
			});		   	
	    	// Scroll Wheel
	    	scroll.mousewheel(function(event, delta){
    			var oldTop = 0;
    			if(scrollLock){
    			  oldTop = newTop; // offset from where we're currently animating to
    			}else{
    			  oldTop = parseInt(scroll.css('top')) || 0; // offset from where we currently are
      			  scrollLock = true;
				}						
				newTop = oldTop + (scrollIncrement * delta);
				if(newTop < minTop){
					newTop = minTop;
				}else if(newTop > 0){
					newTop = 0;
				}
				scroll.stop().animate({ top: newTop + 'px'}, scrollDuration, "linear", function(){
					scrollLock = false;
				});
				var newScrollBarValue = scrollHeight - (containerHeight - newTop);
				scrollBar.slider('value',newScrollBarValue);
	    		return false;
	    	});
			$('a', scroll).each(function(){
			  if(this.hash.length > 0){
			    if(this.hash.substring(0, 1) == '#'){
				  var target = this.hash.substring(1);
				  //console.log(target);
			      if($('a[name=' + target + ']', '.custom-scroll-content').length > 0){
				      var newTop = 0 - $('a[name=' + target + ']', '.custom-scroll-content').position().top + 20;
				      $(this).click(function(){
				      	//console.log('target: ' + target);
				      	//console.log('newTop: ' + newTop);
						scrollLock = true;
						if(newTop < minTop){
							newTop = minTop;
						}else if(newTop > 0){
							newTop = 0;
						}
						scroll.stop().animate({ top: newTop + 'px'}, scrollDuration, "linear", function(){
							scrollLock = false;
						});
						var newScrollBarValue = scrollHeight - (containerHeight - newTop);
						scrollBar.slider('value',newScrollBarValue);
				      	return false;
				      });
				      //console.log('anchor link bound: ' + target);
			      }
			    }
			  }
			});
	    } else {
	        scroll.unbind("mousewheel");
	    	scrollBar.hide();
	    }			
	});		
}

function resetScrollers(){
	$('.custom-scroll-wrapper').each( function(i){
	    var container = $(this);
	    var containerHeight = container.height();
	    var scroll = $(this).children('.custom-scroll-content');
	    var scrollHeight = scroll.height();	    	    
	    var scrollBar = $(this).children('.custom-scroll-bar');
	    if(scrollBar.length > 0){
	    	scrollBar.slider('value', scrollHeight - containerHeight);
	    }
	    scroll.css('top', '0px');	    			
	});	
}