/**
 * W_SubmitLogin Class
 * @author Marco Troost
 */
var W_SubmitLogin = 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)
	{
		// 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_login_homepage');
		this.loader_node					= $('loader_login_homepage');
		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				= $('login_submit_homepage');
		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.checkLogin();
				return false;
			});
		}
	},

	/**
	 * submit forgot password form
	 * @return	void
	 */
	checkLogin: function()
	{
		// set var as object
		var _this	= this;

		if (this.button_login_node)
		{
				// send form (ajax)
				$('stats_login_form_homepage').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
							{
								_this.loadPopup();
							}
							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_homepage').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;
		});
	}
	


});
