<!-- This Source Code Form is subject to the terms of the Mozilla Public
   - License, v. 2.0. If a copy of the MPL was not distributed with this
   - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<div class="app-dock-container">
    <div class="app-dock" id="app-dock">
        <div style="margin-top: 50px;">
            <img src="img/loader.gif" style="width: 65px; height: 65px;" alt=""/>
        </div>
        <div style="width: 100%;"></div>
        <p class="loading-text">Loading...</p>
    </div>
</div>

<script>
    function loadapps() {
        $(".loading-text").html("Loading Apps...");
        $.post(localStorage.getItem("syncurl"), {
            username: localStorage.getItem("username"),
            key: localStorage.getItem("key"),
            action: "listapps"
        }, function (data) {
            if (data.status === 'OK') {
                $("#loading-text").text("One moment...");
                $('#app-dock').html("");
                Object.keys(data.apps).forEach(function (k) {
                    var app = data.apps[k];
                    var iconurl = app.icon;
                    if (!app.icon.startsWith("http")) {
                        iconurl = app.url + app.icon;
                    }
                    $("<div class=\"app-dock-item\" onclick=\"openapp('" + k + "', '" + app.mobileapi + "', '" + app.url + "', '" + iconurl + "', '" + app.title + "')\"><p><img src=\"" + iconurl + "\" class=\"img-responsive app-icon\" /><span>" + app.title + "</span></p></div>").hide().appendTo("#app-dock").fadeIn(200);
                });
            } else {
                navigator.notification.alert(data.msg, null, "Error", 'Dismiss');
                openscreen("homeloaderror");
            }
        }, "json").fail(function () {
            navigator.notification.alert("Could not connect to the server.  Try again later.", null, "Error", 'Dismiss');
            openscreen("homeloaderror");
        });
    }

    if (isconfigvalid()) {
        setnavbar("home");
        loadapps();
    } else {
        openscreen("setup1");
    }
</script>