﻿
Type.registerNamespace('JobScout24.Templates.Controls');

JobScout24.Templates.Controls.LoginControl = function(element)
{
	JobScout24.Templates.Controls.LoginControl.initializeBase(this, [element]);

	this._events				= null;
	this._form					= null;
	this._btnLogin				= null;
	this._hidPoppedUp			= null;
	this._modalPopupID			= null;
	this._submittedThrough		= null;
	this._tbxLoginName			= null;
	this._tbxPassword			= null;
	this._hidDestinationPageUrl	= null;

	this._btnLoginClickDelegate                    = Function.createDelegate(this, this._btnLogin_click);
	this._mpeLoginControlHiddenDelegate            = Function.createDelegate(this, this._mpeLoginControl_hidden);
	this._mpeLoginControlShownDelegate             = Function.createDelegate(this, this._mpeLoginControl_shown);
	this._webRequestManagerInvokingRequestDelegate = Function.createDelegate(this, this._webRequestManager_invokingRequest);
}

JobScout24.Templates.Controls.LoginControl.prototype =
{
	get_btnLogin : function()
	{
		return this._btnLogin;
	},

	set_btnLogin : function(value)
	{
		this._btnLogin = value;
	},

	get_events : function()
	{
		if (!this._events)
		{
			this._events = new Sys.EventHandlerList();
		}

		return this._events;
	},
	
	get_form : function()
	{
		return this._form;
	},

	set_form : function(value)
	{
		this._form = value;
	},

	get_loginName : function()
	{
		return this._tbxLoginName.value;
	},

	set_loginName : function(value)
	{
		this._tbxLoginName.value = value;
	},

	get_hidPoppedUp : function()
	{
		return this._hidPoppedUp;
	},

	set_hidPoppedUp : function(value)
	{
		this._hidPoppedUp = value;
	},

	get_modalPopupID : function()
	{
		return this._modalPopupID;
	},

	set_modalPopupID : function(value)
	{
		this._modalPopupID = value;
	},

	get_password : function()
	{
		return this._tbxPassword.value;
	},

	get_tbxPassword : function()
	{
		return this._tbxPassword;
	},

	set_tbxPassword : function(value)
	{
		this._tbxPassword = value;
	},

	get_tbxLoginName : function(value)
	{
		return this._tbxLoginName;
	},

	set_tbxLoginName : function(value)
	{
		this._tbxLoginName = value;
	},

	get_hidDestinationPageUrl: function()
	{
		return this._hidDestinationPageUrl;
	},

	set_hidDestinationPageUrl: function(value)
	{
		this._hidDestinationPageUrl = value;
	},

	get_destinationPageUrl: function()
	{
		return this._hidDestinationPageUrl.value;
	},

	set_destinationPageUrl: function(value)
	{
		this._hidDestinationPageUrl.value = value;
	},

	dispose: function()
	{
		Sys.Net.WebRequestManager.remove_invokingRequest(
				this._webRequestManagerInvokingRequestDelegate);

		var mpeLoginControl = $find(this._modalPopupID);

		if (mpeLoginControl)
		{
			mpeLoginControl.remove_hidden(this._mpeLoginControlHiddenDelegate);
			mpeLoginControl.remove_shown(this._mpeLoginControlShownDelegate);
		}

		if (this._btnLogin)
		{
			$removeHandler(this._btnLogin, 'click', this._btnLoginClickDelegate);
		}

		JobScout24.Templates.Controls.LoginControl.callBaseMethod(this, 'dispose');
	},

	hide : function()
	{
		if (!this._modalPopupID)
		{
			return;
		}

		var mpeLoginControl = $find(this._modalPopupID);

		if (mpeLoginControl)
		{
			mpeLoginControl.hide();
		}
	},

	initialize : function()
	{
		JobScout24.Templates.Controls.LoginControl.callBaseMethod(this, 'initialize');

		if (this._btnLogin)
		{
			$addHandler(this._btnLogin, 'click', this._btnLoginClickDelegate);
		}

		var mpeLoginControl = $find(this._modalPopupID);

		if (mpeLoginControl)
		{
			mpeLoginControl.add_hidden(this._mpeLoginControlHiddenDelegate);
			mpeLoginControl.add_shown(this._mpeLoginControlShownDelegate);
		}

		Sys.Net.WebRequestManager.add_invokingRequest(
				this._webRequestManagerInvokingRequestDelegate);
	},

	show : function()
	{
		if (!this._modalPopupID)
		{
			return;
		}

		var mpeLoginControl = $find(this._modalPopupID);

		if (!mpeLoginControl)
		{
			return;
		}

		mpeLoginControl.show();
	},

	_btnLogin_click : function(evt)
	{
		this._raiseEvent('login', [evt]);
		this._submittedThrough = this._btnLogin;
	},

	_mpeLoginControl_hidden : function()
	{
		if (this._hidPoppedUp)
		{
			this._hidPoppedUp.value = 'false';
		}
	},

	_mpeLoginControl_shown : function()
	{
		if (this._hidPoppedUp)
		{
			this._hidPoppedUp.value = 'true';
		}
	},

	_onLoginFailed : function(result, context, methodName)
	{
		this.set_loginName('');
		this._raiseEvent('loginFailed', [result, context, methodName]);
	},

	_onLoginSucceeded : function(result, context, methodName)
	{
		this.set_loginName('');
		this._raiseEvent('loginSucceeded', [result, context, methodName]);
	},

	_raiseEvent : function(eventName, eventArgs)
	{
		var handler = this.get_events().getHandler(eventName);

		if (handler)
		{
			if (!eventArgs)
			{
				eventArgs = Sys.EventArgs.Empty;
			}

			handler(this, eventArgs);
		}
	},

	_webRequestManager_invokingRequest : function(sender, args)
	{
//		if (this._submittedThrough != null &&
//			this._submittedThrough == this._btnLogin)
//		{
//			this._submittedThrough = null;

//			var request = args.get_webRequest();
//			//var url = request.get_url();
//			request.set_url(location.href.replace(/http:/, "https:"));
//		}
	},

	add_login : function(handler)
	{
		this.get_events().addHandler('login', handler);
	},

	remove_login : function(handler)
	{
		this.get_events().removeHandler('login', handler);
	},

	add_loginSucceeded : function(handler)
	{
		this.get_events().addHandler('loginSucceeded', handler);
	},

	remove_loginSucceeded : function(handler)
	{
		this.get_events().removeHandler('loginSucceeded', handler);
	}
}

JobScout24.Templates.Controls.LoginControl.registerClass(
		'JobScout24.Templates.Controls.LoginControl', Sys.UI.Control);

if (typeof(Sys) !== 'undefined')
{
	Sys.Application.notifyScriptLoaded();
}
