/**
 * class	SparklePopup
 * author	Jeffrey van der Veen
 */
var SparklePopup = new Class({
	
	/**
	 * initialize
	 * @param	string	content_url
	 * @param	integer	width
	 * @param	integer	height
	 * @return	void
	 */
	initialize: function(content_url, width, height)
	{
		// nodes
		this.document_node		= document.getElement('body');
		this.overlay_node		= $('overlay');
		this.popup_loader_node	= $('popup_loader');
		this.popup_filter_node	= $('popup_filter');
		this.popup_node			= $('popup');
		this.content_node		= $('popup_content');
		
		// id's
		this.id_form_feedback		= 'popup_form_feedback';
		this.id_submit_file_form	= 'submit_file_form';
		
		// strings
		this.default_error_feedback	= 'Niet alle velden zijn correct ingevuld.';
		
		// classes
		this.class_close		= 'close';
		this.class_error		= 'error';
		
		// settings
		this.content_url		= content_url;
		this.width				= width;
		this.height				= height;
		
		// prefixes
		this.prefix_field	= 'field_';
		this.prefix_label	= 'label_';
	},
	
	/**
	 * set events
	 * @return	void
	 */
	setEvents: function()
	{
		var _this				= this;
		var button_close_node	= this.popup_node.getElement('.'+this.class_close);
		
		this.overlay_node.removeEvents();
		
		if (button_close_node)
		{
			button_close_node.removeEvents();
			button_close_node.addEvents(
			{
				'click' : function()
				{
					_this.hide();
					
					return false;
				}	
			});
		}
		else
		{
			this.overlay_node.addEvents(
			{
				'click' : function()
				{
					_this.hide();
					
					return false;
				}	
			});
		}
	},
	
	/**
	 * dimensions
	 * @return	void
	 */
	dimensions: function()
	{
		if (this.width && this.height)
		{
			if (this.width) this.popup_node.setStyle('width', this.width + 'px');
			if (this.height) this.popup_node.setStyle('height', this.height + 'px');
			
			// set position
			this.position();
		}
	},
	
	/**
	 * position
	 * @return	void
	 */
	position: function()
	{
		var margin_left	= Math.floor(this.width / 2).toInt();
		var margin_top	= Math.floor(this.height / 2).toInt();
		
		this.popup_node.setStyle('margin-left', '-'+margin_left+'px');
		this.popup_node.setStyle('margin-top', '-'+margin_top+'px');
	},
	
	/**
	 * load content with an Ajax request
	 * @param	string	str_params_url
	 * @return	void
	 */
	loadContent: function(str_params_url)
	{
		var _this = this;
		
		var content_url = str_params_url ? this.content_url + str_params_url : this.content_url;
		
		// make request
		var http_request = new Request.HTML({
			url			: content_url,
			update		: this.content_node,
			onRequest	: function()
			{
				// create loader
				_this.popup_loader_node.setStyle('display', 'block');
			},
			onComplete	: function()
			{
				// remove loader
				_this.popup_loader_node.setStyle('display', 'none');
				
				// show popup
				_this.popup_node.setStyle('display', 'block');
				
				// set form events
				_this.setFormEvents();
			}
		});
		
		http_request.get();
	},
	
	/**
	 * show
	 * @param	string	str_params_url
	 * @return	void
	 */
	show: function(str_params_url)
	{
		if (this.overlay_node && this.popup_node)
		{
			// set events
			this.setEvents();
			
			// set dimensions
			this.dimensions();
			
			// show
			this.overlay_node.setStyle('display', 'block');
			
			// load content
			this.loadContent(str_params_url);
		}
	},
	
	/**
	 * hide
	 * @return	void
	 */
	hide: function()
	{
		// hide
		if (this.overlay_node)		this.overlay_node.setStyle('display', 'none');
		if (this.popup_node)		this.popup_node.setStyle('display', 'none');
		if (this.popup_filter_node) this.popup_filter_node.setStyle('display', 'none');
		if (this.popup_loader_node) this.popup_loader_node.setStyle('display', 'none');
	},
	
	/**
	 * set form events
	 * @return	void
	 */
	setFormEvents: function()
	{
		// set vars
		var _this = this;
		
		// check for form data
		var form_node = this.content_node.getElement('form');
		
		if (form_node)
		{
			// get submit anchor
			var submit_node = form_node.getElement('.submit');
			
			if (submit_node)
			{
				// always remove all events
				submit_node.removeEvents();
				
				// get id of submit button
				var submit_node_id = submit_node.get('id');
				
				if (submit_node_id == this.id_submit_file_form)
				{
					submit_node.addEvents(
					{
						'click' : function()
						{
							// show loader
							_this.popup_loader_node.setStyle('display', 'block');
							
							// show filter
							_this.popup_filter_node.setStyle('display', 'block');
							
							form_node.submit();
							return false;
						}
					});
				}
				
				else
				{
					submit_node.addEvents(
					{
						'click' : function()
						{
							// send form (ajax)
							form_node.set('send',
							{
								onRequest: function()
								{
									// show loader
									_this.popup_loader_node.setStyle('display', 'block');
									
									// show filter
									_this.popup_filter_node.setStyle('display', 'block');
								},
								onSuccess: function(data)
								{
									// hide loader
									_this.popup_loader_node.setStyle('display', 'none');
									
									// hide filter
									_this.popup_filter_node.setStyle('display', 'none');
									
									// hide error feedback
									if ($(_this.id_form_feedback)) $(_this.id_form_feedback).setStyle('display', 'none');
									
									// remove error classes on labels and input fields
									_this.resetErrorFields();
									
									// decode JSON string and Hash it (nerd) :-P
									var json_data = new Hash(JSON.decode(data));
									
									// error
									if (json_data.has('error'))
									{
										var error_fields		= json_data.get('error');
										var total_error_fields	= error_fields.length;
										
										if (total_error_fields > 0)
										{
											// set error feedback and display
											if ($(_this.id_form_feedback))
											{
												var error_feedback = _this.default_error_feedback;
												
												if (json_data.has('feedback'))
												{
													error_feedback = json_data.get('feedback');
												}
												
												$(_this.id_form_feedback).innerHTML = error_feedback;
												
												$(_this.id_form_feedback).setStyle('display', 'block');
											}
											
											error_fields.each(function(error_field)
											{
												// get nodes
												var label_error_node = $(_this.prefix_label + error_field);
												//var field_error_node = $(_this.prefix_field + error_field);
												
												// set error classes
												if (label_error_node) label_error_node.set('class', _this.class_error);
												//if (field_error_node) field_error_node.addClass(_this.class_error);
											});
										}
									}
									
									// form success
									else
									{
										if (json_data.has('content_html'))
										{
											var content_html				= json_data.get('content_html');
											_this.content_node.innerHTML	= content_html;
											
											// set form events
											_this.setFormEvents();
										}
										
										else
										{
											// hide popup
											_this.hide();
											
											if (json_data.has('success'))
											{
												var redirect = '';
												if (json_data.has('redirect'))
												{
													redirect = json_data.get('redirect');
												}
												
												var success_feedback = json_data.get('success');
												form_alert.show(success_feedback, 3000, redirect);
											}
										}
									}
								}
							});
							
							form_node.send();
							
							return false;
						}
					});
				}
			}
		}
	},
	
	/**
	 * reset error fields
	 * @return	void
	 */
	resetErrorFields: function()
	{
		// set vars
		var _this				= this;
		var label_nodes			= this.content_node.getElements('label');
		var total_label_nodes	= label_nodes.length;
		var input_nodes			= this.content_node.getElements('input');
		var total_input_nodes	= input_nodes.length;
		
		if (total_label_nodes > 0)
		{
			label_nodes.each(function(label_node)
			{
				label_node.removeClass(_this.class_error);
			});
		}
		
		if (total_input_nodes > 0)
		{
			input_nodes.each(function(input_node)
			{
				input_node.removeClass(_this.class_error);
			});
		}
	}
});
