/**
 * @class	CS_Tabs
 * @author	Paul Kruijt
 */
var CS_Tabs = new Class({
	
	/**
	 * initialize
	 * @param	string	root_node_id
	 * @param	string	content_wrapper_id
	 * @param	boolean	use_http_request
	 * @return	void
	 */
	initialize: function(root_node_id, content_wrapper_id, use_http_request)
	{
		// nodes
		this.root_node				= $(root_node_id);
		this.content_wrapper_node	= $(content_wrapper_id);
		this.active_item_node		= null;
		
		// classes
		this.content_class		= 'cs_tabs_content';
		this.tab_listener_class	= 'cs_tab_listener';
		this.active_class		= 'cs_active';
		
		// prefixes
		this.tab_handler_prefix		= 'cs_tab_handler_';
		this.tab_listener_prefix	= 'cs_tab_listener_';
		
		// settings
		this.use_http_request	= !use_http_request ? 0 : 1;
	},
	
	/**
	 * start
	 * @return void
	 */
	start: function()
	{
		// set vars
		var _this	= this;
		
		if (this.root_node && this.content_wrapper_node)
		{
			// loop through all items and set event
			var item_nodes			= this.root_node.getElements('li');
			var total_item_nodes	= item_nodes.length;
			
			if (total_item_nodes > 0)
			{
				item_nodes.each(function(item_node, index)
				{
					var item_handler_node	= item_node.getElement('a');
					
					if (item_handler_node)
					{
						// set active item
						var item_class	= item_handler_node.get('class');
						
						if (item_class == _this.active_class && !_this.active_item_node)
						{
							_this.active_item_node = item_handler_node;
							
							// use http request to show content
							if (_this.use_http_request == 1)
							{
								var url_location = item_handler_node.get('href');
								_this.loadContent(url_location);
							}
							
							// toggle content with styles
							else
							{
								_this.toggleContent(item_handler_node);
							}
						}
						
						// set event
						_this.setHandlerEvent(item_handler_node);
					}
				});
			}
		}
	},
		
	/**
	 * set handler event
	 * @param	object	handler_node
	 * @return	void
	 */
	setHandlerEvent: function(handler_node)
	{
		// set vars
		var _this	= this;
		
		if (handler_node)
		{
			// add events
			handler_node.removeEvents();
			handler_node.addEvents(
			{
				'click' : function()
				{
					// disable current active item
					if (_this.active_item_node) _this.active_item_node.set('class', '');
					
					// set active tab
					this.set('class' , _this.active_class);
					
					_this.active_item_node = this;
					
					// use http request to show content
					if (_this.use_http_request == 1)
					{
						var url_location = this.get('href');
						_this.loadContent(url_location);
					}
					
					// toggle content with styles
					else
					{
						_this.toggleContent(this);
					}
					
					return false;
				}
			});
		}
	},
	
	/**
	 * load content with http request
	 * @param	string	url_location
	 * @return	void
	 */
	loadContent: function(url_location)
	{
		if (url_location)
		{
			var content_node = this.content_wrapper_node.getElement('.'+this.content_class);
			
			if (content_node)
			{
				// make request
				var http_request = new Request.HTML(
				{
					url		: url_location,
					update	: content_node
				});
				
				http_request.get();
			}
		}
	},
	
	/**
	 * toggle content by setting styles
	 * @param	object	tab_handler_node
	 * @return	void
	 */
	toggleContent: function(tab_handler_node)
	{
		var handler_id	= tab_handler_node.get('id');
		
		if (handler_id)
		{
			// get listener
			var connector_id = handler_id.replace(this.tab_handler_prefix, '').toInt();
			
			if (connector_id > 0)
			{
				// disable active tab
				var active_tab_listener_node = this.content_wrapper_node.getElement('.'+this.active_class);
				if (active_tab_listener_node) active_tab_listener_node.set('class', this.tab_listener_class);
				
				// enable tab
				var tab_listener_node = $(this.tab_listener_prefix + connector_id);
				if (tab_listener_node) tab_listener_node.set('class', this.active_class);
			}
		}
	}
});
