/**
 * class	CS_ShowcaseSlider
 * author	Paul Kruijt
 */
var CS_ShowcaseSlider = new Class({
	
	/**
	 * initialize
	 * @param	string	root_node_id
	 * @return	void
	 */
	initialize: function(root_node_id)
	{
		// nodes
		this.root_node	= !root_node_id ? document.getElement('body') : $(root_node_id);
		
		// id's
		this.root_node_id	= !root_node_id ? null : root_node_id;
		
		// classes
		this.slider_wrapper_class				= 'cs_showcase_slider_wrapper';
		this.slider_class						= 'cs_showcase_slider';
		this.handler_left_wrapper_class			= 'cs_showcase_slider_handler_left_wrapper';
		this.handler_left_class					= 'cs_showcase_slider_handler_left';
		this.handler_right_wrapper_class		= 'cs_showcase_slider_handler_right_wrapper';
		this.handler_right_class				= 'cs_showcase_slider_handler_right';
		this.scrollbar_wrapper_class			= 'cs_scrollbar_wrapper';
		this.scrollbar_handler_class			= 'cs_scrollbar_handler';
		
		// settings
		this.slider_wrapper_width	= 0;
		this.slider_width			= 0;
		this.slider_duration		= 1000;
	},
	
	/**
	 * start
	 * @return void
	 */
	start: function()
	{
		if (this.root_node)
		{
			// initialize settings
			this.initSettings();
			
			// set events
			this.setEvents();
		}
	},
	
	/**
	 * initialize settings
	 * @return	void
	 */
	initSettings: function()
	{
		// get slider wrapper width
		var slider_wrapper_node	= this.root_node.getElement('.'+this.slider_wrapper_class);
		
		if (slider_wrapper_node)
		{
			this.slider_wrapper_coordinates	= slider_wrapper_node.getCoordinates();
			this.slider_wrapper_width		= this.slider_wrapper_coordinates.width.toInt();
		}
		
		// get slider width
		var slider_node	= this.root_node.getElement('.'+this.slider_class);
		
		if (slider_node)
		{
			this.slider_coordinates	= slider_node.getCoordinates();
			this.slider_width		= this.slider_coordinates.width.toInt();
		}
	},
	
	/**
	 * set events
	 * @return	void
	 */
	setEvents: function()
	{
		// set handler events
		this.setHandlerEvents();
	},
	
	/**
	 * set handler events
	 * @return	void
	 */
	setHandlerEvents: function()
	{
		// set vars
		var _this				= this;
		var handler_left_node	= this.root_node.getElement('.'+this.handler_left_wrapper_class);
		var handler_right_node	= this.root_node.getElement('.'+this.handler_right_wrapper_class);
		
		if (handler_left_node && handler_right_node)
		{
			handler_left_node.removeEvents();
			handler_left_node.addEvents(
			{
				'click' : function()
				{
					_this.slide(2);
					
					return false;
				}
			});
			
			handler_right_node.removeEvents();
			handler_right_node.addEvents(
			{
				'click' : function()
				{
					_this.slide(1);
					
					return false;
				}
			});
		}
	},
	
	/**
	 * slide
	 * @param	integer	direction (1=forward; 2=backward)
	 * @return	void
	 */
	slide: function(direction)
	{
		// set vars
		var _this					= this;
		var slider_node				= this.root_node.getElement('.'+this.slider_class);
		
		if (direction && slider_node)
		{
			// calculate positions
			var pos_start	= slider_node.getStyle('left').toInt();
			var pos_end		= direction == 1 ? (pos_start - this.slider_wrapper_width) : (pos_start + this.slider_wrapper_width);
			pos_end			= pos_end.toInt();
			
			if (pos_end > 0) pos_end = 0;
			else if (pos_end < (0 - (this.slider_width - this.slider_wrapper_width))) pos_end = (0 - (this.slider_width - this.slider_wrapper_width));
			
			// create filter
			this.cs_filter = new CS_Filter(this.root_node_id);
			this.cs_filter.create();
			
			var slide_effect = new Fx.Morph(slider_node, {duration: this.slider_duration, transition: Fx.Transitions.Quad.easeInOut});
			slide_effect.start({'left': [pos_start, pos_end]}).chain(function()
			{
				// remove filter
				if (_this.cs_filter) _this.cs_filter.remove();
			});
			
			// do stuff when a scrollbar is present
			if (this.cs_scrollbar)
			{
				var scrollbar_wrapper_node = this.root_node.getElement('.'+this.scrollbar_wrapper_class);
				var scrollbar_handler_node = this.root_node.getElement('.'+this.scrollbar_handler_class);
				
				if (scrollbar_wrapper_node && scrollbar_handler_node)
				{
					// calculate positions
					var scrollbar_wrapper_width		= scrollbar_wrapper_node.getWidth().toInt();
					var scrollbar_handler_width		= scrollbar_handler_node.getWidth().toInt();
					var real_scrollable_width		= scrollbar_wrapper_width - scrollbar_handler_width;
					var scrollbar_handler_pos_start	= scrollbar_handler_node.getStyle('left').toInt();
					
					// make pos end a positive number
					var new_pos_end	= pos_end - pos_end - pos_end;
					
					// set new positions
					var scrollbar_handler_pos_end	= (new_pos_end / (this.slider_width - this.slider_wrapper_width)) * real_scrollable_width;
					scrollbar_handler_pos_end		= scrollbar_handler_pos_end.toInt();
					
					var scrollbar_handler_slide_effect = new Fx.Morph(scrollbar_handler_node, {duration: this.slider_duration, transition: Fx.Transitions.Quad.easeInOut});
					scrollbar_handler_slide_effect.start({'left': [scrollbar_handler_pos_start, scrollbar_handler_pos_end]}).chain(function()
					{
						// set scrollbar position
						_this.cs_scrollbar.moveTo(new_pos_end + pos_end);
					});
				}
			}
		}
	},
	
	/**
	 * enable scrollbar
	 * @return	void
	 */
	enableScrollbar: function()
	{
		// set nodes
		
		if (this.root_node)
		{
			var slider_wrapper_node	= this.root_node.getElement('.'+this.slider_wrapper_class);
			
			if (slider_wrapper_node)
			{
				this.cs_scrollbar = new CS_Scrollbar(this.root_node, slider_wrapper_node, 'horizontal');
				this.cs_scrollbar.start();
			}
		}
	}
});