/**
 * class CS_Scrollbar
 * @author Paul Kruijt
 */
var CS_Scrollbar = new Class(
{
	/**
	 * Constructor
	 * @param	string/object	scrollbar_node
	 * @param	string/object	target_node
	 * @param	string			scroll_type
	 * @return	void
	 */
	initialize: function(scrollbar_node, target_node, scroll_type)
	{
		// nodes
		this.scrollbar_node	= $type(scrollbar_node) == 'element' ? scrollbar_node : $(scrollbar_node);
		this.target_node	= $type(target_node) == 'element' ? target_node : $(target_node);
		
		// classes
		this.scrollbar_class		= 'cs_scrollbar_wrapper';
		this.handler_class			= 'cs_scrollbar_handler';
		this.handler_start_class	= 'cs_scrollbar_handler_start';
		this.handler_middle_class	= 'cs_scrollbar_handler_middle';
		this.handler_end_class		= 'cs_scrollbar_handler_end';
		
		// settings
		this.scroll_type	= scroll_type != 'horizontal' ? 'vertical' : 'horizontal'; /* default: vertical scroll */
	},
	
	/**
	 * start
	 * @return	void
	 */
	start: function()
	{
		// set vars
		var _this	= this;
		
		if (this.scrollbar_node && this.target_node)
		{
			var scrollbar_node		= this.scrollbar_node.getElement('.'+this.scrollbar_class);
			var handler_node		= this.scrollbar_node.getElement('.'+this.handler_class);
			var target_inner_node	= this.target_node.getElement('div');
			
			if (scrollbar_node && handler_node && target_inner_node)
			{
				// style position target elements
				var target_position = this.target_node.getStyle('position');
				if (target_position != 'absolute') this.target_node.setStyle('position', 'relative');
				
				target_inner_node.setStyles(
				{
					'left'		: '0px',
					'position'	: 'absolute',
					'top'		: '0px'
				});
				
				var steps = (this.scroll_type == 'horizontal' ? (target_inner_node.getScrollSize().x - this.target_node.getSize().x) : (target_inner_node.getScrollSize().y - this.target_node.getSize().y));
				
				// create slider instance (build in mootools)
				this.scrollbar = new Slider(scrollbar_node, handler_node,
				{
					steps		: steps,
					onChange	: function(value)
					{
						var x = (_this.scroll_type == 'horizontal' ? (0 - value) : 0);
						var y = (_this.scroll_type == 'horizontal' ? 0 : (0 - value));
						
						//_this.target_node.scrollTo(x, y);
						
						target_inner_node.setStyles(
						{
							'left'	: x+'px',
							'top'	: y+'px'
						});
					}
				}).set(0);
				
				// set events
				this.setEvents();
			}
		}
	},
	
	/**
	 * set events
	 * @return	void
	 */
	setEvents: function()
	{
		// set vars
		var _this = this;
		
		if (this.scrollbar)
		{
			this.target_node.addEvent('mousewheel', function(e)
			{	
				e			= new Event(e).stop();
				var step	= _this.scrollbar.step - e.wheel * 30;	
				
				_this.moveTo(step);
			});
		}
	},
	
	/**
	 * move to
	 * @return	void
	 */
	moveTo: function(pos)
	{
		if (this.scrollbar && pos)
		{
			this.scrollbar.set(pos);
		}
	}
});