﻿DependUserControls.QuickPost = function(serverVars) {
	this.ServerVars = serverVars;
	this.Initialize();
};
DependUserControls.QuickPost.prototype = {
	Initialize: function() {
		var quickPostContainerSelector = '#' + serverVars.quickPostContainer;
		var quickPostService = new this.ServiceProxy('/Services/QuickPostService.asmx/');
		var intervalId, timeoutId, userId = serverVars.userId;
		var messagePanel = $('#qpMessage'), responsePanel = $('#qpResponses');
		var watermark = 'Enter Answer Here';
		var enableAnswers = serverVars.enableAnswers;

		$('#qpResponse').val(watermark);

		var randomQuickPostParams = { quickPostId: serverVars.quickPostId, femaleKey: null, maleKey: null };

		$('#qpDisplayContainerHat').hide();
		DisplayAnswers();

		function DisplayAnswers() {
			if (enableAnswers === true) {
				InvokeResponses();
				$('#qpAnswerHat').fadeOut(1000);
				SetUpTimedResponseDisplay();
			} else {
				responsePanel.hide();
				$('#divider').hide();
			}
		}

		$('#qpResponse').focus(function() {
			var response = $('#qpResponse');
			if (response.val() === watermark) {
				response.val('');
			}
		});

		$('#qpResponse').blur(function() {
			var response = $('#qpResponse');
			if (response.val() === '') {
				response.val(watermark);
			}
		});

		var isError = false;
		var quickPostError = function() {
			isError = true;

			window.clearInterval(intervalId);
			window.clearTimeout(timeoutId);

			SetMessagePanelText('There has been a server error.  Our system administrators have been notified. Please try again later.');

			timeoutId = window.setTimeout(function() {
				SetUpTimedResponseDisplay();
			}, 5000);
		};

		var isNextAvailable = true;
		$('#qpNext').click(function() {
			if (isNextAvailable) {
				isNextAvailable = false;
				window.clearInterval(intervalId);
				window.clearTimeout(timeoutId);

				var hat = $('#qpAnswerHat');

				hat.fadeIn(250, function() {
					InvokeResponses();
					hat.fadeOut(250);
					SetUpTimedResponseDisplay();
					isNextAvailable = true;
				});
			}
		});

		$('#qpSubmitResponse').click(function() {
			window.clearInterval(intervalId);
			window.clearTimeout(timeoutId);

			var response = $('#qpResponse').val();
			var gender = $('input[@name=gender]:checked').val();

			if (ValidateSubmission(response, gender)) {
				$('#qpSubmitResponse').attr('disabled', 'disabled');
				var submitParams = { quickPostId: serverVars.quickPostId, response: response, gender: gender, userId: userId };

				quickPostService.invoke('SubmitResponse', submitParams, function(result) {

					// pull the timeout out into the rest of the flow and use the if statement simply to
					// set a variable with the text to send to the page.  makes the code smaller and simpler.
					var m;
					if (result) {
						m = 'Thank you for participating! Would you like to connect with others and continue sharing your valuable viewpoint? Join our online community! <a href="/Join.aspx" ><img src="/Styles/Images/Shared/btn_JoinNow_Blue.gif" alt="Join Now" class="QPJoinNowBtn" /></a>';
					} else {
						m = 'There was an error submitting your response.  Please try again.';
					}

					SetMessagePanelText(m);
					timeoutId = window.setTimeout(function() {
						$('#qpResponse').val(watermark);

						ResetMessagePanel();
						if (enableAnswers) {
							SetUpTimedResponseDisplay();
						}
						$('#qpSubmitResponse').removeAttr('disabled');
					}, 10000);
					isError = false;
				}, quickPostError);
			} else {
				window.clearInterval(intervalId);
				SetMessagePanelText('Please enter your response, and be sure to click either the "I Am a Man" or "I Am a Woman" radio buttons.');

				timeoutId = window.setTimeout(function() {
					ResetMessagePanel();
					if (enableAnswers) {
						SetUpTimedResponseDisplay();
					}
				}, 8000);
			}
		});

		function SetMessagePanelText(text) {
			var hat = $('#qpDisplayContainerHat');

			hat.fadeIn(250, function() {
				responsePanel.hide();
				$('#qpSubmit').hide();
				$('#divider').hide();
				messagePanel.show();
				$('#qpMessageText').html(text);
				hat.fadeOut(250);
			});
		}

		function ResetMessagePanel() {
			var hat = $('#qpDisplayContainerHat');

			hat.fadeIn(250, function() {
				messagePanel.hide();
				$('#qpMessageText').text('');

				$('#qpSubmit').show();

				if (enableAnswers) {
					if (!responsePanel.is(':visible')) {
						responsePanel.show();
						$('#divider').show();
					}
				}

				hat.fadeOut(250);
			});
		}

		function ValidateSubmission(response, gender) {
			var success = true;
			if (response.length === 0 || response === watermark) {
				success = false;
			}

			if (gender === undefined) {
				success = false;
			}
			return success;
		}

		function SetUpTimedResponseDisplay() {
			var hat = $('#qpAnswerHat');

			intervalId = window.setInterval(function() {
				hat.fadeIn(250, function() {
					InvokeResponses();
					hat.fadeOut(250);
				});
			}, 5000);
		}

		function InvokeResponses(container) {
			quickPostService.invoke('GetRandomResponse', randomQuickPostParams, function(result) {
				$('#qpFemaleResponse').text(result[0].Value);
				$('#qpMaleResponse').text(result[1].Value);

				randomQuickPostParams.femaleKey = result[0].PrimaryKey;
				randomQuickPostParams.maleKey = result[1].PrimaryKey;

				if (isError) {
					ResetMessagePanel();
				}
				isError = false;
			}, quickPostError);
		}
	}
};
Depend.Extend(DependUserControls.QuickPost, Depend.Core);
