//var scrollIncrement = 24; //pixels
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-container' will become scrollable 
	*   using the first two anchors in the next element after the scroll container
	*   with the class 'custom-scroll'
	*
	*	Requirements:
	*   All children of the 'custom-scroll-container' are wrapped in a div to contain the content that scrolls.
	*   The div that is wrapped around the scrollable content is positioned absolute.
	*   The custom-scroll-container gets a fixed height, position relative, and overflow:hidden.
	*   The fixed height comes from the parent of custom-scroll-container, so that must have a fixed height already.
	**/
	$(window).load( function(){
		$('.custom-scroll-container').each( function(i){
		    var container = $(this);
		    var containerHeight = container.parent().height();
		    container.height(containerHeight);
		    $(this).wrapInner('<div style="position:absolute;top:0px;left:0px;width:100%;"></div>');
		    var scroll = $(this).children(':first');
		    var scrollHeight = scroll.height();	    
		    var scrollIncrement = parseInt(containerHeight * scrollWeight);
		    
		    var scrollButtons = container.next('.custom-scroll');
		    if (scrollHeight > containerHeight) {
		    	scrollButtons.show();
		    	var minTop = containerHeight - scrollHeight;
		    	var scrolling = false;
		    	// Scroll Wheel
		    	scroll.mousewheel(function(event, delta){
		    		if(!scrollLock){
		    			scrollLock = true;
		    			var oldTop = parseInt(scroll.css('top')) || 0;
						var newTop = oldTop + (scrollIncrement * delta);
						if(newTop < minTop){
							newTop = minTop;
						}else if(newTop > 0){
							newTop = 0;
						}
						scroll.animate({ top: newTop + 'px'}, scrollDuration, "linear", function(){
							if(!scrolling){
								scrollLock = false;
							}
						});
		    		}
		    		return false;
		    	});
		    	// Scroll Buttons
		    	scrollButtons.children('a').each(function(i){
					if(i == 0){
						$(this).mousedown(function(event){
						    if(!scrollLock){		
						    	scrollLock = true;			    
								scrolling = false;
								var oldTop = parseInt(scroll.css('top')) || 0;
								var newTop = oldTop + scrollIncrement;
								if(newTop < minTop){
									newTop = minTop;
								}else if(newTop > 0){
									newTop = 0;
								}
								scroll.animate({ top: newTop + 'px'}, scrollDuration, "linear", function(){
									if(!scrolling){
										scrollLock = false;
									}
								});
								$(this).everyTime(scrollDuration + scrollDelay, "scrollUp", function() {
								    scrollLock = true;
									scrolling = true;						
									oldTop = newTop;
									newTop = oldTop + scrollIncrement;
									if(newTop < minTop){
										newTop = minTop;
									}else if(newTop > 0){
										newTop = 0;
									}
									var myScrollDuration = scrollDuration;
									if(newTop - oldTop != scrollIncrement){
										myScrollDuration = (scrollDuration / scrollIncrement) * (newTop - oldTop);
									}
									scroll.animate({ top: newTop + 'px'}, myScrollDuration, "linear", function(){
										if(!scrolling){
											scrollLock = false;
										}
									});
								}, 0, true);
							}
						}).mouseup(function(event){
							$(this).stopTime("scrollUp");
							if(scrolling){
								scroll.stop();
								scrollLock = false;
							}
						}).mouseout(function(event){
							$(this).stopTime("scrollUp");
							if(scrolling){
								scroll.stop();
								scrollLock = false;
							}
						}).click(function(event){
							return false;
						});
					}else if(i == 1){
						$(this).mousedown(function(event){
							if(!scrollLock){
								scrollLock = true;
			    				scrolling = false;
								var oldTop = parseInt(scroll.css('top')) || 0;
								var newTop = oldTop - scrollIncrement;
								if(newTop < minTop){
									newTop = minTop;
								}else if(newTop > 0){
									newTop = 0;
								}
								scroll.animate({ top: newTop + 'px'}, scrollDuration, "linear", function(){
								    if(!scrolling){
										scrollLock = false;
									}
								});
								$(this).everyTime(scrollDuration + scrollDelay, "scrollDown", function() {
									scrollLock = true;
									scrolling = true;
									oldTop = newTop;
									newTop = oldTop - scrollIncrement;
									if(newTop < minTop){
										newTop = minTop;
									}else if(newTop > 0){
										newTop = 0;
									}
									var myScrollDuration = scrollDuration;
									if(oldTop - newTop != scrollIncrement){
										myScrollDuration = (scrollDuration / scrollIncrement) * (oldTop - newTop);
									}
									scroll.animate({ top: newTop + 'px'}, myScrollDuration, "linear", function(){
										if(!scrolling){
											scrollLock = false;
										}
									});
								}, 0, true);
							}
						}).mouseup(function(event){
							$(this).stopTime("scrollDown");
							if(scrolling){
								scroll.stop();
								scrollLock = false;
							}
						}).mouseout(function(event){
							$(this).stopTime("scrollDown");
							if(scrolling){
								scroll.stop();
								scrollLock = false;
							}
						}).click(function(event){
							return false;
						});
					}
				});
		    } else {
		    	scrollButtons.hide();
		    }
			
		});
	});
});


/** This has nothing to do with the scrollers, just fixing Nav in IE7*/
$(function(){
    if($.browser.msie && ($.browser.version.substr(0,1) == "6" || $.browser.version.substr(0,1) == "7")){
		$('#main-nav > li').each(function(){
			var myWidth = $('a:first', this).width();
			$('ul:first', this).css('margin-left', 0 - 37 - myWidth);
		});
	}
    if($.browser.msie && $.browser.version.substr(0,1) == "6" ){
		$('#main-nav > li').each(function(){
			$(this).mouseenter(function(){
				$(this).addClass('sfhover');
			}).mouseleave(function(){
				$(this).removeClass('sfhover');
			});
		});
		/** IE 6 Fix Attempt
		$('a', 'table#footer-table').each(function(){
			$(this).append('
');
		}); **/
	}
});