/*
 * 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/.
 */


var routes = [
    {
        path: '/home',
        templateUrl: './pages/home.html',
        name: 'home',
        keepAlive: true,
        on: {
            pageAfterIn: function () {
                function tryToLoadHomePage() {
                    if (typeof loadHomePage != "function") {
                        setTimeout(tryToLoadHomePage, 500);
                    } else {
                        loadHomePage(function () {});
                    }
                }
                tryToLoadHomePage();
            }
        }
    },
    {
        path: '/inventory',
        templateUrl: './pages/inventory.html',
        name: 'inventory',
        keepAlive: true,
        on: {
            pageAfterIn: function () {
                function tryToLoadInventory() {
                    if (typeof loadInventory != "function") {
                        setTimeout(loadInventory, 500);
                    } else {
                        loadInventory(function () {});
                    }
                }
                tryToLoadInventory();
            }
        }
    },
    {
        path: '/signin',
        url: './pages/signin.html',
        name: 'signin'
    },
    {
        path: '/chooseteam',
        templateUrl: './pages/chooseteam.html',
        name: 'chooseteam',
        options: {
            context: {
                teams: SETTINGS['teams']
            }
        }
    },
    {
        path: '/upgrade',
        url: './pages/upgrade.html',
        name: 'upgrade'
    },
    {
        path: '/connection',
        url: './pages/connection.html',
        name: 'connection'
    },
    {
        path: '/settings',
        name: 'settings',
        templateUrl: './pages/settings.html',
        on: {
            pageAfterIn: function () {
                function tryToLoadSettings() {
                    if (typeof loadSettings != "function") {
                        setTimeout(loadSettings, 500);
                    } else {
                        loadSettings();
                    }
                }
                tryToLoadSettings();
            }
        }
    },
    {
        path: '/credits',
        name: 'credits',
        async: function (routeTo, routeFrom, resolve, reject) {
            $.ajax({
                url: 'code-credits.txt',
                dataType: 'text',
                type: 'GET',
                async: true,
                success: function (codecredits) {
                    $.ajax({
                        url: 'art-credits.html',
                        dataType: 'text',
                        type: 'GET',
                        async: true,
                        success: function (artcredits) {
                            resolve({
                                templateUrl: './pages/credits.html'
                            }, {
                                context: {
                                    artcredits: artcredits,
                                    codecredits: codecredits
                                }
                            });
                        },
                        error: function (jqXHR, status, errorThrown) {
                            reject();
                        }
                    });
                },
                error: function (jqXHR, status, errorThrown) {
                    reject();
                }
            });
        }
    }
];