/**
 * 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.Main", {
	extend: "Ext.app.ViewController",

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

	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;
		var i_view = i_controller.getView();

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

	onLoginClick: function (f_callback) {

		var i_controller = this;
		var i_view = i_controller.getView();
		var i_form = i_view.getForm();
		var o_values = i_form.getValues();

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

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

				Ext.getBody().unmask();

				if (o_result.msgid == "IP21100") {
					f_callback(o_result);
				}
				else {

					i_controller.doAnimate(i_view);
				}
			}
		});
	},

	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();
	}
});
