Hootenanny = window.Hootenanny || {};

Hootenanny.Scrollable = function() {
	var _config = null;
	
	var _content_container = 'scrollable-content';
	var _content_wrapper = 'scrollable-content-wrapper';
	var _scroller_wrapper = 'scroller-wrapper';
	var _slider_bg = 'slider-bg';
	var _slider_thumb = 'slider-thumb';
	var _scroller_up = 'scroller-up';
	var _scroller_down = 'scroller-down';
	var _slider_height = null;
	var _content_height = null;
	var _slider = null;
	
	function _init_slider() {
		_slider = YAHOO.widget.Slider.getVertSlider(_slider_bg, _slider_thumb, 0, _config.slider_height);

		_slider.subscribe("change", function(offsetFromStart) {
			var content_height = $('#' + _content_container).height();
			
			// make sure div needs scrolling
			if (content_height > 328) {
				//var new_val = Math.floor(((content_height - _config.content_height)/_config.slider_height) * offsetFromStart);
				var new_val = (_slider.getValue() == 0) ? 0 : Math.floor(((content_height - _config.content_height)/_config.slider_height) * offsetFromStart);
				
				var attributes = {
					scroll: { to: [0, new_val] }
				};
				var anim = new YAHOO.util.Scroll(_content_wrapper, attributes, 0.5, YAHOO.util.Easing.easeOut);
				anim.animate();
			}
		});
		
		// set up arrow links
		$('#' + _scroller_up).mousedown(function() {
			$(this).find('img:first').attr('src', '/images/scroller_up_hover.png');
			
			if (_slider.getValue() > 0) {
				var new_val = (_slider.getValue() - 18 < 0) ? 0 : _slider.getValue() - 18;
				_slider.setValue(new_val);
			}
		}).mouseup(function() {
			$(this).find('img:first').attr('src', '/images/scroller_up.png');
		}).click(function() { return false; });
		
		$('#' + _scroller_down).mousedown(function() {
			$(this).find('img:first').attr('src', '/images/scroller_down_hover.png');
			
			if (_slider.getValue() < 260) {
				var new_val = (_slider.getValue() + 18 > 260) ? 260 : _slider.getValue() + 18;
				_slider.setValue(new_val);
			}
		}).mouseup(function() {
			$(this).find('img:first').attr('src', '/images/scroller_down.png');
		}).click(function() { return false; });

		// anything need to happen at start/end of slide? not right now...
		_slider.subscribe("slideStart", function() {
			// ??
		});

		_slider.subscribe("slideEnd", function() {
			// make sure scroll value matches slider if at 0
			if (_slider.getValue() == 0) {
				$('#' + _content_wrapper).attr('scrollTop', '0px');
			}
			
			// if a custom listener method was passed, fire it now
			if (_config.slider_change?true:false) {
				_config.slider_change();
			}
		});
		
		_calibrate_slider();
	}
	
	function _calibrate_slider() {
		// make sure scrolling div and scroller handle are back at the top
		$('#' + _content_wrapper).attr('scrollTop', '0px');
		_slider.setValue(0);
		
		if ($('#' + _content_container).height() > _config.content_height) {
			$('#' + _scroller_wrapper).css('visibility', 'visible');
		} else {
			$('#' + _scroller_wrapper).css('visibility', 'hidden');
		}
	}
	
	return {
		init: function() {
			if (arguments.length != 1) {
				alert('slider cannot intitialize - configuration parameters not present');
				return;
			}
			
			_config = arguments[0];
			_init_slider();
		},
		
		calibrate_slider: function() {
			_calibrate_slider();
		},
		
		get_content_wrapper: function() {
			return $('#' + _content_wrapper);
		},
		
		get_slider_value: function() {
			return _slider.getValue();
		}
	}
}();