/**
 * W_ShowLogin Class
 * @author Marco Troost
 */
var W_ShowLogin = new Class({
	
	/**
	 * initialize
	 * @param	string	root_node_id
	 * @param	string	overlay_node_id
	 * @param	string	popup_node_id
	 * @return	void
	 */
	initialize: function(root_node_id, overlay_node_id, popup_node_id, item_id)
	{
		// id's
		this.overlay_node_id				= overlay_node_id;
		this.popup_node_id					= popup_node_id;
		this.root_node_id					= root_node_id;		
		
		this.filter_node					= $('filter_stats');
		this.loader_node					= $('loader_stats');
		this.hide_class						= 'hide';

		this.root_node						= $(this.root_node_id);	
		this.popup_node						= $(this.popup_node_id);	
		this.overlay_node					= $(this.overlay_node_id);
		this.close_button_node				= $('close_popup_button');
		this.input_email_node				= $('login_input_email');
		this.input_password_node			= $('login_input_password');
		this.button_login_node				= $('menuitem_'+item_id);
		this.button_submit_node				= $('login_submit');
		this.button_forgot_password_node	= $('forgot_password_submit');
		this.content_url 					= '/http/customer_manager.php';
	},

	/**
	 * start
	 * @return	void
	 */
	start: function()
	{
		if (this.root_node_id)
		{
			this.setEvents();			
		}
	},

	/**
	 * set events
	 * @return	void
	 */
	setEvents: function()
	{
		// set var as object
		var _this	= this;
		
		if (this.button_login_node)
		{
			this.button_login_node.removeEvents();
			this.button_login_node.addEvent('click',function()
			{
				_this.loadPopup();
			});
		}
	},
	
	
	/**
	 * submit login form
	 * @return	void
	 */
	submitLoginForm: function()
	{
		// set var as object
		var _this	= this;
		
		if (this.button_submit_node)
		{
			// on click submitbutton 
			this.button_submit_node.removeEvents();
			this.button_submit_node.addEvent('click',function()
			{
				_this.sendLoginForm();
			});
			
			// on keyup enter-key password inputbox
			this.input_password_node.removeEvents();
			this.input_password_node.addEvent('keyup', function(event)
			{
				 if (event.key == "enter") 
				 {
					 _this.sendLoginForm();
				 }
			});
			
			// on keyup enter-key email inputbox
			this.input_email_node.removeEvents();
			this.input_email_node.addEvent('keyup', function(event)
			{		
				if (event.key == "enter")
				{
					_this.sendLoginForm();
				}
			});
		}
	},
	
	/**
	 * send login form
	 * @return	void
	 */
	sendLoginForm: function()
	{
		// set var as object
		var _this	= this;
		
		if (this.button_submit_node)
		{
			// send form (ajax)
			$('stats_login_form').set('send',
			{
				onRequest: function()
				{
					// show page filter and loader
					_this.filter_node.className	= '';
					_this.loader_node.className	= '';
				},
				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
					var json_data = new Hash(JSON.decode(data));
					if (json_data.has('status'))
					{
						var status	= json_data.get('status');

						if(status == 1) // ingelogd
						{
							// make request
							var http_request = new Request.HTML(
							{
								url			: '/http/customer_manager.php',
								update		: $('popup_content'),
								onComplete	: function() {
									_this.closePopup();
								}
							});

							http_request.get();

						}
						else if (status == 2) // error message
						{
							if (json_data.has('message'))
							{
								var message	= json_data.get('message');
								$('loging_error_message').set('text', message);
							}
						}
					}
				}
			});

			$('stats_login_form').send();
		}
	},

	/**
	 * submit forgot password form
	 * @return	void
	 */
	submitForgotPasswordForm: function()
	{
		// set var as object
		var _this	= this;
		if (this.button_forgot_password_node)
		{
			this.button_forgot_password_node.removeEvents();
			this.button_forgot_password_node.addEvent('click',function()
			{
				// send form (ajax)
				$('stats_forgot_password_form').set('send',
				{
					onRequest: function()
					{
						// show page filter and loader
						_this.filter_node.className	= '';
						_this.loader_node.className	= '';
					},
					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
						var json_data = new Hash(JSON.decode(data));
						if (json_data.has('status'))
						{
							var status	= json_data.get('status');

							if(status == 1) // mail send
							{
								if (json_data.has('message'))
								{
									var message	= json_data.get('message');
									$('loging_error_message').set('text', message);

									$('stats_forgot_password_form').set('style', 'display:none;');
								}
							}
							else if (status == 2) // error message
							{
								if (json_data.has('message'))
								{
									var message	= json_data.get('message');
									$('loging_error_message').set('text', message);
								}
							}
						}
					}
				});

				$('stats_forgot_password_form').send();

			});
		}
	},

	
	
	/**
	 * load popup
	 * @return	void
	 */
	loadPopup: function()
	{
		// set vars
		var _this = this;

		// make request
		var http_request = new Request.HTML({
			url			: this.content_url,
			update		: this.popup_node,
			onComplete	: function() {
				_this.closePopup();
			}
		});
		http_request.get();

		this.overlay_node.setStyle('display', 'block');
		this.popup_node.setStyle('display', 'block');

		if (Browser.Engine.trident4) // IE6
		{
			$('text_container').getElements('select').setStyle('display', 'none');
		}
		return false;
	},

	/**
	 * close popup
	 * @return	void
	 */
	closePopup: function()
	{
		// set vars
		var _this = this;

		$('close_popup_button').addEvent('click',function()
		{
			$('popup_content').innerHTML = '';
			$('overlay').setStyle('display', 'none');
			$('popup_content').setStyle('display', 'none');

			if (Browser.Engine.trident4) // IE6
			{
				$('text_container').getElements('select').setStyle('display', 'block');
			}
			return false;
		});
	}

});
