/**
 * @class	CS_GalleryBasic
 * @author	Paul Kruijt
 */
var CS_GalleryBasic = new Class({
	
	/**
	 * initialize
	 * @param	string	root_node_id
	 * @param	integer	time_interval
	 * @param	integer	effect_type
	 * @return	void
	 */
	initialize: function(root_node_id, effect_type)
	{
		// nodes
		this.root_node				= !root_node_id ? document.getElement('body') : $(root_node_id);
		this.active_thumbnail_node	= null;
		
		// classes
		this.gallery_wrapper_class					= 'cs_gallery_basic_wrapper';
		this.gallery_class							= 'cs_gallery_basic';
		this.gallery_main_wrapper_class				= 'cs_gallery_basic_main_wrapper';
		this.gallery_main_class						= 'cs_gallery_basic_main';
		this.gallery_main_image_wrapper_class		= 'cs_gallery_basic_main_image_wrapper';
		this.gallery_main_image_class				= 'cs_gallery_basic_main_image';
		this.gallery_main_text_wrapper_class		= 'cs_gallery_basic_main_text_wrapper';
		this.gallery_main_text_class				= 'cs_gallery_basic_main_text';
		this.gallery_thumbnails_wrapper_class		= 'cs_gallery_basic_thumbnails_wrapper';
		this.gallery_thumbnails_class				= 'cs_gallery_basic_thumbnails';
		this.gallery_thumbnail_wrapper_class		= 'cs_gallery_basic_thumbnail_wrapper';
		this.gallery_thumbnail_class				= 'cs_gallery_basic_thumbnail';
		this.gallery_thumbnail_hover_class			= 'cs_gallery_basic_thumbnail_hover';
		this.gallery_thumbnail_active_class			= 'cs_gallery_basic_thumbnail_active';
		this.gallery_thumbnail_image_class			= 'cs_gallery_basic_thumbnail_image';
		this.gallery_thumbnail_text_class			= 'cs_gallery_basic_thumbnail_text';
		this.gallery_hidden_image_class				= 'cs_gallery_basic_hidden_image';
		this.gallery_hidden_text_class				= 'cs_gallery_basic_hidden_text';
		this.gallery_hidden_link_class				= 'cs_gallery_basic_hidden_link';
		this.gallery_paging_wrapper_class			= 'cs_gallery_basic_paging_wrapper';
		this.gallery_paging_class					= 'cs_gallery_basic_paging';
		this.gallery_paging_previous_wrapper_class	= 'cs_gallery_basic_paging_previous_wrapper';
		this.gallery_paging_previous_class			= 'cs_gallery_basic_paging_previous';
		this.gallery_paging_previous_disabled_class	= 'cs_gallery_basic_paging_previous_disabled';
		this.gallery_paging_next_wrapper_class		= 'cs_gallery_basic_paging_next_wrapper';
		this.gallery_paging_next_class				= 'cs_gallery_basic_paging_next';
		this.gallery_paging_next_disabled_class		= 'cs_gallery_basic_paging_next_disabled';
		
		// settings
		this.effect_type		= !effect_type ? 0 : effect_type.toInt(); /* default no effect */
		this.effect_interval	= 500;
		this.total_items		= this.getTotalItems();
		
		// overall
		this.gallery_play	= null;
	},
	
	/**
	 * start
	 * @return	void
	 */
	start: function()
	{
		if (this.root_node)
		{
			// disable paging
			if (this.total_items < 2)
			{
				this.disablePaging();
			}
			
			// set events
			this.setEvents();
			
			// set first item active
			this.activateItem();
		}
	},
	
	/**
	 * get total items
	 * @return	integer	total_items
	 */
	getTotalItems: function()
	{
		var total_items = 0;
		
		if (this.root_node)
		{
			var thumbnail_nodes			= this.root_node.getElements('.'+this.gallery_thumbnail_wrapper_class);
			var total_thumbnail_nodes	= thumbnail_nodes.length;
			
			if (total_thumbnail_nodes > 0) total_items = total_thumbnail_nodes;
		}
		
		return total_items;
	},
	
	/**
	 * disable paging
	 * @return	void
	 */
	disablePaging: function()
	{
		if (this.root_node)
		{
			var paging_node = this.root_node.getElements('.'+this.gallery_paging_wrapper_class);
			
			if (paging_node) paging_node.setStyle('display', 'none');
		}
	},
	
	/**
	 * set events
	 * @return	void
	 */
	setEvents: function()
	{
		// set vars
		var _this	= this;
		
		// get thumbnails and set events
		var thumbnail_nodes			= this.root_node.getElements('.'+this.gallery_thumbnail_wrapper_class);
		var total_thumbnail_nodes	= thumbnail_nodes.length;
		
		if (total_thumbnail_nodes > 0)
		{
			thumbnail_nodes.each(function(thumbnail_node)
			{
				thumbnail_node.addEvents(
				{
					'mouseover': function()
					{
						var thumbnail_inner_node = this.getElement('.'+_this.gallery_thumbnail_class);
						
						if (thumbnail_inner_node)
						{
							thumbnail_inner_node.set('class', _this.gallery_thumbnail_hover_class);
						}
					},
					
					'mouseout': function()
					{
						var thumbnail_inner_node = this.getElement('.'+_this.gallery_thumbnail_hover_class);
						
						if (thumbnail_inner_node)
						{
							thumbnail_inner_node.set('class', _this.gallery_thumbnail_class);
						}
					},
					
					'mousedown': function()
					{
						_this.activateItem(this);
					}
				});
				
				var thumbnail_anchor_node = thumbnail_node.getElement('a');
				
				if (thumbnail_anchor_node)
				{
					thumbnail_anchor_node.addEvents(
					{
						'click': function()
						{
							return false;
						}
					});
				}
			});
			
			// paging events
			if (this.total_items > 1)
			{
				var paging_previous_node	= this.root_node.getElement('.'+this.gallery_paging_previous_wrapper_class);
				var paging_next_node		= this.root_node.getElement('.'+this.gallery_paging_next_wrapper_class);
				
				if (paging_previous_node)
				{
					var previous_anchor_node = paging_previous_node.getElement('a');
					
					if (previous_anchor_node)
					{
						previous_anchor_node.addEvents(
						{
							'click': function()
							{
								var item_node = _this.getPreviousItem();
								
								_this.activateItem(item_node);
								
								return false;
							}
						});
					}
				}
				
				if (paging_next_node)
				{
					var next_anchor_node = paging_next_node.getElement('a');
					
					if (next_anchor_node)
					{
						next_anchor_node.addEvents(
						{
							'click': function()
							{
								var item_node = _this.getNextItem();
								
								_this.activateItem(item_node);
								
								return false;
							}
						});
					}
				}
			}
		}
	},
	
	/**
	 * activate item
	 * @param	object	item_node
	 * @return	void
	 */
	activateItem: function(item_node)
	{
		// set vars
		var _this	= this;
		
		// get next item
		if (!item_node)
		{
			item_node = this.getNextItem();
		}
		
		if (item_node)
		{
			var thumbnail_inner_node = item_node.getElement('div');
			
			if (thumbnail_inner_node)
			{
				// de-activate active item
				if (this.active_thumbnail_node) this.active_thumbnail_node.set('class', this.gallery_thumbnail_class);
				
				// set new active item
				thumbnail_inner_node.set('class', this.gallery_thumbnail_active_class);
				
				this.active_thumbnail_node = thumbnail_inner_node;
				
				// set item content
				var item_image_node	= item_node.getElement('.'+this.gallery_hidden_image_class);
				var item_text_node	= item_node.getElement('.'+this.gallery_hidden_text_class);
				
				// set main image
				if (item_image_node)
				{
					var main_image_node	= this.root_node.getElement('.'+this.gallery_main_image_class);
					
					if (main_image_node)
					{
						switch (this.effect_type)
						{
							// fade
							case 1:
							
								var fade_out_effect = new Fx.Morph(main_image_node, {duration: this.effect_interval, transition: Fx.Transitions.Quad.easeOut});
								
								fade_out_effect.start({
									'opacity': 0
								}).chain(function()
								{
									var item_image_content	= item_image_node.get('html');
									main_image_node.set('html', item_image_content);
									
									var fade_in_effect = new Fx.Morph(main_image_node, {duration: _this.effect_interval, transition: Fx.Transitions.Quad.easeOut});
									
									fade_in_effect.start({
										'opacity': 1
									});
								});
							
							break;
							
							// no effect
							default:
							
								var item_image_content	= item_image_node.get('html');
								main_image_node.set('html', item_image_content);
								
							break;
						}
					}
				}
				
				// set main text
				if (item_text_node)
				{
					var main_text_node	= this.root_node.getElement('.'+this.gallery_main_text_class);
					
					if (main_text_node)
					{
						var item_text_content		= item_text_node.get('html');
						main_text_node.innerHTML	= item_text_content;
					}
				}
			}
		}
	},
	
	/**
	 * link item
	 * @param	object	item_node
	 * @return	void
	 */
	linkItem: function(item_node)
	{
		if (item_node)
		{
			var item_link_node	= item_node.getElement('.'+this.gallery_item_link_class);
			
			// link to page
			if (item_link_node)
			{
				var item_link_content	= item_link_node.get('text');
				
				if (item_link_content != '')
				{
					document.location	= item_link_content;
				}
			}
		}
	},
	
	/**
	 * get next item
	 * @return	object	item_node
	 */
	getNextItem: function()
	{
		var item_node;
		
		// get items
		var thumbnail_nodes			= this.root_node.getElements('.'+this.gallery_thumbnail_wrapper_class);
		var total_thumbnail_nodes	= thumbnail_nodes.length;
		
		if (total_thumbnail_nodes > 0)
		{
			// check if an item is active (if not...set first item to active)
			if (!this.active_thumbnail_node)
			{
				item_node = thumbnail_nodes[0];
			}
			else
			{
				item_node = this.active_thumbnail_node.getParent().getNext();
				
				// get first item
				if (!item_node || item_node.get('tag') == 'br') item_node = thumbnail_nodes[0];
			}
		}
		
		return item_node;
	},
	
	/**
	 * get previous item
	 * @return	object	item_node
	 */
	getPreviousItem: function()
	{
		var item_node;
		
		// get items
		var thumbnail_nodes			= this.root_node.getElements('.'+this.gallery_thumbnail_wrapper_class);
		var total_thumbnail_nodes	= thumbnail_nodes.length;
		
		if (total_thumbnail_nodes > 0)
		{
			item_node = this.active_thumbnail_node.getParent().getPrevious();
			
			// get first item
			if (!item_node) item_node = thumbnail_nodes[total_thumbnail_nodes - 1];
		}
		
		return item_node;
	}
});
