; (function (trv, $, window, document, undefined) {
    "use strict";

    var defaultOptions = {
    };

    function DocumentMapArea(placeholder, options) {
        options = $.extend({}, defaultOptions, options);

        var controller = options.controller;
        if (!controller) {
            throw "No controller (telerikReporting.reportViewerController) has been specified.";
        }

        var $placeholder = $(placeholder), $documentMap;

        init();

        function init() {
            $documentMap = $("<div class='rv-document-map'>");
            $documentMap.appendTo(placeholder);

            attach();
        }

        function onTreeViewSelectionChanged(e) {
            var documentMapNode = this.dataItem(e.node),
                page = documentMapNode.page,
                id = "#" + documentMapNode.id
            ;

            controller.navigateToPage(page, id);
        }

        function clearDocumentMap() {
            showDocumentMap([]);
        }

        function showDocumentMap(documentMap) {
            if (!$.isEmptyObject(documentMap)) {
                var $treeView =
                    $documentMap.data("kendoTreeView");

                if (!$treeView) {
                    $documentMap.kendoTreeView({
                        dataTextField: "text",
                        select: onTreeViewSelectionChanged,
                        dataSource: documentMap
                    });
                }
                else {
                    $treeView.setDataSource(documentMap);
                }
            }
        }

        function attach() {
            controller
                /* 
                TODO: Should be checked what the perspective is
                .beforeLoadReport(function () {
                    controller.setDocumentMapVisible({ visible: false });
                })
                */

                .beginLoadReport(function () {
                    clearDocumentMap();
                })

                .reportLoadComplete(function (event, args) {
                    if (args.documentMapAvailable) {
                        showDocumentMap(args.bookmarkNodes);
                        /*
                        TODO: Should be checked what the perspective is
                        controller.setDocumentMapVisible({ visible: true });
                        */
                    }
                })

                .error(function (event, error) {
                    clearDocumentMap();
                })

                .getDocumentMapVisible(function (event, args) {
                    args.visible = !$placeholder.hasClass("hidden");
                })

                .setDocumentMapVisible(function (event, args) {
                    (args.visible ? $.fn.removeClass : $.fn.addClass).call($placeholder, "hidden");
                })
            ;
        }
    };

    var pluginName = "telerik_ReportViewer_DocumentMapArea";

    $.fn[pluginName] = function (options) {
        return this.each(function () {
            if (!$.data(this, pluginName)) {
                $.data(this, pluginName, new DocumentMapArea(this, options));
            }
        });
    };

}(window.telerikReportViewer = window.telerikReportViewer || {}, jQuery, window, document));