/**
The module for the View Finder Options Dialog.
@class ViewFinderOptionsDialog
@namespace ProspectDirector.Modules.Views
**/
ProspectDirector.Modules.Views.ViewFinderOptionsDialog = (function (options) {

    var 

    //---------------------------
    //--- COMPONENTS
    //---------------------------

    COMPONENTS = {

        editor: null

    },

    //---------------------------
    //--- PRIVATE MEMBERS
    //---------------------------

    PRIVATE = {

        /**
        @method init
        @private
        **/
        init: function () {

            options = $.extend({
            }, options);

            COMPONENTS.editor = new ProspectDirector.Modules.Views.ViewFinderOptionsDialog.Editor(options);

        }

    },

    //---------------------------
    //--- PUBLIC MEMBERS
    //---------------------------

    PUBLIC = {
    };

    PRIVATE.init();

    return PUBLIC;

});

/**
@class Editor
@namespace ProspectDirector.Modules.Views.ViewFinderOptionsDialog
**/
ProspectDirector.Modules.Views.ViewFinderOptionsDialog.Editor = Class.extend({

    init: function (options) {

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

        this._registerEventHandlers();

    },

    //---------------------------
    //--- PUBLIC PROPERTIES
    //---------------------------

    /**
    @property get_dialog
    @private
    */
    get_dialog: function () {
        return $(this.options.dialogSelector);
    },

    /**
    @property get_form
    @private
    */
    get_form: function () {
        return this.get_dialog().find("form");
    },

    /**
    @property get_cancelButton
    @private
    */
    get_cancelButton: function () {
        return this.get_dialog().find("input[data-command=cancel]");
    },

    /**
    @property get_saveButton
    @private
    */
    get_saveButton: function () {
        return this.get_dialog().find("input[data-command=save]");
    },

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

    /**
    @property _saveButton_onClick
    @private
    */
    _cancelButton_onClick: function () {

        this.get_dialog().data("dialog").hide();

    },

    /**
    @method submit_onSuccess
    @private
    **/
    _submit_onSuccess: function (a, b, c) {

        ProspectDirector.Types.Core.Notifications.success("Find Options Updated");
        this.get_dialog().data("dialog").hide({
            command: "ok"
        });

    },

    /**
    @property _saveButton_onClick
    @private
    */
    _saveButton_onClick: function () {

        this.get_saveButton().disable();

        PD.ajax({

            type: this.get_form().attr('method'),

            url: this.get_form().attr('action'),

            data: this._packageData(),

            contextData: {
                container: this.get_form(),
                control: this.get_saveButton()
            },

            success: $.proxy(this._submit_onSuccess, this)

        });

        ProspectDirector.Types.Core.Notifications.action("Updating find options...");

    },

    _packageData: function () {

        var search = 0,
            include = 0;

        this.get_dialog().find("input[name=search]:checked").each(function (idx, el) {
            search += parseInt($(el).val());
        });

        this.get_dialog().find("input[name=include]:checked").each(function (idx, el) {
            include += parseInt($(el).val());
        });

        return {
            search: search,
            include: include,
            condense: this.get_dialog().find("input[name=condense]:checked").length > 0
        }

    },

    //---------------------------
    //--- PRIVATE METHODS
    //---------------------------

    _registerEventHandlers: function () {

        this.get_saveButton().on("click", $.proxy(this._saveButton_onClick, this));
        this.get_cancelButton().on("click", $.proxy(this._cancelButton_onClick, this));

    }

});