/**
 * class	CS_ShowcaseSlider
 * author	Rakesh Sieuw
 */
var CS_ShowcaseSlider = new Class({
	
	/**
	 * initialize
	 * @param	string	root_node_id
	 * @param	integer	speed_move
	 * @param	boolean	infinite_loop
	 * @return	void
	 */
	initialize: function(root_node_id, speed_move, infinite_loop)
	{
		// id's
		this.root_node_id	= root_node_id;
		this.debugger_id	= 'cs_debugger';
		
		// classes
		this.slider_wrapper_class				= 'cs_showcase_slider_wrapper';
		this.slider_class						= 'cs_showcase_slider';
		this.thumbnail_wrapper_class			= 'cs_showcase_slider_thumbnail_wrapper';
		this.thumbnail_class					= 'cs_showcase_slider_thumbnail';
		this.thumbnail_image_wrapper_class		= 'cs_showcase_slider_thumbnail_image_wrapper';
		this.thumbnail_image_class				= 'cs_showcase_slider_thumbnail_image';
		this.thumbnail_title_wrapper_class		= 'cs_showcase_slider_thumbnail_title_wrapper';
		this.thumbnail_title_class				= 'cs_showcase_slider_thumbnail_title';
		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.active_class						= 'cs_active';
		
		// nodes
		this.root_node		= $(this.root_node_id);
		
		// settings
		this.speed_move		= !speed_move ? (2500 / 100000) : (speed_move / 100000);
		this.infinite_loop	= !infinite_loop ? 0 : 1;
		
		// create debugger
		//this.createDebugger();
	},
	
	/**
	 * start
	 * @return void
	 */
	start: function()
	{
		
		if (this.root_node)
		{
			var _this = this;
			
			// settings
			this.root_node_coordinates	= this.root_node.getCoordinates();
			this.root_node_width		= this.root_node_coordinates.width.toInt();
			this.root_node_height		= this.root_node_coordinates.height.toInt();
			
			// get all images
			var image_nodes			= this.root_node.getElements('img');
			var total_image_nodes	= image_nodes.length;
			
			if (total_image_nodes > 0)
			{
				var first_column_node	= this.root_node.getElement('td');
				
				if (first_column_node)
				{
					// clone first column only when infinite loop is enabled
					if (this.infinite_loop == 1)
					{
						first_column_node.clone().injectAfter(first_column_node);
					}
					
					// set events
					this.setEvents();
				}
			}
		}
	},
	
	/**
	 * set events
	 * @return	void
	 */
	setEvents: function()
	{
		var _this = this;
		
		var slider_wrapper_node	= this.root_node.getElement('.'+this.slider_wrapper_class);
		
		if (slider_wrapper_node)
		{
			slider_wrapper_node.addEvent('mousemove',	_this.startMove.bindWithEvent(_this));
			slider_wrapper_node.addEvent('mouseleave',	_this.stopMove.bindWithEvent(_this));
		}
	},
	
	/**
	 * start move
	 * @param	void	event
	 * @return	void
	 */
	startMove: function(event)
	{
		var _this = this;
		
		// first kill timer
		if (this.timer_move) this.stopMove();
		
		// move root inner node
		this.move(event);
		
		// make sure that inner node keeps moving (on mouse stop)
		this.timer_move = setTimeout(
		function ()
		{
			_this.startMove(event);
		}
		, 0
		)
	},
	
	/**
	 * stop move
	 * @return	void
	 */
	stopMove: function()
	{
		clearTimeout(this.timer_move);
	},
	
	/**
	 * move images
	 * @param	void	event
	 * @return	void
	 */
	move: function(event)
	{
		// get coordinates of root node
		var slider_wrapper_node	= this.root_node.getElement('.'+this.slider_wrapper_class);
		var slider_node			= this.root_node.getElement('.'+this.slider_class);
		var slider_table_node	= this.root_node.getElement('table');
		
		if (slider_wrapper_node && slider_node && slider_table_node)
		{
			var root_node_coordinates	= slider_wrapper_node.getCoordinates();
			var root_node_width			= !root_node_coordinates.width.toInt() ? 0 : root_node_coordinates.width.toInt();
			var root_node_pos_left		= !root_node_coordinates.left.toInt() ? 0 : root_node_coordinates.left.toInt();
			
			var root_node_width_center	= (Math.ceil(root_node_width) / 2).toInt();
			
			// get coordinates of root inner node
			var root_inner_node_width		= !slider_node.getStyle('width').toInt() ? 0 : slider_node.getStyle('width').toInt();
			var root_inner_node_pos_left	= !slider_node.getStyle('left').toInt() ? 0 : slider_node.getStyle('left').toInt();
			
			// get coordinates of mouse
			var mouse_pos_left	= ((root_node_width_center - (event.client.x - root_node_pos_left)) * this.speed_move).toInt();
			
			// enable handlers
			var handler_right_node	= this.root_node.getElement('.'+this.handler_right_wrapper_class);
			var handler_left_node	= this.root_node.getElement('.'+this.handler_left_wrapper_class);
			
			if (handler_right_node && handler_left_node)
			{
				var handler_right_anchor_node	= handler_right_node.getElement('a');
				var handler_left_anchor_node	= handler_left_node.getElement('a');
				
				if (handler_right_anchor_node && handler_left_anchor_node)
				{
					if (mouse_pos_left < 0)
					{
						handler_right_anchor_node.set('class', this.active_class);
						handler_left_anchor_node.set('class', '');
					}
					
					else if (mouse_pos_left > 0)
					{
						handler_right_anchor_node.set('class', '');
						handler_left_anchor_node.set('class', this.active_class);
					}
					
					else if (mouse_pos_left == 0)
					{
						handler_right_anchor_node.set('class', '');
						handler_left_anchor_node.set('class', '');
					}
				}
			}
			
			
			// set new position of root inner node
			var root_inner_node_pos_left_new	= root_inner_node_pos_left + mouse_pos_left;
			
			// set limits
			root_inner_node_pos_left_new	= root_inner_node_pos_left_new >= 0 ? 0 : root_inner_node_pos_left_new;
			root_inner_node_pos_left_new	= root_inner_node_pos_left_new <= (0 - (root_inner_node_width - root_node_width)) ? (0 - (root_inner_node_width - root_node_width)) : root_inner_node_pos_left_new;
			
			// 360 degrees
			if (this.infinite_loop == 1)
			{
				//*********************************************
				// take last image and place it first in line *
				//*********************************************
				if (root_inner_node_pos_left_new >= 0)
				{
					// get all images
					var image_nodes			= $(this.root_node_id).getElements('img');
					var total_image_nodes	= image_nodes.length;
					
					// place last image node first in line
					image_nodes[total_image_nodes - 1].injectBefore(image_nodes[0]);
					
					// re-init all images
					var image_nodes			= $(this.root_node_id).getElements('img');
					var total_image_nodes	= image_nodes.length;
					
					var previous_image_node_width = 0;
					for (var a=0; a<total_image_nodes; a++)
					{
						// get coordinates
						var image_node				= image_nodes[a];
						var image_node_coordinates	= image_node.getCoordinates();
						var image_node_width		= (image_node_coordinates.width).toInt();
						var image_node_pos_left		= (image_node_coordinates.left).toInt();
						
						// set new position of image node
						image_node.setStyle('left', previous_image_node_width+'px');
						
						// set new position of root inner node
						if (a == 0) root_inner_node_pos_left_new = (root_inner_node_pos_left_new - image_node_width).toInt();
						
						// set width of previous image node
						previous_image_node_width = previous_image_node_width + image_node_width;
					}
				}
				
				//*********************************************
				// take first image and place it last in line *
				//*********************************************
				if (root_inner_node_pos_left_new <= (0 - (root_inner_node_width - root_node_width)))
				{
					// get all images
					var image_nodes			= $(this.root_node_id).getElements('img');
					var total_image_nodes	= image_nodes.length;
					
					// place last image node first in line
					image_nodes[0].injectAfter(image_nodes[total_image_nodes - 1]);
					
					// re-init all images
					var image_nodes			= $(this.root_node_id).getElements('img');
					var total_image_nodes	= image_nodes.length;
					
					var previous_image_node_width = 0;
					for (var a=0; a<total_image_nodes; a++)
					{
						// get coordinates
						var image_node				= image_nodes[a];
						var image_node_coordinates	= image_node.getCoordinates();
						var image_node_width		= (image_node_coordinates.width).toInt();
						var image_node_pos_left		= (image_node_coordinates.left).toInt();
						
						// set new position of image node
						image_node.setStyle('left', previous_image_node_width+'px');
						
						// set new position of root inner node
						if (a == (total_image_nodes - 1)) root_inner_node_pos_left_new = (root_inner_node_pos_left_new + image_node_width).toInt();
						
						// set width of previous image node
						previous_image_node_width = previous_image_node_width + image_node_width;
					}
				}
			}
			
			// slide
			slider_node.setStyles({
				'left':	root_inner_node_pos_left_new+'px'
			});
		}
	},
	
	/**
	 * create debugger
	 * @return void
	 */
	createDebugger: function()
	{
		var document_node = document.getElement('body');
		
		if (document_node)
		{
			var debugger_node = new Element('div',
			{
				'id' : this.debugger_id
			});
			
			debugger_node.inject(document_node);
		}
	}
});