var mLangPack = "";
var systemIndex = {
	initializeBase: function() {
		$.ajax({
			url: "system.ajax/ajax.handle.language.pack.php",
			type: "POST",
			dataType: "JSON",
			data: { },
			success: function(ajaxResponse) {
				mLangPack = ajaxResponse.langPack;
				systemIndex.submitFormSignUp();
				systemIndex.submitFormSignIn();
				systemIndex.lostPasswordClick();
			}
		});
		
		$('[data-toggle="popover"]').popover();
	},
	
	initError: function(mErrorText) {
		swal("Oops!", mErrorText, "error");
	},
	
	submitFormSignUp: function () {
		$("#formSignUp").submit(function () {
			var mData = $(this).serialize();
			
			var mButton = $("#registerButton");
			var mPreviousText = mButton.val();
			mButton.val(mLangPack.index.loading);
			mButton.attr("disabled", "disabled");
			
			var mCaptcha = $("#useCaptcha").val() == "1";
			var mCanProceed = true;
			
			if(mCaptcha == true) {
				var mNumberOne = parseInt($("#n1").text());
				var mNumberTwo = parseInt($("#n2").text());
				var mResult = parseInt($("#captchaResult").val());
				var mCanProceed = (mNumberOne + mNumberTwo) == mResult;
			}
			
			if(mCanProceed) {
				$.ajax({
					url: "system.ajax/ajax.handle.submit.signup.php",
					type: "POST",
					dataType: "JSON",
					data: mData,
					success: function(jsonResponse) {
						if(jsonResponse.isValid == true) top.location = "page.timeline.php";
						else systemIndex.initError(jsonResponse.isValid);
						
						mButton.val(mPreviousText);
						mButton.removeAttr("disabled");
					}
				});
			} else {
				systemIndex.initError(mLangPack.index.captchawrong);
				mButton.val(mPreviousText);
				mButton.removeAttr("disabled");
			}
		});
	},
	
	submitFormSignIn: function () {
		$("#formSignIn").submit(function () {
			var mData = $(this).serialize();
			
			var mButton = $("#loginButton");
			var mPreviousText = mButton.val();
			mButton.val("Loading..");
			mButton.attr("disabled", "disabled");
			
			$.ajax({
				url: "system.ajax/ajax.handle.submit.signin.php",
				type: "POST",
				dataType: "JSON",
				data: mData,
				success: function(jsonResponse) {
					if(jsonResponse.isValid == true) top.location = "page.timeline.php";
					else systemIndex.initError(jsonResponse.isValid);
					
					mButton.val(mPreviousText);
					mButton.removeAttr("disabled");
				}
			});
		});
	},
	
	lostPasswordClick: function () {
		$("#lostPassword").click(function () {
			$("#modalResetPassword").modal("show");
		});
	}
};

systemIndex.initializeBase();