﻿BenefulMap.ModalAuthenticatedForm = BenefulMap.ModalForm.extend({
    init: function(modalTitle, modalSlug, modalElements, modalOptions, snippetPath, params, formElements) {
        this._super(modalTitle, modalElements, modalOptions, snippetPath, params, formElements);
        this.slug = modalSlug;
        this.checkAnchorForSlug();
    },
    getSlug: function() {
        return this.slug;
    },
    setSlug: function(slug) {
        this.slug = slug;
    },
    addModalTargetListener: function() {
        var self = this;
        $(this.targetEl).live('click', function(event) {
            event.preventDefault();
            self.checkIfUserAuthenticated();
        });
    },
    checkIfUserAuthenticated: function() {
        var self = this;
        WagWorld.Web.AjaxUtility.IsAuthenticated(function(result) {
            var isLoggedIn = result.value.Pass;
            if (isLoggedIn == true) {
                console.warn(self.getParams());
                self.setFiredTarget(this);
                self.populate();
            } else {
                self.close();
                $(document).trigger("login.open", [self.getSlug()]);
            }
        });
    },
    checkAnchorForSlug: function() {
        if (window.location.hash == "#" + this.getSlug()) {
            this.open();
        }
    },
    open: function() {
        this.checkIfUserAuthenticated();
    }
});


