// Show a web view in the current nav controller.
function showWebView(/*NavigationController*/controller, /*String*/title, /*Array*/urlArray) {
    log.start();

    var wv = new WebView(title, urlArray);

    // Bridge between web view and the application.
    if(firstTime) {
        Ti.App.addEventListener('requestBibleQuote', function ShowQuoteListener(e) {
            log.start("Looking up verse: " + e.verse);
            var bibleVersion = Titanium.App.Properties.getString('BibleVersion', 'kjv');

            if(!Ti.Network.online) {
                var dlg = Titanium.UI.createAlertDialog({
                    title : L('network_none'),
                    ok : L('button_ok'),
                    message : L('no_quote')
                });
                dlg.show();
            } else {
                var bq = new BibleQuote(bibleVersion, e.verse);
                bq.open(controller);
            }
        });
        firstTime = false;
    }

    wv.open(controller);
    // Push the view onto the modal stack
}
function courageousButtonClick(e) {
    var theButton = e.source;
    log.start('Button pressed ' + theButton.id);

    var dlg;

    Titanium.Analytics.featureEvent('com.noblecall.toolset.' + theButton.id);

    switch (theButton.id) {
        case 'agreements':
        case 'questions':
            showWebView(navController, courageousButtons[theButton.id].text, courageousButtons[theButton.id].url);
            break;
        case 'prayers':
            // There are 2 paths through identity. We need to ask the user whether they are a man or a woman.
            dlg = Ti.UI.createOptionDialog({
                title : L('question_listener'),
                options : [L('button_listener'), L('button_speaker'), L('button_cancel')],
                cancel : 2
            });
            dlg.addEventListener('click', function(e) {
                if(e.index != 2) {
                    showWebView(navController, courageousButtons[theButton.id].text, courageousButtons[theButton.id].url[e.index]);
                }
            });
            dlg.show();
            break;
        case 'words':
            // There are 2 paths through words. We need to ask the user whether they are a man or a woman.
            dlg = Ti.UI.createOptionDialog({
                title : L('question_man'),
                options : [L('button_man'), L('button_woman'), L('button_cancel')],
                cancel : 2
            });
            dlg.addEventListener('click', function(e) {
                if(e.index != 2) {
                    showWebView(navController, courageousButtons[theButton.id].text, courageousButtons[theButton.id].url[e.index]);
                }
            });
            dlg.show();
            break;
        default:
            dlg = Ti.UI.createAlertDialog({
                title : 'Alert',
                message : "Button " + theButton.id + " is not implemented!",
                buttonNames : ['Ok']
            });
            dlg.show();
            break;
    }
}
// Go back to the initial window of the NavigationController
function NavigationControllerHome() {
    log.start();

    // Store a copy of all the current windows on the stack
    var windows = this.windowStack.concat([]);
    for(var i = 1, l = windows.length; i < l; i++) {
        if (this.navGroup) {
            this.navGroup.close(windows[i]);
        } else { 
            windows[i].close();
        }
    }
    //reset stack
    this.windowStack = [this.windowStack[0]];
}
        Ti.App.addEventListener('requestBibleQuote', function ShowQuoteListener(e) {
            log.start("Looking up verse: " + e.verse);
            var bibleVersion = Titanium.App.Properties.getString('BibleVersion', 'kjv');

            if(!Ti.Network.online) {
                var dlg = Titanium.UI.createAlertDialog({
                    title : L('network_none'),
                    ok : L('button_ok'),
                    message : L('no_quote')
                });
                dlg.show();
            } else {
                var bq = new BibleQuote(bibleVersion, e.verse);
                bq.open(controller);
            }
        });
function open() {
    log.start();

    // Construct the main window and navigation controller.
    var w = Ti.UI.createWindow({
        anchorPoint: { x: 0, y: 0 },
        title : L('app_title'),
    });
    navController = new NavigationController();

    // Create a Info button on the parent window.
    var InfoButton = require('widgets/InfoButton');
    var infoButton = new InfoButton();
    infoButton.attach(w, function mainInfoButtonClick(e) {
        var InfoPanel = require('ui/InfoPanel');
        var mainInfoPanel = new InfoPanel();   
        mainInfoPanel.open(navController);
    });
       
    // We want a maximum of three buttons across the narrowest aspect of the device.
    var mainButtonGrid = new ButtonGrid({
        viewWidth: Math.min(Ti.Platform.displayCaps.platformWidth, Ti.Platform.displayCaps.platformHeight), 
        buttons: mainButtons, 
        buttonWidth: style.buttonGrid.width,
        buttonHeight: style.buttonGrid.height,
        click: mainButtonGridClick
    });
    w.add(mainButtonGrid.scrollview);

    w.barColor = style.win.barColor;

    // Nav controller is our root.
    navController.open(w);

    // Handle orientation
    function relayoutMainWindow(e) {
        mainButtonGrid.relayout(Ti.Platform.displayCaps.platformWidth);
    }
    Ti.Gesture.addEventListener('orientationchange', relayoutMainWindow);
    
    setTimeout(relayoutMainWindow, 1000);
    
    var density = Ti.Platform.displayCaps.density;
    alert("Density: " + density + ' platformHeight: ' + Ti.Platform.displayCaps.platformHeight + ' platformWidth: ' + Ti.Platform.displayCaps.platformWidth);
}
function NavigationControllerOpen(/*Ti.UI.Window*/windowToOpen) {
    log.start();

    //add the window to the stack of windows managed by the controller
    this.windowStack.push(windowToOpen);

    //grab a copy of the current nav controller for use in the callback
    var that = this;
    windowToOpen.addEventListener('close', function() {
        that.windowStack.pop();
    });
    //hack - setting this property ensures the window is "heavyweight" (associated with an Android activity)
    windowToOpen.navBarHidden = windowToOpen.navBarHidden || false;

    //This is the first window
    if (this.windowStack.length === 1) {
        var containerWindow;

        switch (Ti.Platform.osname) {
            case 'iphone':
            case 'ipad':
                this.navGroup = Ti.UI.iPhone.createNavigationGroup({
                    window : windowToOpen
                });
                containerWindow = Ti.UI.createWindow();
                containerWindow.add(this.navGroup);
                containerWindow.open({
                    transition : Titanium.UI.iPhone.AnimationStyle.FLIP_FROM_LEFT
                });
                break;
            case 'android':
                windowToOpen.exitOnClose = true;
                windowToOpen.open();
                break;
            case 'mobileweb':
                this.navGroup = Ti.UI.MobileWeb.createNavigationGroup({
                    window : windowToOpen
                });
                containerWindow = Ti.UI.createWindow();
                containerWindow.add(this.navGroup);
                containerWindow.open();
                break;
            default:
                log.assert(false, "Cross Platform code not implemented.");
                break;
        }
    } else {
        switch (Ti.Platform.osname) {
            case 'iphone':
            case 'ipad':
                this.navGroup.open(windowToOpen);
                break;
            case 'android':
                windowToOpen.open();
                break;
            case 'mobileweb':
                this.navGroup.open(windowToOpen);
                break;
            default:
                log.assert(false, "Cross Platform code not implemented.");
                break;
        }
    }
}
function NavigationController() {
    log.start();
    this.windowStack = [];
}
function MainWindow() {
    log.start();
}