/**
 * W_EmailPdfPopup Class
 * @author Marco Troost
 */
var W_EmailPdfPopup = new Class({
	
	/**
	 * initialize
	 * @param	string	root_node_id
	 * @param	string	popup_node_id
	 * @param	integer	object_type_id
	 * @param	integer	country_id
	 * @return	void
	 */
	initialize: function(root_node_id, popup_node_id, object_id, search_for, page)
	{
		// id's
		this.root_node_id				= root_node_id;	
		this.popup_node_id				= popup_node_id;
		this.object_id					= object_id;
		this.search_for					= search_for;

		this.root_node					= $(this.root_node_id);
		this.popup_node					= $(this.popup_node_id);	
		
		this.email_btn					= $('email_pdf_btn');
		this.filter_node				= $('filter');
		this.loader_node				= $('loader');
		
		// classes
		this.hide_class					= 'hide';
		
		// propperties
		if (page)
		{
			this.page					= page;	
		}
		else {
			this.page					= 1;
		}
	},

	/**
	 * start
	 * @return	void
	 */
	start: function()
	{	
		var _this	= this;
		
		if (this.root_node_id)
		{			
			this.email_btn.addEvent('click',function()			
			{
				_this.loadPopup();
			}); 	
		}
	},

	/**
	 * load popup
	 * @return	void
	 */
	loadPopup: function()
	{
		// set vars
		var _this		= this;
		if (this.page == 2)
		{
			var content_url	= '/http/email_stats_pdf_popup.php?object_id='+this.object_id+'&search_for='+this.search_for;
		}
		else {
			var content_url	= '/http/email_pdf_popup.php?object_id='+this.object_id+'&search_for='+this.search_for;
		}
		
		
		// make request
		var http_request = new Request.HTML(
		{
			url			: content_url,
			update		: this.popup_node,
			onComplete	: function() 
			{
				_this.submitPopup();
				_this.closePopup();
			}
		});
		
		http_request.get();		
		this.popup_node.setStyle('display', 'block');
		return false;
	},
	
	/**
	 * submit popup
	 * @return	void
	 */
	submitPopup: function()
	{			
		// set vars
		var _this 					= this;

		this.submit_button_node		= $('send_email_popup_button');
		
		this.submit_button_node.addEvent('click',function()
		{
			var class_name = _this.submit_button_node.get('class');
			if (class_name != 'disabled') // only send email when classname is not 'disabled'
			{					
				var popup_email_form = $('popup_email_form');
	
				if (popup_email_form)
				{
					popup_email_form.set('send',
					{
						onRequest: function()
						{
							// show page filter and loader
							_this.filter_node.className	= 'a';
							_this.loader_node.className	= 'a';		
						},
						onSuccess: function(data)
						{
							// hide page filter and loader
							_this.filter_node.className	= _this.hide_class;
							_this.loader_node.className	= _this.hide_class;
	
							// decode JSON string and Hash it (nerd) :-P
							if (data.length) // note: no spaces in output allowed
							{
								var json_data = new Hash(JSON.decode(data));	
								// form submitted
								if (json_data)
								{
									if (json_data.has('error'))
									{
										var error = json_data.get('error');
										$('error_message').set('html', error);
									}
									else {
										$('error_message').set('html', 'error');
										// $('send_email_popup_button').setClass('display', 'none');									
										$('send_email_popup_button').className = 'disabled';
										$('send_email_popup_button').setAttribute('disabled', 'disabled');
										
									}
								}
							}
							else
							{
								// data.lengt = 0 (note: no spaces in output allowed)
								var success_text = $('mail_success').get('value');
								$('error_message').set('html', success_text);
								$('send_email_popup_button').className = 'disabled';
								$('send_email_popup_button').setAttribute('disabled', 'disabled');
							}
						}
					});
					 popup_email_form.send();
				}
				return false;
			}
		});		
	},
	
	/**
	 * close popup
	 * @return	void
	 */
	closePopup: function()
	{			
		// set vars
		var _this 					= this;
		var close_button_node		= $('close_email_popup_button');

		close_button_node.addEvent('click',function()
		{
			close_button_node.removeEvents();
			_this.popup_node.innerHTML = '';
			_this.popup_node.setStyle('display', 'none');
			return false;
		});
	}
});
