ProspectDirector.Types.View.FindAnchor = Class.extend({

    init: function (options) {

        this.options = $.extend({
            selector: null,
            searchUrl: "",
            viewStateId: null
        }, options);

        this._initSearchBox();

        this._initGrid();

        this._registerEventHandlers();

    },

    /* properties */

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

    get_searchBox: function () {
        return this.get_dialog().find("[data-role=search]");
    },

    get_grid: function () {
        return this.get_dialog().find("[data-role=grid]");
    },

    get_selectionCount: function () {
        return this.get_dialog().find("[data-role=selection-count]");
    },

    get_selection: function () {
        return this.get_grid().data("selection");
    },

    get_okButton: function () {
        return this.get_dialog().find("[data-role=dialog-button][data-command=ok]");
    },

    get_cancelButton: function () {
        return this.get_dialog().find("[data-role=dialog-button][data-command=cancel]");
    },

    get_depthSelector: function () {
        return this.get_dialog().find("[data-role=depth-selector]");
    },

    /* child control event handlers */

    _okButton_onClick: function (sender, e) {

        var data = [],
            rows = this._getSelectedRows(),
            i = 0,
            partIndex = 0,
            part = [];


        for (i = 0; i < rows.length; i++) {

            parts = this._getParts(rows[i]);

            for (partIndex = 0; partIndex < parts.length; partIndex++) {

                if (data[partIndex] === undefined) {
                    data[partIndex] = [];
                }

                part = data[partIndex];

                part[part.length] = parts[partIndex];

            }

        }

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

    },

    _getParts: function (row) {

        var parts = this._getRow(row).parts,
            pieces = [],
            j = 0;

        for (j = 0; j < parts.length; j++) {
            pieces[pieces.length] = {
                id: parts[j].id,
                name: parts[j].name
            };
        }

        return pieces;

    },

    _cancelButton_onClick: function (sender, e) {
        this.get_dialog().data("dialog").hide();
    },

    _grid_onClick: function (sender, e) {

        var count = sender.target.get_selections().length,
            message = count + " item selected";

        if (count > 1) {
            message = count + " items selected";
        }

        this.get_selectionCount().text(message);

    },

    _grid_onDblClick: function (sender, e) {
        this._okButton_onClick(sender, e);
    },

    /* control initializers */

    _initSearchBox: function () {

        this.get_searchBox().searchBox({

            search: $.proxy(function (sender, e) {

                var grid = this.get_grid().data("kendoGrid");
                grid.dataSource._skip = 0;
                grid.dataSource.read();

            }, this)

        });

    },

    _initGrid: function () {

        var columns = [
            { field: "target", title: "Target", width: 200 },
            { field: "path", title: "Path", width: 500 }
        ];

        this.get_grid().kendoGrid({

            height: 300,

            selectable: null,

            resizable: true,

            reorderable: true,

            columns: columns,

            dataSource: {
                serverPaging: true,
                serverSorting: true,
                pageSize: 50,
                transport: {
                    read: {
                        url: ProspectDirector.RUNTIME.resolveUrl(this.options.searchUrl),
                        data: $.proxy(function () {
                            return {
                                viewStateId: this.options.viewStateId,
                                term: this.get_searchBox().data("searchBox").value(),
                                depth: this._getSelectedDepth()
                            };
                        }, this)
                    }
                }
            },

            sortable: {
                mode: "multiple",
                allowUnsort: true
            },

            scrollable: {
                virtual: true
            }

        });

        this.get_grid().disableTextSelection();

        this.get_grid().data("selection", new ProspectDirector.Types.Core.GridSelectionManager({
            grid: this.get_grid(),
            selectionMode: "multiple",
            click: $.proxy(this._grid_onClick, this)
        }));

        this.get_grid().on("dblclick", "TR[data-uid]", $.proxy(this._grid_onDblClick, this));
    },

    /* convenience methods */

    _getSelectedRows: function () {
        return this.get_grid().find(".k-grid-content tr.k-state-selected");
    },

    _getRow: function (rowElement) {
        return this.get_grid().data("kendoGrid").dataSource.getByUid($(rowElement).data().uid);
    },

    _getSelectedDepth: function (rowElement) {
        var depth = this.get_depthSelector().val();
        if (depth === undefined || depth === null) {
            return 0;
        }
    },

    _registerEventHandlers: function () {
        this.get_okButton().on("click", $.proxy(this._okButton_onClick, this));
        this.get_cancelButton().on("click", $.proxy(this._cancelButton_onClick, this));
    }

});