/// class ProspectDirector.Modules.Personalization.AddToFavoritesDialog
/// using ProspectDirector.Types.Core.Dialog

ProspectDirector.Modules.Personalization.AddToFavoritesDialog = (function (options) {

    var 

    PRIVATE = {

        //---------------------------
        //--- CONSTRUCTOR
        //---------------------------

        init: function () {

            options = $.extend({
                selector: null
            }, options);

            PRIVATE.init_dialog();
            PRIVATE.init_list();
            PRIVATE.get_dialog().find("INPUT[type=radio]").first().attr("checked", "checked");
            PRIVATE.setState();

        },

        //---------------------------
        //--- PROPERTIES
        //---------------------------

        get_dialog: function () {
            return $(options.selector);
        },

        get_form: function () {
            return this.get_dialog().find("FORM");
        },

        get_okButton: function () {
            return PRIVATE.get_dialog().find("INPUT[type=button][data-command=ok]");
        },

        get_cancelButton: function () {
            return PRIVATE.get_dialog().find("INPUT[type=button][data-command=cancel]");
        },

        get_lists: function () {
            return PRIVATE.get_dialog().find("[data-id=lists]");
        },

        get_name: function () {
            return PRIVATE.get_dialog().find("[data-id=name]");
        },

        get_radioNew: function () {
            return PRIVATE.get_dialog().find("[data-id=list-new]");
        },

        get_radioExisting: function () {
            return PRIVATE.get_dialog().find("[data-id=list-existing]");
        },

        get_entityIds: function () {
            return PRIVATE.get_dialog().find("[data-id=entity-ids]");
        },

        //---------------------------
        //--- EVENT HANDLERS
        //---------------------------

        okButton_onClick: function (e) {

            ProspectDirector.Types.Core.Notifications.action("Adding to Favorites");

            PRIVATE.get_okButton().disable();

            PD.ajax({
                type: PRIVATE.get_form().attr('method'),
                url: PRIVATE.get_form().attr('action'),
                data: PRIVATE.get_form().serialize(),
                contextData: {
                    container: PRIVATE.get_form(),
                    control: PRIVATE.get_okButton()
                },
                success: $.proxy(this.okButton_onRequestSuccess, this)
            });

        },

        okButton_onRequestSuccess: function (a, b, c) {

            var items = JSON.parse(PRIVATE.get_entityIds().val()).length;

            ProspectDirector.Types.Core.Notifications.success(items + " item(s) added to favorites.");

            this.get_dialog().data("dialog").hide({
                command: "ok",
                data: {
                    id: a.id
                }
            });

        },

        cancelButton_onClick: function (e) {
            PRIVATE.get_dialog().data("dialog").hide();
        },

        radio_onClick: function () {
            PRIVATE.setState();
        },

        dialog_onKeyPress: function (e) {
            
            if (e.keyCode === 13 && e.target.tagName !== "TEXTAREA") {
                e.preventDefault();
                this.okButton_onClick();
            }

        },

        //---------------------------
        //--- INTIALIZERS
        //---------------------------

        init_dialog: function () {

            PRIVATE.get_okButton().on("click", $.proxy(PRIVATE.okButton_onClick, PRIVATE));
            PRIVATE.get_cancelButton().on("click", $.proxy(PRIVATE.cancelButton_onClick, PRIVATE));
            PRIVATE.get_radioNew().on("click", $.proxy(PRIVATE.radio_onClick, PRIVATE));
            PRIVATE.get_radioExisting().on("click", $.proxy(PRIVATE.radio_onClick, PRIVATE));

            this.get_dialog().on("keypress", $.proxy(PRIVATE.dialog_onKeyPress, PRIVATE));

            setTimeout(function () {
                PRIVATE.get_dialog().find("SELECT,INPUT[type=text]").first().focus();
            }, 100);

        },

        init_list: function () {

            var index = 0;

            for (index; index < options.data.lists.length; index++) {

                PRIVATE.get_lists().append("<option value=\"" + options.data.lists[index].id + "\">" + options.data.lists[index].name + "</option>");

            }

        },

        //---------------------------
        //--- METHODS
        //---------------------------

        setState: function () {

            if (PRIVATE.get_radioNew().is(":checked")) {
                PRIVATE.get_lists().attr("disabled", "disabled");
                PRIVATE.get_name().removeAttr("disabled");
            }
            else {
                PRIVATE.get_name().attr("disabled", "disabled");
                PRIVATE.get_lists().removeAttr("disabled");
            }

        }

    },

    PUBLIC = {

    };

    PRIVATE.init();

    return PUBLIC;

});

ProspectDirector.Modules.Personalization.AddToFavoritesDialog.Dialog =
new (ProspectDirector.Types.Core.Dialog.extend({

    init: function () {

        this._super("AddToFavoritesDialog",
                "Add To Favorites",
                ProspectDirector.RUNTIME.resolveUrl("~/Personalization/AddToFavoritesDialog"));

    }

}));