oFeedbackModule = {
	sAskLink:"#smartConvoAskLink",
	sAskId:"#ask",
	sFeedbackLink:"#smartConvoFeedbackLink",
	sFeedbackId:"#feedback",
	sFeedbackModal:"#modalFeedback",
	sFeedbackForm:"#feedbackForm",
	currentAction:"",
	sMissingToFieldErrorMessage:"Please enter a valid email address below.",
	sMissingOtherFieldMessage:"Please leave a comment or suggest a question.",
	textareaMaxLength:4000,
	sMaxLengthExceededMessage:"Your comments exceed the maximum characters allowed, which is 4,000. Please edit your comments and send again.",
	
	init:function() {
		$(oFeedbackModule.sAskLink).click(function() {
			oFeedbackModule.openForm($(this).attr('href'), oFeedbackModule.sAskId);
			returnVal = false;
			return returnVal;
		});
		$(oFeedbackModule.sFeedbackLink).click(function() {
			oFeedbackModule.openForm($(this).attr('href'), oFeedbackModule.sFeedbackId);
			returnVal = false;
			return returnVal;
		});
	},
	updateCharacterCounter:function(target) {
		var value = $(target).val();
		$(target).next("p.counter").children("em").text(4000 - value.length);
	},
	openForm:function(contentURL, action) {
		//Fetches the form HTML, places it into the DOM, then opens it in a modal layer
		oFeedbackModule.currentAction = action;
		$.ajax({
			url: contentURL,
			cache: false,
			success: function(html){
				oFeedbackModule.addModal(html);
				oFeedbackModule.openContentPage();
				oFeedbackModule.initializeContentPage();
				oFeedbackModule.attachFormSubmissionHandler();
				var wtUri = oTruncate.sliceText($('.smartConversation #main h1').html(),30) + '/smart_conv/engage/feedback/start';
				var wtTitle = 'Smart Conversation - Engage Action - Feedback - Start';
			    dcsMultiTrack('DCS.dcsuri',wtUri, 'WT.ti',wtTitle,'DCSext.ev_SC_Comment','','DCSext.sc_question','','DCSext.ev_SC_SocialTag','','DCSext.sc_socialSite','','DCSext.ev_SC_Email','','DCSext.sc_description','','DCSext.ev_SC_Feedback','','DCSext.SC_VAQ_Search','','DCSext.SC_VAQ_FilterEnable','');

				oModalLayer.popup(oFeedbackModule.sFeedbackModal);
				$('.ui-dialog-overlay').css({paddingBottom:'200px'});
				
				// we have to call this after the popup has been created so that we 
				// can override some of the default dialog behavior (ie. close)
				oFeedbackModule.attachDialogCloseHandler();
			}
		});
	},
	openSuccessPage:function() {
		$(oFeedbackModule.sFeedbackModal + " .contentDialog").hide();
		$(oFeedbackModule.sFeedbackModal + " .closeDialog").hide();
		$(oFeedbackModule.sFeedbackModal + " .successDialog").show();
		var wtUri = '/smart_conv/engage/feedback/submit';
		var wtTitle = 'Smart Conversation - Engage Action - Feedback Submit';
		dcsMultiTrack('DCS.dcsuri',wtUri,'WT.ti',wtTitle,'DCSext.ev_SC_Feedback','1');
		oFeedbackModule.setDefaultDialogCloseHandler();
		oModalLayer.animateDimensions();
		oModalLayer.animatePosition();		
	},
	openClosePage:function() {
		$(oFeedbackModule.sFeedbackModal + " .contentDialog").hide();
		$(oFeedbackModule.sFeedbackModal + " .successDialog").hide();
		$(oFeedbackModule.sFeedbackModal + " .closeDialog").show();
		oFeedbackModule.setDefaultDialogCloseHandler();					
		oModalLayer.animateDimensions();
		oModalLayer.animatePosition();
	},
	openContentPage:function() {
		$(oFeedbackModule.sFeedbackModal + " .closeDialog").hide();
		$(oFeedbackModule.sFeedbackModal + " .successDialog").hide();
		$(oFeedbackModule.sFeedbackModal + " .contentDialog").show();
		oFeedbackModule.setSpecialDialogCloseHandler();
		oModalLayer.animateDimensions();
		oModalLayer.animatePosition();
	},
	addModal:function(html) {	
		// if the modal has been added before, remove it			
		if($(oFeedbackModule.sFeedbackModal)) {
			$(oFeedbackModule.sFeedbackModal).remove();
		}
			
		// add the new content into the body	
		$("body").append(html);
	},
	attachDialogCloseHandler:function() {
		// if jQuery UI Dialog had a "before" close callback, we would 
		// simply add the oFeedbackModule.openClosePage method. Since 
		// it doesn't (yet), we have to make this happen instead of 
		// letting the close callback do its job. 
		$("#modalFeedback input.closeConfirm").click(function() {
			// if jQuery UI Dialog had the callback, we would just 
			// connect this click with the dialog("close") method, 
			// which would in turn execute our callback.
			oFeedbackModule.openClosePage();
			returnVal = false;
			return returnVal;
		});

		$("#modalFeedback input.close").click(function() {
			// if jQuery UI Dialog had the callback, we would just 
			// connect this click with the dialog("close") method, 
			// which would in turn execute our callback.
			$("#modalFeedback").dialog("close");
			returnVal = false;
			return returnVal;
		});

		$(oFeedbackModule.sFeedbackModal + ' input.back').click(function() {
			// if jQuery UI Dialog had the callback, we would just 
			// connect this click with the dialog("close") method, 
			// which would in turn execute our callback.
			oFeedbackModule.openContentPage();
			returnVal = false;
			return returnVal;
		});
		
		oFeedbackModule.setSpecialDialogCloseHandler();
	},
	setDefaultDialogCloseHandler:function() {
 		// the following are overrides for close button (X) and escape 
 		// key that would normally just call dialog("close"), but we 
 		// need to override manually to go to our close page
		var xCloseDialog = function() {
				$("#modalFeedback").dialog("close");
				returnVal = false;
				return returnVal;
			};
		$("a.ui-dialog-titlebar-close").unbind('click');
		$("a.ui-dialog-titlebar-close").click(xCloseDialog);
		
		var escCloseDialog = function(e) {
				returnVal = true;
				var keyCode = e.which;
				if(keyCode === 27) {
					$("#modalFeedback").dialog("close");
					returnVal = false;
				}
				return returnVal;
			};
		$("html").unbind('keydown');
		$("html").keydown(escCloseDialog);
	},
	setSpecialDialogCloseHandler:function() {
		// the following are overrides for close button (X) and escape 
 		// key that would normally just call dialog("close"), but we 
 		// need to override manually to go to our close page
		var xCloseDialog = function() {
				oFeedbackModule.openClosePage();
				returnVal = false;
				return returnVal;
			};
		$("a.ui-dialog-titlebar-close").unbind('click');
		$("a.ui-dialog-titlebar-close").click(xCloseDialog);
		
		var escCloseDialog = function(e) {
				returnVal = true;
				var keyCode = e.which;
				if(keyCode === 27) {
					oFeedbackModule.openClosePage();
					returnVal = false;
				}
				return returnVal;
			};
		$("html").unbind('keydown');
		$("html").keydown(escCloseDialog);
	},
	attachFormSubmissionHandler:function() {
		//submit handler for the feedback form
		var options = {  
	        beforeSubmit:  oFeedbackModule.validateForm,
	        success:       oFeedbackModule.openSuccessPage 
	    };
		
		$(oFeedbackModule.sFeedbackForm).submit(function() {
			$(this).ajaxSubmit(options);
			returnVal = false;
			return returnVal;
		});
		
		// submit handler for the close form
		$("#feedbackCloseForm").submit(function() {
			var value = $("input[name='closeChoice'][checked]").val();
			if(value === 'stay') {
				oFeedbackModule.openContentPage();
			} else if(value === 'close') {
				$(oFeedbackModule.sFeedbackModal).dialog("close");
			}
			returnVal = false;
			return returnVal;
		});
	},
	initializeContentPage:function() {
		// show the feedback section specified by currentAction
		$("#ask, #feedback").addClass("collapsed");
		$(".inputSection").hide();
		$(oFeedbackModule.currentAction).removeClass("collapsed");
		$(oFeedbackModule.currentAction + " .inputSection").show();
		
		// attach character counters to the textareas
		oUtils.attachCharacterCounter("#feedback textarea",oFeedbackModule.textareaMaxLength);
		oUtils.attachCharacterCounter("#ask textarea",oFeedbackModule.textareaMaxLength);
		
		// attach togglers to the sections
		$("#modalFeedback h3").click(function(e) {
			var target = e.target;
			var parentDiv = $(target).parents('div#feedback,div#ask');
			parentDiv.toggleClass("collapsed").children(".inputSection").toggle();
			oModalLayer.animateDimensions();
		});
	},
	validateForm:function() {
		$(".addError").removeClass("addError");
		$(".error").removeClass("error");
		
		var errorMessages = new Array();
		
		$(oFeedbackModule.sFeedbackForm + " input.email").each(function() {
			var email = $(this);
			if(email && !oUtils.isValidEmail(email.val())) {
				email.parents('div.fieldPair').addClass('addError');
				errorMessages[errorMessages.length] = oFeedbackModule.sMissingToFieldErrorMessage;
			}
		});
		
		var otherFieldFilledIn = false;
		$("#feedbackForm input[type='radio'][checked], #feedbackForm textarea").each(function() {
			if($(this).val() && $(this).val().length > 0) {
				otherFieldFilledIn = true;
			}
		});
		if(!otherFieldFilledIn) {
			errorMessages[errorMessages.length] = oFeedbackModule.sMissingOtherFieldMessage;
		}
		
		$(oFeedbackModule.sFeedbackForm + " textarea").each(function() {
			var textarea = $(this);
			
			if(textarea && textarea.val() && textarea.val().length > oFeedbackModule.textareaMaxLength) {
				textarea.parents('div.fieldPair').addClass('addError');
				if(errorMessages[errorMessages.length - 1] != oFeedbackModule.sMaxLengthExceededMessage){
					errorMessages[errorMessages.length] = oFeedbackModule.sMaxLengthExceededMessage;
				}
			}
		});
		
		if(errorMessages.length > 0){
			if($(oFeedbackModule.sFeedbackForm + " .errorMessage").length === 0) {
				$(oFeedbackModule.sFeedbackForm).prepend('<div class="errorMessage"></div>');
			}
			
			var errorMessage = errorMessages[0];
			for(var i = 1; i < errorMessages.length; i++) {
				errorMessage += "<br />" + errorMessages[i];
			}
			
			$(oFeedbackModule.sFeedbackForm + ' .errorMessage').html(errorMessage);
			$(oFeedbackModule.sFeedbackForm + ' .errorMessage').fadeIn('normal');
						
			$(oFeedbackModule.sFeedbackForm + ' .addError').each(function(i){
				$(this).addClass('error');
			});
			var wtUri = '/smart_conv/engage/feedback/error';
			var wtTitle = 'Smart Conversation - Engage Action - Feedback Error';
			dcsMultiTrack('DCS.dcsuri',wtUri,'WT.ti',wtTitle);
			oModalLayer.animateDimensions();
		}

		
		return errorMessages.length === 0;
	}
};

$(document).ready(function() {
	oFeedbackModule.init();
});