/**
 * This class is the controller for the main view for the application. It is specified as
 * the "controller" of the Main view class.
 */
Ext.define("Portfolio.login.controller.Html", {
	extend: "Ext.app.ViewController",

	alias: "controller.portfolio-login-html",

	listen: {
		global: {
			logindone: {
				fn: function () {

					var i_controller = this;
					var i_view = i_controller.getView();
					i_view.destroy();
				}
			}
		}
	},

	onSpecialKey: function(i_field, i_event) {

		var i_controller = this;

		if (i_event.getKey() === i_event.ENTER) {
			i_controller.onLoginClick();
		}
	},

	onLoginClick: function(f_callback) {

		f_callback = f_callback || Ext.emptyFn;

		var i_controller = this;
		var i_view = i_controller.getView();

		var b_endding = i_view.getEndServerSession();

		if (b_endding) {
			return true;
		}

		var i_userId = Ext.get("userid");
		var i_password = Ext.get("password");

		var i_box = Ext.get("box");

		var o_values = {
			password: i_password.getValue(),
			username: i_userId.getValue()
		};

		var o_preset = Portfolio.util.Handler.baseconfig.session.preset;

		var a_state = [];

		a_state.push(o_preset.origin.toLowerCase());
		a_state.push(o_values.username.toLowerCase());

		var s_state = a_state.join("-");

		var s_deviceId = Ext.state.Manager.get(s_state);

		Ext.apply(o_values, {
			devId: s_deviceId
		});

		Portfolio.login.util.Handler.doLogin(o_values, function(o_result) {

			if (o_result.success) {
				Ext.GlobalEvents.fireEvent("login", o_result);
			}
			else {

				Ext.getBody().unmask();

				i_controller.doAnimate(i_box);
				f_callback(o_result, o_values);
			}
		});
	},

	onForgotPassword: function(f_callback) {

		f_callback = f_callback || Ext.emptyFn;

		var i_userId = Ext.get("userid");

		Ext.Ajax.request({
			method: "POST",
			url: "/ip2-services/ip2navigator.aspx",
			jsonData: {
				func: "forgottenPassword",
				userid: i_userId.getValue()
			},
			success: function(i_response) {

				var o_result = Ext.decode(i_response.responseText, true) || {};

				if (o_result.success) {
					o_result.messageId = "mailWithPasswordSent";
					f_callback(o_result);
				}
				else {
					f_callback(o_result);
				}
			}
		});
	},

	doAnimate: function(i_element) {

		var n_x = i_element.getX();

		i_element.animate({
			duration: 250,
			easing: "easeIn",
			to: {
				x: n_x - 7
			},
			listeners: {
				afteranimate: {
					fn: function() {
						n_x = n_x - 7;
					}
				}
			}
		}).animate({
			duration: 30,
			easing: "easeInOut",
			to: {
				x: n_x + 14
			},
			listeners: {
				afteranimate: {
					fn: function() {
						n_x = n_x + 14;
					}
				}
			}
		}).animate({
			duration: 30,
			easing: "easeInOut",
			to: {
				x: n_x - 14
			},
			listeners: {
				afteranimate: {
					fn: function() {
						n_x = n_x - 14;
					}
				}
			}
		}).animate({
			duration: 30,
			easing: "easeInOut",
			to: {
				x: n_x + 14
			},
			listeners: {
				afteranimate: {
					fn: function() {
						n_x = n_x + 14;
					}
				}
			}
		}).animate({
			duration: 30,
			easing: "easeInOut",
			to: {
				x: n_x - 14
			},
			listeners: {
				afteranimate: {
					fn: function() {
						n_x = n_x - 14;
					}
				}
			}
		}).animate({
			duration: 30,
			easing: "easeInOut",
			to: {
				x: n_x + 14
			},
			listeners: {
				afteranimate: {
					fn: function() {
						n_x = n_x + 14;
					}
				}
			}
		}).animate({
			duration: 30,
			easing: "easeInOut",
			to: {
				x: n_x - 14
			},
			listeners: {
				afteranimate: {
					fn: function() {
						n_x = n_x - 14;
					}
				}
			}
		}).animate({
			duration: 30,
			easing: "easeInOut",
			to: {
				x: n_x + 14
			},
			listeners: {
				afteranimate: {
					fn: function() {
						n_x = n_x + 14;
					}
				}
			}
		}).animate({
			duration: 30,
			easing: "easeInOut",
			to: {
				x: n_x - 14
			},
			listeners: {
				afteranimate: {
					fn: function() {
						n_x = n_x - 14;
					}
				}
			}
		}).animate({
			duration: 30,
			easing: "easeInOut",
			to: {
				x: n_x + 14
			},
			listeners: {
				afteranimate: {
					fn: function() {
						n_x = n_x + 14;
					}
				}
			}
		}).animate({
			duration: 30,
			easing: "easeInOut",
			to: {
				x: n_x - 14
			},
			listeners: {
				afteranimate: {
					fn: function() {
						n_x = n_x - 14;
					}
				}
			}
		}).animate({
			duration: 30,
			easing: "easeInOut",
			to: {
				x: n_x + 14
			},
			listeners: {
				afteranimate: {
					fn: function() {
						n_x = n_x + 14;
					}
				}
			}
		}).animate({
			duration: 250,
			easing: "easeOut",
			to: {
				x: n_x - 7
			},
			listeners: {
				afteranimate: {
					fn: function() {
						n_x = n_x - 7;
					}
				}
			}
		});
	},

	afteranimate: function(i_view) {

		return i_view.getPosistion();
	}
});
