/**
 * @class	GalleryCarousel
 * @author	Jeffrey van der Veen
 */
var GalleryCarousel = new Class(
{
	/**
	 * initialize
	 * @param	string	root_id
	 * @param	integer	effect_type
	 * @param	integer	time_interval
	 * @return	void
	 */
	initialize: function(root_id, effect_type, time_interval)
	{
		// nodes
		this.root_node = root_id ? $(root_id) : null;
		
		// classes
		this.image_viewport_class		= 'mb_gallery_image_viewport';
		this.image_class				= 'mb_gallery_image';
		this.image_active_class			= 'mb_gallery_image_active';
		this.image_hold_class			= 'mb_gallery_image_hold';
		this.images_class				= 'mb_gallery_images';
		this.image_prev_class			= 'mb_gallery_image_prev';
		this.image_next_class			= 'mb_gallery_image_next';
		this.image_info_class			= 'mb_gallery_image_info';
		this.image_title_class			= 'mb_gallery_image_title';
		this.image_description_class	= 'mb_gallery_image_description';
		this.info_title_class			= 'mb_gallery_info_title';
		this.info_description_class		= 'mb_gallery_info_description';
		this.info_link_class			= 'mb_gallery_info_link';
		this.info_link_target_class		= 'mb_gallery_info_link_taget';
		this.controls_class				= 'mb_gallery_controls';
		this.control_class				= 'mb_gallery_control';
		this.control_index_class		= 'mb_gallery_control_index';
		this.blocker_class				= 'mb_gallery_blocker';
		this.active_class				= 'active';
		this.disabled_class				= 'disabled';
		
		// prefixes
		this.image_prefix	= 'mb_gallery_image_';
		this.info_prefix	= 'mb_gallery_info_';
		this.control_prefix	= 'mb_gallery_control_';
		
		// settings
		effect_type			= effect_type ? parseInt(effect_type) : 0;
		time_interval		= time_interval ? parseInt(time_interval) : 0;
		this.effect_type	= isNaN(effect_type) ? 0 : effect_type;
		this.time_interval	= isNaN(time_interval) ? 0 : time_interval;
		
		// overall
		this.timer					= null;
		this.images_amount			= 0;
		this.image_viewport_width	= 0;
	},
	
	/**
	 * start
	 * @return	void
	 */
	start: function()
	{
		var _this = this;
		
		if (this.root_node)
		{
			var images_node			= this.root_node.getElement('.'+this.images_class);
			var image_viewport_node	= this.root_node.getElement('.'+this.image_viewport_class);
			
			if (images_node)
			{
				var images_img_nodes = images_node.getElements('img');
				
				if (images_img_nodes)
				{
					this.images_amount = images_img_nodes.length;
					
					if (this.images_amount == 1)
					{
						this.stateHandler(this.image_prev_class, 0);
						this.stateHandler(this.image_next_class, 0);
					}
					else if (this.images_amount > 1)
					{
						if (this.time_interval)
						{
							this.timer = new Fx(
							{
								duration	: this.time_interval,
								onComplete	: function()
								{
									_this.showImageSibling(1);
									_this.timer.start();
								}
							});
							
							this.timer.start();
						}
					}
				}
			}
			
			if (image_viewport_node)
			{
				var image_viewport_size		= image_viewport_node.getSize();
				this.image_viewport_width	= image_viewport_size.x;
			}
		}
		
		this.setEvents();
	},
	
	/**
	 * set events
	 * @return	void
	 */
	setEvents: function()
	{
		var _this = this;
		
		if (this.root_node)
		{
			var image_prev_node	= this.root_node.getElement('.'+this.image_prev_class);
			var image_next_node	= this.root_node.getElement('.'+this.image_next_class);
			var controls_node	= this.root_node.getElement('.'+this.controls_class);
			
			if (image_prev_node && image_next_node)
			{
				image_prev_node.addEvent('click', function()
				{
					if (!this.hasClass(_this.disabled_class))
					{
						_this.showImageSibling(2);
					}
				});
				
				image_next_node.addEvent('click', function()
				{
					if (!this.hasClass(_this.disabled_class))
					{
						_this.showImageSibling(1);
					}
				});
			}
			
			if (controls_node)
			{
				var control_nodes = controls_node.getElements('.'+this.control_class);
				
				if (control_nodes)
				{
					control_nodes.addEvent('click', function()
					{
						if (!this.hasClass(_this.active_class))
						{
							var control_index_node = this.getElement('.'+_this.control_index_class);
							
							if (control_index_node)
							{
								var control_index_text	= control_index_node.get('text');
								var control_index		= parseInt(control_index_text);
								
								if (!isNaN(control_index))
								{
									var img_class = _this.image_prefix+control_index;
									
									var active_control_node	= controls_node.getElement('.'+_this.active_class);
									var direction			= 1;
									
									if (active_control_node)
									{
										var active_control_index_node = active_control_node.getElement('.'+_this.control_index_class);
										
										if (active_control_index_node)
										{
											var active_control_index_text	= active_control_index_node.get('text');
											var active_control_index		= parseInt(active_control_index_text);
											
											if (!isNaN(active_control_index))
											{
												if (control_index < active_control_index)
												{
													direction = 2;
												}
											}
										}
									}
									
									_this.showImage(img_class, direction);
									_this.showImageInfo(img_class);
								}
							}
						}
					});
				}
			}
		}
	},
	
	/**
	 * show image
	 * @param	string	img_class
	 * @param	integer	direction
	 * @return	void
	 */
	showImage: function(img_class, direction)
	{
		var _this = this;
		
		if (this.root_node && img_class)
		{
			var image_node		= this.root_node.getElement('.'+this.image_class);
			var images_node		= this.root_node.getElement('.'+this.images_class);
			var img_index		= parseInt(img_class.replace(this.image_prefix, ''));
			var image_a_node	= null;
			
			if (!isNaN(img_index))
			{
				var controls_node	= this.root_node.getElement('.'+this.controls_class);
				var info_class		= this.info_prefix+img_index;
				var info_node		= this.root_node.getElement('.'+info_class);
				
				if (controls_node)
				{
					var control_index_node = controls_node.getElement('.'+this.control_index_class+':contains('+img_index+')');
					
					if (control_index_node)
					{
						var control_node = control_index_node.getParent('.'+this.control_class);
						
						if (control_node)
						{
							if (!control_node.hasClass(this.active_class))
							{
								var active_control_node = controls_node.getElement('.'+this.active_class);
								
								if (active_control_node)
								{
									active_control_node.removeClass(this.active_class);
								}
								
								control_node.addClass(this.active_class);
							}
						}
					}
				}
				
				if (info_node)
				{
					var info_link_node = info_node.getElement('.'+this.info_link_class);
					
					if (info_link_node)
					{
						var info_link_target_node	= info_node.getElement('.'+this.info_link_target_class);
						var info_link_text			= info_link_node.get('text');
						
						image_a_node = new Element('a', {href: info_link_text});
						
						if (info_link_target_node)
						{
							var info_link_target_text = info_link_target_node.get('text');
							
							image_a_node.set('target', info_link_target_text);
						}
					}
				}
			}
			
			if (image_node && images_node)
			{
				var img_node = images_node.getElement('.'+img_class);
				
				if (img_node)
				{
					var img_node_clone			= img_node.clone();
					var img_node_clone_clone	= img_node_clone.clone();
					var image_active_node		= image_node.getElement('.'+this.image_active_class);
					var image_hold_node			= image_node.getElement('.'+this.image_hold_class);
					
					if (image_active_node)
					{
						if (this.effect_type && image_hold_node)
						{
							switch (this.effect_type)
							{
								case 1: // fade
									image_active_node.setStyle('z-index', 20);
									image_hold_node.setStyle('z-index', 10);
									
									img_node_clone.inject(image_hold_node);
									
									var effect = new Fx.Morph(image_active_node,
									{
										duration	: 500,
										transition	: Fx.Transitions.Quad.easeOut,
										onStart		: function()
										{
											_this.displayBlocker(1);
											_this.setTimer(0);
										},
										onComplete	: function()
										{
											image_active_node.set('html', null);
											img_node_clone_clone.inject(image_active_node);
											image_active_node.setStyle('opacity', 1);
											image_hold_node.set('html', null);
											
											if (image_a_node)
											{
												image_a_node.wraps(img_node_clone_clone);
											}
											
											_this.displayBlocker(0);
											_this.setTimer(1);
										}
									});
									
									effect.start(
									{
										'opacity': 0
									});
									break;
								case 2: // slide
									if (this.image_viewport_width)
									{
										var image_left_start	= 0;
										var image_left_end		= -this.image_viewport_width;
										
										direction = !isNaN(parseInt(direction)) ? direction : 1;
										
										switch (direction)
										{
											case 1: // left
												image_node.setStyle('left', 0);
												image_active_node.setStyle('left', 0);
												image_hold_node.setStyle('left', this.image_viewport_width);
												break;
											case 2: // right
												image_left_start	= -this.image_viewport_width;
												image_left_end		= 0;
												
												image_node.setStyle('left', -this.image_viewport_width);
												image_active_node.setStyle('left', this.image_viewport_width);
												image_hold_node.setStyle('left', 0);
												break;
										}
										
										img_node_clone.inject(image_hold_node);
										
										var effect = new Fx.Morph(image_node,
										{
											duration	: 500,
											transition	: Fx.Transitions.Quad.easeOut,
											onStart		: function()
											{
												_this.displayBlocker(1);
												_this.setTimer(0);
											},
											onComplete	: function()
											{
												image_active_node.set('html', null);
												img_node_clone_clone.inject(image_active_node);
												image_node.setStyle('left', image_left_start);
												image_hold_node.set('html', null);
												
												if (image_a_node)
												{
													image_a_node.wraps(img_node_clone_clone);
												}
												
												_this.displayBlocker(0);
												_this.setTimer(1);
											}
										});
										
										effect.start(
										{
											'left': image_left_end
										});
									}
									break;
							}
						}
						else
						{
							image_active_node.set('html', null);
							img_node_clone.inject(image_active_node);
							
							if (image_a_node)
							{
								image_a_node.wraps(img_node_clone);
							}
						}
					}
				}
			}
		}
	},
	
	/**
	 * show image sibling
	 * @param	integer	direction
	 * @return	void
	 */
	showImageSibling: function(direction)
	{
		if (this.root_node)
		{
			var image_active_node	= this.root_node.getElement('.'+this.image_active_class);
			var images_node			= this.root_node.getElement('.'+this.images_class);
			
			if (image_active_node && images_node)
			{
				var image_active_img_node = image_active_node.getElement('img');
				
				if (image_active_img_node)
				{
					var image_active_img_class = image_active_img_node.get('class');
					
					if (image_active_img_class)
					{
						var img_node = images_node.getElement('.'+image_active_img_class);
						
						if (img_node)
						{
							var img_sibling_node	= null;
							var img_sibling_class	= null;
							
							direction = !isNaN(parseInt(direction)) ? direction : 1;
							
							switch (direction)
							{
								case 1: // left
									img_sibling_node = img_node.getNext('img');
									
									if (!img_sibling_node)
									{
										img_sibling_class = this.image_prefix+1;
									}
									break;
								case 2: // right
									img_sibling_node = img_node.getPrevious('img');
									
									if (!img_sibling_node)
									{
										img_sibling_class = this.image_prefix+this.images_amount;
									}
									break;
							}
							
							if (img_sibling_node)
							{
								var img_sibling_class = img_sibling_node.get('class');
							}
							
							if (img_sibling_class)
							{
								this.showImage(img_sibling_class, direction);
								this.showImageInfo(img_sibling_class);
							}
						}
					}
				}
			}
		}
	},
	
	/**
	 * show image info
	 * @param	string	img_class
	 * @return	void
	 */
	showImageInfo: function(img_class)
	{
		var _this = this;
		
		if (this.root_node && img_class)
		{
			var img_index = parseInt(img_class.replace(this.image_prefix, ''));
			
			if (!isNaN(img_index))
			{
				var info_class	= this.info_prefix+img_index;
				var info_node	= this.root_node.getElement('.'+info_class);
				
				if (info_node)
				{
					var image_info_node			= this.root_node.getElement('.'+this.image_info_class);
					var image_title_node		= this.root_node.getElement('.'+this.image_title_class);
					var image_description_node	= this.root_node.getElement('.'+this.image_description_class);
					var info_title_node			= info_node.getElement('.'+this.info_title_class);
					var info_description_node	= info_node.getElement('.'+this.info_description_class);
					var info_title_text			= info_title_node ? info_title_node.get('text') : null;
					var info_description_text	= info_description_node ? info_description_node.get('text') : null;
					
					if (image_info_node)
					{
						if ((info_title_node || info_description_node) && image_info_node.hasClass(this.disabled_class))
						{
							image_info_node.removeClass(this.disabled_class);
						}
						else if (!info_title_node && !info_description_node && !image_info_node.hasClass(this.disabled_class))
						{
							image_info_node.addClass(this.disabled_class);
						}
					}
					
					if (image_title_node)
					{
						image_title_node.set('text', info_title_text);
					}
					
					if (image_description_node)
					{
						image_description_node.set('text', info_description_text);
					}
				}
			}
		}
	},
	
	/**
	 * state handler
	 * @param	string	handler_class
	 * @param	integer	state
	 * @return	void
	 */
	stateHandler: function(handler_class, state)
	{
		if (this.root_node && handler_class)
		{
			var handler_node = this.root_node.getElement('.'+handler_class);
			
			if (handler_node)
			{
				state = !isNaN(parseInt(state)) ? state : 1;
				
				switch (state)
				{
					case 0: // disable
						handler_node.addClass(this.disabled_class);
						break;
					case 1: // enable
						handler_node.removeClass(this.disabled_class);
						break;
				}
			}
		}
	},
	
	/**
	 * display blocker
	 * @param	integer	display
	 * @return	void
	 */
	displayBlocker: function(display)
	{
		if (this.root_node)
		{
			var blocker_node = this.root_node.getElement('.'+this.blocker_class);
			
			if (blocker_node)
			{
				display = !isNaN(parseInt(display)) ? display : 1;
				
				switch (display)
				{
					case 0: // hide
						blocker_node.setStyle('display', 'none');
						break
					case 1: // show
						blocker_node.setStyle('display', 'block');
						break;
				}
			}
		}
	},
	
	/**
	 * set timer
	 * @param	integer	state
	 * @return	void
	 */
	setTimer: function(state)
	{
		if (this.timer)
		{
			state = !isNaN(parseInt(state)) ? state : 1;
			
			switch (state)
			{
				case 0: // stop
					this.timer.cancel();
					break
				case 1: // start
					this.timer.start();
					break;
			}
		}
	}
});
