﻿BenefulMap.ModalEditSpot = BenefulMap.ModalAuthenticatedForm.extend({
	init: function(modalTitle, modalSlug, modalElements, modalOptions, snippetPath, params, formElements, categoryButtons, otherCategorySuggestion, context) {
		this._super( modalTitle, modalSlug, modalElements, modalOptions, snippetPath, params, formElements);
		this.categoryButtons = modalElements.modalDiv + " " + categoryButtons;
		this.otherCategorySuggestion = modalElements.modalDiv + " " + otherCategorySuggestion;
		this.spotId;
		this.context = context;
		if (context == "detail") {
		    this.spotId = jQuery.url.attr("file").replace(".aspx", "");
		}
	},
	getContext: function() {
	   return this.context;
	},
	setContext: function(context) {
	   this.context = context;
	},
	getSpotId: function() {
	   return parseInt(this.spotId);
	},
	setSpotId: function(spotId) {
	   this.spotId = spotId;
	},
	getCategoryButtons: function() {
		return this.categoryButtons;
	},
	getOtherCategorySuggestion: function() {
		return this.otherCategorySuggestion;
	},
	showThankYou: function() {
		this.setTitle("Thank You");
		var params = {};
		params.ph = 2;
		params.id = this.getSpotId();
		this.setParams(params);
		this.populate();
	},
	redisplayFormWithMessage: function(message) {
		var params = {};
		params.ph = 1;
		params.id = this.getSpotId();
        params.message = message;
        this.setParams(params);
        this.populate();
	},
	submitFormData: function() {
		this._super();
		var self = this;
	},
	checkResult: function(pass, msg) {
        if (pass == true) {
            this.showThankYou();
        } else {
            this.redisplayFormWithMessage(msg);
        }
	},
	addModalListeners: function() {
		this._super();
		this.continueButtonListener();
		this.cancelButtonListener();
		this.validationListeners();
		this.textareaCountListener();
		$('span.star input').rating();
		this.radioButtonListeners();
		this.disableContinueButton();
	},
	submitFormData: function() {
		this._super();
		var self = this;
		var output = {};
		jQuery.each(this.getFormData(), function(key, value) {
			key = key.replace("Optional", "");
			output[key] = value;	
		});
		output.BusinessId = this.getSpotId();
		console.log("output is: ", output);
		var jsonBusinessObject = JSON.stringify(output);
		WagWorld.Web.AjaxUtility.EditSpot(jsonBusinessObject, function(result) {
			console.log(result);
			self.checkResult(result.value.Pass, result.value.Reason);
		});
	},
	radioButtonListeners: function() {
		var self = this;
		$(this.getCategoryButtons()).click( function(event) {
			if ($(this).val() == "5") {
				$(self.getOtherCategorySuggestion()).removeAttr("disabled");
			} else {
				$(self.getOtherCategorySuggestion()).attr("disabled", "disabled");
			}
		});
	},
	addModalTargetListener: function() {
		var self = this;
		$(this.targetEl).live('click', function(event) {
			event.preventDefault();
			if (self.getContext() == "account") {
			    self.setSpotId($(this).attr("rel").replace("spot",""));
			    var params = self.getParams();
			    params.id = self.getSpotId();
			    self.setParams(params);
			}
			self.setFiredTarget(this);
			self.populate();
		});
	}
});



