/**
 * @class	CS_ShowcaseSlider
 * @author	Paul Kruijt
 * @author	Jeffrey van der Veen
 */
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; // default 1 second
		this.time_interval			= 2000; // default 2 seconds
		
		// overall
		this.slider_play	= null;
		this.direction		= 1;
	},
	
	/**
	 * start
	 * @param	integer	slider_duration (milliseconds)
	 * @return	void
	 */
	start: function(slider_duration)
	{
		if (this.root_node)
		{
			// set slider duration
			if (slider_duration) this.slider_duration = slider_duration;
			
			// 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
		if (this.slider_width > this.slider_wrapper_width)
		{
			if (this.slider_play) this.setSliderEvents();
			else this.setHandlerEvents();
		}
	},
	
	/**
	 * set slider events
	 * @return	void
	 */
	setSliderEvents: function()
	{
		// set vars
		var _this 		= this;
		var slider_node	= this.root_node.getElement('.'+this.slider_class);
		
		if (slider_node)
		{
			// show handlers
			slider_node.removeEvents();
			slider_node.addEvents(
			{
				'mouseenter'	: function(e)
				{
					_this.stop();
				},
				
				'mouseleave'	: function(e)
				{
					_this.play(_this.direction);
				}
			});
		}
	},
	
	/**
	 * 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)
		{
			// show handlers
			handler_left_node.setStyle('display', 'block');
			handler_right_node.setStyle('display', 'block');
			
			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));
			
			// change play direction
			if (this.slider_play)
			{
				if (direction == 2)
				{
					this.direction	= 1;
					pos_end			= 0;
					this.play(this.direction);
				}
				
				if (pos_end == (0 - (this.slider_width - this.slider_wrapper_width)))
				{
					this.direction	= 2
					this.play(this.direction);
				}
			}
			
			// 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();
			}
		}
	},

	/**
	 * enable autostart
	 * @param	integer	time_interval (milliseconds)
	 * @return	void
	 */
	enableAutoplay: function(time_interval)
	{
		// set time interval
		if (time_interval) this.time_interval = time_interval;
		
		// play slider
		this.play(1);
	},
	
	/**
	 * play slider
	 * @return	void
	 */
	play: function(direction)
	{
		var _this = this;
		
		// clear interval
		clearInterval(this.slider_play);
		
		// set interval
		this.slider_play = setInterval(function()
		{
			_this.slide(direction);
		}, this.time_interval);
	},
	
	/**
	 * stop slider
	 * @return	void
	 */
	stop: function()
	{
		// clear interval
		clearInterval(this.slider_play);
	}
});
