/*
@author: Will Cavanagh
@date: 2009-09-14
* Custom scroll box
*/

	var customScroller = {

		intRegex : /^\d+$/,
		maxScroll : 0,
		inited : false,
		upColor : "#FFF",
		downColor : "#FFF", 
		defSpeed : 1000,
		
		//init function -- sets config values and initallizes jQuery slider.
		//@param options : object containing set up parameters
		//@return null
		init : function(options) {
			//if there are no options/colors specified, make empty object
			if(!options) { options = { }; }
			if(!options.sliderHandle) { options.sliderHandle = { }; }
			
			//assign variables, use defaults if not defined.
			var width = this.intRegex.test(options.width) ? options.width : 500;
			var height = this.intRegex.test(options.height) ? options.height : 300;
			var scrollHeight = this.intRegex.test(options.scrollbarHeight) ? options.scrollbarHeight : height-50;
			var sliderHandleH = this.intRegex.test(options.sliderHandle.height) ? options.sliderHandle.height : 15;
			var sliderHandleW = this.intRegex.test(options.sliderHandle.width) ? options.sliderHandle.width : 10;
			var vertical = options.vertical === null ? true : options.vertical;
			upColor = options.sliderHandle.upColor === null ? '#4a4a4a' : options.sliderHandle.upColor;
			downColor = options.sliderHandle.downColor === null ? '#333' : options.sliderHandle.downColor;
			var bkgdColor = options.bkgdColor === null ? '#848484' : options.bkgdColor;
			defSpeed = options.defaultSpeed === null ? '1000' : options.defaultSpeed;
			
			//set content width before measuring
			jQuery("#content-scroll").css({width: width});
		
			//get height of content, subtract height of pane to be shown in
			maxScroll = jQuery("#content-scroll").height() - height;
			//set the height of pane to hold content.  This is done after measuring content height for browser compatability reasons
			jQuery("#content-scroll").css({width: width, height: height});
			
			var orientation;
			if(this.vertical) {
				orientation = 'vertical';
			} else {
				orientation = 'horizontal';
			}
	
			//create the jQuery.UI slider
			jQuery("#content-slider").slider({
				value: 100,
				orientation: 'vertical',
	   			animate: false,
	  			change: customScroller.handleSliderChange,
	    		slide: customScroller.handleSliderSlide,
		  		start: customScroller.handleSliderStart,
	   			stop: customScroller.handleSliderStop
		    });
	    	
			jQuery("#content-slider").css({height:scrollHeight-sliderHandleH, top:sliderHandleH, width:sliderHandleW});
   			jQuery(".ui-slider-handle").css({background:upColor, width:sliderHandleW, height:sliderHandleH});
   			jQuery("#slider-bkg").css({background:bkgdColor, width:sliderHandleW, height:scrollHeight});
   	 	
			jQuery("#content-scroll").mousewheel(function(objEvent, intDelta){
				customScroller.scroll(intDelta * -30, 0);
			});
					
			this.inited = true;
		},
		
		//animates a scroll to the beginning of the content
		//@return null
		goTop : function() {
			if(!customScroller.inited) { alert("Run init()"); }
			var scrollto = 0;
			jQuery("#content-scroll").animate({scrollTop: scrollto}, {queue:false, duration:defSpeed});
			jQuery("#content-slider").slider('option', 'value', 100*(1-(scrollto/maxScroll)));
		},
		
		
		//handler function bound to a slider change event.
		//@param e : event
		//@param ui : slider ui object
		//@return null
		handleSliderChange : function(e, ui) {
			if(!customScroller.inited) { alert("Run init()"); }
			jQuery("#content-scroll").animate({scrollTop: ((100-ui.value) / 100) * (maxScroll) }, {queue:false, duration:defSpeed});
		},
		
		//handler function bound to a slider slide event.
		//@param e : event
		//@param ui : slider ui object
		//@return null
		handleSliderSlide : function(e, ui) {
			if(!customScroller.inited) { alert("Run init()"); }
			jQuery("#content-scroll").attr({scrollTop: ((100-ui.value) / 100) * (maxScroll) });
		},
		
		//handler function bound to a slider start of slide event.
		//@return null
		handleSliderStart : function() {
			if(!customScroller.inited) { alert("Run init()"); }
			jQuery(".ui-slider-handle").css({background:downColor});
		},		

		//handler function bound to a slider end of slide event.
		//@return null
		handleSliderStop : function() {
			if(!customScroller.inited) { alert("Run init()"); }
			jQuery(".ui-slider-handle").css({background:upColor});
		},		


		//scroll by a given amount.
		//@param amt : amount to scroll by
		//@param speed : sroll animation speed, defaults to default speed defined in init params
		//@return null
		scroll : function(amt, speed) {
			if(!customScroller.inited) { alert("Run init()"); }
			if(!this.intRegex.test(speed)) { speed = defSpeed; }
			var scrollto = jQuery("#content-scroll").scrollTop() + amt;
			if(scrollto > (maxScroll - 20)) { scrollto = maxScroll; } //near or past end of content, scroll to end
			if(scrollto < 20) { scrollto = 0; } //near or past beginning of content, scroll to beginning
		
			jQuery("#content-scroll").animate({scrollTop: scrollto}, {queue:false, duration:speed}); //animate content scroll
			jQuery("#content-slider").slider('option', 'value', 100*(1-(scrollto/maxScroll))); //update slider position
		}
	
	
	};
