/// <reference path="//Microsoft.WinJS.1.0/js/ui.js" />  
/// <reference path="//Microsoft.WinJS.1.0/js/base.js" />  
(function () {
    "use strict";
    WinJS.Namespace.define("Navigation", {
        Commands: { 
            BackButtonClicked: function (e) {
                WinJS.Navigation.back().then(function (e) {
                    var cmd = Navigation.getCommand(WinJS.Navigation.location);
                    Navigation.checkButtons(cmd ? cmd : null);
                });
            },
            HomeButtonClicked: function (e) {
                WinJS.Navigation.navigate("pages/home/home.html");
                WinJS.Navigation.history.backStack.length = 0;
                Navigation.checkBackButtons();
            },
            CalendarButtonClicked: function (V2) { 
                WinJS.Navigation.navigate("pages/calendar/calendar.html");
            },
            SparkLineButtonClicked: function (V2) { 
                WinJS.Navigation.navigate("pages/sparkLines/sparkLines.html");
            },
            TokenInputButtonClicked: function (V2) { 
                WinJS.Navigation.navigate("pages/tokenInput/tokenInput.html");
            },
            GridButtonClicked: function (V2) { 
                WinJS.Navigation.navigate("pages/grid/grid.html");
            },
    
        },
        checkBackButtons: function (e) { 
            backButton.disabled = !WinJS.Navigation.canGoBack;
            nbcBack.disabled = !WinJS.Navigation.canGoBack;
            nbcHome.disabled = !WinJS.Navigation.canGoBack;
        },
        wireNavigation: function (args) {
            WinJS.Utilities.markSupportedForProcessing(Navigation.Commands.BackButtonClicked);
            Navigation.registerCommand("pages/home/home.html", Navigation.Commands.HomeButtonClicked);
            WinJS.Utilities.markSupportedForProcessing(Navigation.Commands.HomeButtonClicked);
            Navigation.registerCommand("pages/calendar/calendar.html", Navigation.Commands.CalendarButtonClicked);
            WinJS.Utilities.markSupportedForProcessing(Navigation.Commands.CalendarButtonClicked);
            Navigation.registerCommand("pages/sparkLines/sparkLines.html", Navigation.Commands.SparkLineButtonClicked);
            WinJS.Utilities.markSupportedForProcessing(Navigation.Commands.SparkLineButtonClicked);
            Navigation.registerCommand("pages/tokenInput/tokenInput.html", Navigation.Commands.TokenInputButtonClicked);
            WinJS.Utilities.markSupportedForProcessing(Navigation.Commands.TokenInputButtonClicked);
            Navigation.registerCommand("pages/grid/grid.html", Navigation.Commands.GridButtonClicked);
            WinJS.Utilities.markSupportedForProcessing(Navigation.Commands.GridButtonClicked);
        },
        CommandRegistration: new Array(0),
        getCommand: function (page) {
            var command = null;
            Navigation.CommandRegistration.forEach(function (item) {
                if (item[0] === page) {
                    command = item[1];
                }
            });
            return command;
        },
        registerCommand: function (page, commandID) {
            var found = false;
            Navigation.CommandRegistration.forEach(function (item) {
                if (item[0] === page) {
                    found = true;
                    var index = item.indexOf(commandID);
                    if (index === -1) {
                        item[item.length] = commandID;
                    }
                }
            });
            if (!found) {
                var lastIndex = Navigation.CommandRegistration.length;
                Navigation.CommandRegistration[lastIndex] = new Array(2);
                Navigation.CommandRegistration[lastIndex][0] = page;
                Navigation.CommandRegistration[lastIndex][1] = commandID;
            }
        },
        checkButtons : function (command) {
            $('button:not(#backButton)').forEach(function (item) {
                var ctl = item.winControl ? item.winControl : item;
                var ctlCmd = ctl ? ctl.onclick : null;
                if (ctlCmd !== Navigation.Commands.BackButtonClicked) {
                    if (ctlCmd === command) {
                        item.disabled = true;
                    }
                    else {
                        item.disabled = false;
                    }
                }
            });
        },
    });
    WinJS.Navigation.addEventListener("navigating", function (e) {
        e.detail.setPromise(
            new WinJS.Promise(function (fDone, fError, fProgress) {
                WinJS.UI.Animation.exitPage(contentTarget.children).then(function () {
                    WinJS.Utilities.empty(contentTarget);
                    WinJS.UI.Pages.render(e.detail.location, contentTarget).then(function () {
                        WinJS.UI.Animation.enterPage(contentTarget.children).then(fDone("Finished"));
                    });
                })
            })
            );
        Navigation.checkButtons(Navigation.getCommand(WinJS.Navigation.location));
        Navigation.checkBackButtons;
    });
    Navigation.wireNavigation();
})();