(function () {
    var mobile = window.mobile = {};
    var popupMenu = null;
    var $itemMenu = null;

    mobile.initialize = function (nav) {
        if (nav === "mobile") {
            $(".breadcrumb").delegate(".link-item", "click", breadcrumbItemClicked);
        }

        $itemMenu = $(".item.menu");

        $('.rmRootToggle').on("click", function () {
            var open = $itemMenu.hasClass("expanded");
            if (!open)
                $itemMenu.addClass("expanded");
            else
                $itemMenu.removeClass("expanded");
        });
    };

    mobile.popupMenuLoad = function (sender, args) {
        popupMenu = sender;
    };

    mobile.popupMenuClicked = function (sender, args) {
        var item = args.get_item();
        var id = item.get_value().split('|')[0];
        var text = getFolderText(item.get_text());
        var breadcrumbItems = [];

        // clear all breadcrumbs
        $(".item.link-item").remove();

        while (item !== sender) {
            breadcrumbItems.push(createBreadcrumbItem(item));
            item = item.get_parent();
        }

        for (var i = breadcrumbItems.length; i--;) {
            $(".breadcrumb").append(breadcrumbItems[i]);
        }

        $(".item.link-item").last().addClass("selected");

        if (typeof mail === 'object') {
            mail.filterGrid(parseInt(id));
        }

        if (typeof notes === "object") {
            notes.filterGrid(text);
        }

        if (popupMenu._opened) {
            closePopupMenu();
        }
    };

    mobile.toggleMenu = function () {
        var open = $itemMenu.hasClass("expanded");

        if (!open) {
            openPopupMenu();
        } else {
            closePopupMenu();
        }
    }

    function openPopupMenu() {
        $itemMenu.addClass("expanded");
        popupMenu._positionPopUpContainer();
        popupMenu._animatePopUp();
    }

    function closePopupMenu() {
        $itemMenu.removeClass("expanded");
        if (popupMenu._opened) {
            popupMenu._animatePopUp();
        }
    }

    function createBreadcrumbItem(item) {
        var id = item.get_value().split('|')[0];

        return "<span class='item link-item' data-id='" + id + "'>" + item.get_text() + "</span>";
    };

    function breadcrumbItemClicked(e) {
        var id = e.target.attributes["data-id"].value;

        // all following items must removed
        $(e.target).nextAll().remove();
        // filter grid
        page.filterGrid(parseInt(id));
        $(e.target).addClass("selected");
    }

    mobile.getSelectedFolderText = function () {
        var item = popupMenu.get_selectedItem();
        if (item) {
            var regex = /\(.*\)/;
            var text = item.get_text().replace(regex, '');
            return text.trim();
        }
    }

    mobile.getSelectedFolderValue = function () {
        var item = popupMenu.get_selectedItem();
        if (item) {
            return getFolderId(item);
        }
    }

    function getFolderId(item) {
        return item.get_value().split('|')[0];
    }

    function getFolderText(text) {
        return text.replace(/\(.*\)/, '').trim();
    }

    mobile.get_navigationControl = function () {
        return popupMenu;
    }

    mobile.get_selected = function () {
        return popupMenu.get_selectedItem();
    }

    mobile.updateCount = function (folder, increase) {
        if (folder === null) return;
        var $folderText = $(".rmLink:contains('" + folder + "')");
        var $breadCrumbItem = $(".item.link-item:contains('" + folder + "')");
        if ($folderText.size() === 0) return;
        var text = $folderText.text();
        var regex = /\((.*?)\)/;
        count = parseInt(text.match(regex)[1], 10);
        if (increase === undefined || increase === true) {
            count++;
        } else {
            if (count === 0) return;
            count--;
        }
        var updatedItem = popupMenu._extractItemFromDomElement($folderText[0]);
        var updatedText = text.replace(regex, '(' + count + ')');
        if (updatedItem) {
            updatedItem.set_text(updatedText);
        }
        $breadCrumbItem.text(updatedText);
    };

    mobile.addFolderClicked = function () {
        popupMenu.add_itemClicked(this.popupMenuClicked);
    };

    mobile.popupMenuPopulated = function (sender, args) {
        var items = sender.get_items();
        if (items.getItem(0) !== null) {
            items.getItem(0).select();
        }
    }
})();