/// class PD.Web.UI.Controls.TreeViewSelectionModule
/// using PD.Web.UI.Controls.TreeViewModule

PD.Web.UI.Controls.TreeViewSelectionModule =
PD.Web.UI.Controls.TreeViewModule.extend({

    //===================================
    // EVENT HANDLERS
    //===================================

    _onChange: function (e) {

        var tree = this.get_tree(),
            changeHandler = this.getOption("onChange", undefined),
            $node = this.get_kendo().select(),
            item = null;

        if (tree._supressChangeEvent === true) {
            tree._supressChangeEvent = false;
            return;
        }

        if ($node != null || $node.length > 0) {
            item = this.get_kendo().dataItem($node);
        }

        if (changeHandler != null) {

            changeHandler({
                item: item
            });

        }

    },

    _onSelect: function (e) {

        var tree = this.get_tree(),
            selectHandler = this.getOption("onSelect", undefined),
            $oldNode = this.get_kendo().select(),
            $newNode = e.node,
            oldItem = null,
            newItem = null;

        if ($oldNode != null || $oldNode.length > 0) {
            oldItem = this.get_kendo().dataItem($oldNode);
        }

        if ($newNode != null || $newNode.length > 0) {
            newItem = this.get_kendo().dataItem($newNode);
        }

        if (selectHandler != null) {

            e.preventDefault();
            
            setTimeout(function () {

                selectHandler({
                    oldSelection: oldItem,
                    newSelection: newItem,
                    commit: function () {
                        tree.select(newItem.id, true);
                    }
                });

            }, 25);

        }
        
    },

    //===================================
    // PUBLIC METHODS
    //===================================

    ready: function () {
        this.get_kendo().bind("change", $.proxy(this._onChange, this));
        this.get_kendo().bind("select", $.proxy(this._onSelect, this));
    }

});