ContactFormController.prototype = new PastelBaseController();
ContactFormController.prototype.constructor = ContactFormController;

/**
 * @constructor
 * @base PastelBaseController
 */
function ContactFormController() {
	// options ====================================================================================
	this.setOpts({
		viewClass: ContactFormView,
		viewSelector: "div.id_contact_page_cont",
		wrapperSelector: "div.left",
		viewRemoveOnHide: true,
		ajaxSingleRequest: false,
		ajaxUrl: "/index/contact/"
	});

	// setup ======================================================================================
	/** @param {ContactFormView} view */
	this._setupView = function(view) {
		this._setupClickHandler(view);
	};
	
	/** @param {ContactFormView} view */
	this._setupClickHandler = function(view) {
		view.contactForm().click($.proxy(function(event) {
			event.preventDefault();
			var $target = $(event.target);

			if (view.isReloadCaptchaBtn($target)) {
				view.changeCaptchaImg();
			} else if (view.isSubmitBtn($target)) {
				if (!view.validate()) {
					return;
				}

				var jsonData = view.getJsonData();
				this.ajax({
					url: "/index/sendemail/",
					data: {jsonData: jsonData},
					success: function(data) {
						data = $.evalJSON(data);
						view.changeCaptchaImg();
						if (data.status === "wrong_security_code") {
							var info = {
									title: translate('Wrong_security_code'),
									text: translate('Entered_security_code_wrong'),
									buttons: ["ok"]
								};
								_APP.alertManager.show(info);
						} else if (data.status === "success") {
							view.name("set", "");
							view.email("set", "");
							view.message("set", "");
							var info = {
								title: translate('Information'),
								text: translate('Email_sent_successfully'),
								buttons: ["ok"]
							};
							_APP.alertManager.show(info);
						}
					},
					error: function() {
						view.changeCaptchaImg();
						var info = {
							title: translate('Error'),
							text: translate('Email_cant_be_sent'),
							buttons: ["ok"]
						};
						_APP.alertManager.show(info);
					}
				}, this);
				
			}
		}, this));
	};
 
}

