/**
 * @class	Language Selectbox
 * @author	Marco Troost
 */
var LanguageSelectbox = new Class(
{	
	/**
	 * initialize
	 * @param	string	root_node_id
	 * @return	void
	 */
	initialize: function(root_node_id, handler_id)
	{
		// nodes
		this.root_node		= !root_node_id ? this.body_node : $(root_node_id);
		this.handler_node	= $(handler_id);
		
		// id
		this.handler_id		= handler_id;
		this.root_node_id	= root_node_id;
	},
	
	/**
	 * start
	 * @return	void
	 */
	start: function()
	{		
		if (this.root_node)
		{
			// set events
			this.setEvents();
		}
	},
	
	/**
	 * set events
	 * @return	void
	 */
	setEvents: function()
	{
		// set open events
		this.openSelectbox();
	},
	
	
	/**
	 * Open selectbox
	 * @return	void
	 */
	openSelectbox: function()
	{
		// set vars
		var _this = this;
		if (this.handler_node)
		{
			this.handler_node.removeEvents();
			
			this.handler_node.addEvent('click',function()
			{
				$$('#'+_this.root_node_id+' ul').setStyles({
					overflow: 'auto',
					height: 'auto'
				});
				
				// set close events
				_this.closeSelectbox();
				
				return false;
			});
		}
	},
	
	/**
	 * close selectbox
	 * @return	void
	 */
	closeSelectbox: function()
	{
		// set vars
		var _this = this;
		
		$(this.root_node).addEvent('click',function(event)
		{
			event.stopPropagation();
		});
		
		$$('body').addEvent('click',function()
		{

			$$('#'+_this.root_node_id+' ul').setStyles({
				overflow: 'hidden',
				height: 26
			});
		});
	},
	
	
	
	/**
	 * Send form
	 * @return	void
	 */
	checkAnchors: function()
	{		
		var _this		= this;				

		// loop trough nodes
		var nodes		= this.root_node.getElements('a');
		var total_nodes	= nodes.length;
		if (total_nodes)
		{
			nodes.each(function(node, index)
			{
				anchor_href = node.get('href');
				if (anchor_href)
				{
					is_download = anchor_href.contains(_this.search_for);
					
					if (is_download)
					{
						// get file_key
						var file_key = anchor_href.substr(anchor_href.indexOf("=") + 1);
						if (file_key)
						{
							// check if cookie exist
							var cookie = Cookie.read('3M');

							if (cookie)
							{
								// change href to a page with module
								node.set('href','/html/index.php?page_id='+_this.page_id+'&file='+file_key);						
							}
							else // cookie does not exist
							{ 
								// change class and href to open the register popin
								node.set('class', 'cs_lightbox_button_open');
								node.set('href', '/http/register_form.php?file='+file_key);						
							}							
						}
					}
				}
			});
		}
	}	
});
