; (function (trv, $, window, document, undefined) {
    "use strict";
    
    var utils = trv.utils;
    if (!utils) {
        throw 'Missing telerikReporting.utils';
    }

    function MainMenu(dom, options) {
        var menu = $(dom).data('kendoMenu'),
            loadingFormats,
            formatsLoaded;

        if (!menu) {
            init();
        }

        function init() {
            menu = $(dom).kendoMenu().data('kendoMenu'),
            menu.bind('open', onSubmenuOpen);

            attachCommands(dom, options.model);
        }

        function onSubmenuOpen(e) {
            var $item = $(e.item);
            if ($item.children('ul[data-role=export-format-list]').length > 0) {
                onOpenFormatsMenu($item);
            }
        }

        function onOpenFormatsMenu($item) {
            if (!loadingFormats) {
                beginLoadFormats($item);
            }
        }

        function fillFormats(formats) {
            $(dom)
                .find('ul[data-role=export-format-list]')
                .each(function () {
                    var $list = $(this),
                        $parent = $list.parents('li'),
                        itemsToRemove = $list.children('li');
                    ;

                    $.each(formats, function () {
                        var format = this;
                        var li = utils.stringFormat('<li><a href="#" data-role="button" data-command="export" data-command-parameter="{name}"><span>{localizedName}</span></a></li>', format);
                        menu.append(li, $parent);
                    });

                    attachCommands($parent);
                    
                    itemsToRemove.each(function () {
                        menu.remove($(this));
                    });
                });
        }

        function beginLoadFormats($item) {
            loadingFormats = true;
            menu.append({ text: 'Loading...' }, $item);

            $.when(options.controller.getDocumentFormats())
                .done(function (formats) {
                    formatsLoaded = true;
                    loadingFormats = false;

                    window.setTimeout(function () { fillFormats(formats); }, 0);

                    menu.unbind('open', onSubmenuOpen);
                })
                .fail(function () {
                    loadingFormats = false;
                })
            ;
        }

        function attachCommands(root) {
            $(root).find('a[data-command]').each(function () {
                attachCommand(this, options.model);
            });
        }

        function attachCommand(element, model) {
            var $element = $(element),
                dataCommand = $element.attr('data-command'),
                cmd = dataCommand ? model[dataCommand] : null
            ;

            if (cmd) {
                $element
                .click(function (e) {
                    if (cmd.enabled()) {
                        cmd.exec($(this).attr('data-command-parameter'));
                    }
                    else {
                        e.preventDefault();
                    }
                });

                $(cmd)
                    .bind("enabledChanged", function (e) {
                        (cmd.enabled() ? $.fn.removeClass : $.fn.addClass).call($element.parents('li'), "k-state-disabled");
                    })
                .bind("checkedChanged", function (e) {
                    (cmd.checked() ? $.fn.addClass : $.fn.removeClass).call($element.parents('li'), "k-state-selected");
                })
                ;
            }
        }
    };
    
    var pluginName = "telerik_ReportViewer_MainMenu";

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

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