/**
 * @class	vacancie_detail
 * @author	Paul Kruijt
 */
var Vacancie_detail = new Class({

	/**
	 * initialize
	 * @param	string	handler_id
	 * @param	string	listener_id
	 * @param	string	http_url
	 * @param	unknown	handler_value
	 * @param	string	event_type
	 * @return	void
	 */
	initialize: function(root_node)
	{
		// nodes
		this.root_node		= $(root_node);
		this.listener_node	= $('vacancie_container');	
		
		// strings
		this.job_prefix		= 'job_';

	},

	/**
	 * start
	 * @return	void
	 */
	start: function()
	{
		if (this.root_node)
		{
			// set events			
			
			this.checkUrl();
			
			this.getDetail();

		}
	},


	
	/**
	 * get detail
	 * @param	unknown	handler_value
	 * @return	void
	 */
	getDetail: function()
	{
		var _this = this;

		// get anchors
		var anchor_nodes		= _this.root_node.getElements('.title span a');
		var total_anchor_nodes	= anchor_nodes.length;
		if (total_anchor_nodes)
		{
			anchor_nodes.each(function(anchor_node, index)
			{
				anchor_node.removeEvents();
				anchor_node.addEvents(
				{
					'click' : function()
					{		
						job         = this.get('class');
						job_id		= job.replace(_this.job_prefix, '');
						
						_this.getData(job_id);
						return false;
					}
				});
			});
		}
		
		// get anchors
		var anchor_nodes		= _this.root_node.getElements('.more span a');
		var total_anchor_nodes	= anchor_nodes.length;
		if (total_anchor_nodes)
		{
			anchor_nodes.each(function(anchor_node, index)
			{
				anchor_node.removeEvents();
				anchor_node.addEvents(
				{
					'click' : function()
					{
						job         = this.get('class');
						job_id		= job.replace(_this.job_prefix, '');
						
						_this.getData(job_id);
						return false;
					}
				});
			});
		}
	},
	
	/**
	 * to overview
	 * @param	unknown	handler_value
	 * @return	void
	 */
	toOverview: function()
	{
		var _this = this;
		
		anchor_node		= $('back_to_overview');
		listener_node	= $('vacancie_container');
		
		if (anchor_node)
		{
			anchor_node.removeEvents();
			anchor_node.addEvents(
			{
				'click' : function()
				{
					_this.getData(0);
					return false;
				}
			});					
		}
		
		_this.jobApplication();
	},
	
	
	/**
	 * check url
	 * @param	unknown	handler_value
	 * @return	void
	 */

	checkUrl: function()
	{
		var _this = this;
		
		var job_id = this.getUrl('#');
		
		if (job_id)
		{
			job_node		= $('job_application');
			listener_node	= $('vacancie_container');			
			http_url		= '/http/yellowyard/index.php';


			// make request
			var http_request = new Request.HTML({			
				url			: http_url,
				update		: listener_node,			
				onRequest	: function()
				{

				},
				onComplete	: function()
				{
					// set complete events
					anchor_node		= $('back_to_overview');

					if (anchor_node)
					{
						anchor_node.removeEvents();
						anchor_node.set('href', '/kandidaten/vacature-overzicht/');	
					}
				}
			});

			http_request.get({'menu' : 'job', 'id' : job_id});

			return false;				
		}
			

	},
	
	getUrl: function(key, url)
	{
		var _this = this;

		if(arguments.length < 2) url =location.href;
		if(arguments.length > 0 && key != ""){
			if(key == "#"){
				var regex = new RegExp("[#]([^$]*)");
			} else if(key == "?"){
				var regex = new RegExp("[?]([^#$]*)");
			} else {
				var regex = new RegExp("[?&]"+key+"=([^&#]*)");
			}
			var results = regex.exec(url);
			return (results == null )? "" : results[1];
		} else {
			url = url.split("?");
			var results = {};
				if(url.length > 1){
					url = url[1].split("#");
					if(url.length > 1) results["hash"] = url[1];
					url[0].split("&").each(function(item,index){
						item = item.split("=");
						results[item[0]] = item[1];
					});
				}
			return results;
		}
	},	
	
	/**
	 * get jobApplication
	 * @param	unknown	handler_value
	 * @return	void
	 */
	jobApplication: function()
	{
		var _this = this;
		
		job_node		= $('job_application');
		listener_node	= $('vacancie_container');
		if (job_node)
		{
			job_node.removeEvents();
			job_node.addEvents(
			{
				'click' : function()
				{					
					http_url = '/http/yellowyard/index.php';
					job_id	= this.get('href');

					// make request
					var http_request = new Request.HTML({			
						url			: http_url,
						update		: listener_node,			
						onRequest	: function()
						{

						},
						onComplete	: function()
						{
							// set complete events
							//_this.setCompleteEvents();
						}
					});

					http_request.get({'menu' : 'apply', 'id' : job_id});

					return false;
				}
			});					
		}		

	},

	/**
	 * get data
	 * @param	unknown	handler_value
	 * @return	void
	 */
	getData: function(job_id)
	{
		var _this = this;
		http_url = '/http/yellowyard/index.php';

		// make request
		var http_request = new Request.HTML({			
			url			: http_url,
			update		: this.listener_node,			
			onRequest	: function()
			{

			},
			onComplete	: function()
			{
				// set complete events
				//_this.setCompleteEvents();
			}
		});

		http_request.get({'menu' : 'job', 'id' : job_id});
	}
});
