// For an introduction to the Blank template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkId=232509
(function () {
    "use strict";

    var app = WinJS.Application;
    var activation = Windows.ApplicationModel.Activation;

    app.onactivated = function (args) {
        if (args.detail.kind === activation.ActivationKind.launch) {
            if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
                // TODO: This application has been newly launched. Initialize
                // your application here.
            } else {
                // TODO: This application has been reactivated from suspension.
                // Restore application state here.
            }
            args.setPromise(WinJS.UI.processAll());
        }
    };

    app.oncheckpoint = function (args) {
        // TODO: This application is about to be suspended. Save any state
        // that needs to persist across suspensions here. You might use the
        // WinJS.Application.sessionState object, which is automatically
        // saved and restored across suspension. If you need to complete an
        // asynchronous operation before your application is suspended, call
        // args.setPromise().
    };

    app.start();
    // After starting the application...
    $(document).ready(function () {
        /* Body element settings
        * settings like CSS */
        var body = $('body');
        // Hyperlink for the game
        var link = $('body div.vicvacvoe div a');
        link.attr('href', window.location);
        // Game initialization
        var vic = $('.vicvacvoe table tr td');
        var player = 1;
        /* vic.each(function () {
        $(this).attr('class', 'winningblock');
        }); */
        // Characters
        var tick = '✓';
        var cross = 'X';
        // Blocks of the board
        var block1 = "";
        var block2 = "";
        var block3 = "";
        var block4 = "";
        var block5 = "";
        var block6 = "";
        var block7 = "";
        var block8 = "";
        var block9 = "";
        vic.click(function () {
            var className = $(this).attr('class');
            if (className != 'dead') {
                // A new board place to write on...
                // Now do the processes here...
                if (player == 1) {
                    // First player!
                    var idName = $(this).attr('id');
                    switch (idName) {
                        case 'block1':
                            block1 = 'user1';
                            break;
                        case 'block2':
                            block2 = 'user1';
                            break;
                        case 'block3':
                            block3 = 'user1';
                            break;
                        case 'block4':
                            block4 = 'user1';
                            break;
                        case 'block5':
                            block5 = 'user1';
                            break;
                        case 'block6':
                            block6 = 'user1';
                            break;
                        case 'block7':
                            block7 = 'user1';
                            break;
                        case 'block8':
                            block8 = 'user1';
                            break;
                        case 'block9':
                            block9 = 'user1';
                            break;
                    }
                    console.log(block1 + ", " + block2 + ", " + block3 + ", " + block4 + ", " + block5 + ", " + block6 + ", " + block7 + ", " + block8 + ", " + block9);
                    $(this).text(tick);
                    player = 2;
                } else {
                    // Second player!
                    var idName = $(this).attr('id');
                    switch (idName) {
                        case 'block1':
                            block1 = 'user2';
                            break;
                        case 'block2':
                            block2 = 'user2';
                            break;
                        case 'block3':
                            block3 = 'user2';
                            break;
                        case 'block4':
                            block4 = 'user2';
                            break;
                        case 'block5':
                            block5 = 'user2';
                            break;
                        case 'block6':
                            block6 = 'user2';
                            break;
                        case 'block7':
                            block7 = 'user2';
                            break;
                        case 'block8':
                            block8 = 'user2';
                            break;
                        case 'block9':
                            block9 = 'user2';
                            break;
                    }
                    console.log(block1 + ", " + block2 + ", " + block3 + ", " + block4 + ", " + block5 + ", " + block6 + ", " + block7 + ", " + block8 + ", " + block9);
                    $(this).text(cross);
                    player = 1;
                }
                $(this).attr('class', 'dead');
            }
            var color = '#b2e374';
            // check for the blocks filled!
            if (block1 == 'user1' && block2 == 'user1' && block3 == 'user1') {
                // user1 has line 1
                // Coloring the table elements
                $('.vicvacvoe table tr td#block1').css('background-color', color);
                $('.vicvacvoe table tr td#block2').css('background-color', color);
                $('.vicvacvoe table tr td#block3').css('background-color', color);
                vic.attr('class', 'dead');
            } else if (block1 == 'user2' && block2 == 'user2' && block3 == 'user2') {
                // user2 has line 1
                // Coloring the table elements
                $('.vicvacvoe table tr td#block1').css('background-color', color);
                $('.vicvacvoe table tr td#block2').css('background-color', color);
                $('.vicvacvoe table tr td#block3').css('background-color', color);
                vic.attr('class', 'dead');
            }
            // check next line
            if (block4 == 'user1' && block5 == 'user1' && block6 == 'user1') {
                // user1 has line 2
                // Coloring the table elements
                $('.vicvacvoe table tr td#block4').css('background-color', color);
                $('.vicvacvoe table tr td#block5').css('background-color', color);
                $('.vicvacvoe table tr td#block6').css('background-color', color);
                vic.attr('class', 'dead');
            } else if (block4 == 'user2' && block5 == 'user2' && block6 == 'user2') {
                // user2 has line 2
                // Coloring the table elements
                $('.vicvacvoe table tr td#block4').css('background-color', color);
                $('.vicvacvoe table tr td#block5').css('background-color', color);
                $('.vicvacvoe table tr td#block6').css('background-color', color);
                vic.attr('class', 'dead');
            }
            // check next line
            if (block7 == 'user1' && block8 == 'user1' && block9 == 'user1') {
                // user1 has line 3
                // Coloring the table elements
                $('.vicvacvoe table tr td#block7').css('background-color', color);
                $('.vicvacvoe table tr td#block8').css('background-color', color);
                $('.vicvacvoe table tr td#block9').css('background-color', color);
                vic.attr('class', 'dead');
            } else if (block7 == 'user2' && block8 == 'user2' && block9 == 'user2') {
                // user2 has line 3
                // Coloring the table elements
                $('.vicvacvoe table tr td#block7').css('background-color', color);
                $('.vicvacvoe table tr td#block8').css('background-color', color);
                $('.vicvacvoe table tr td#block9').css('background-color', color);
                vic.attr('class', 'dead');
            }
            // check next line
            if (block1 == 'user1' && block4 == 'user1' && block7 == 'user1') {
                // user1 has line 4
                // Coloring the table elements
                $('.vicvacvoe table tr td#block1').css('background-color', color);
                $('.vicvacvoe table tr td#block4').css('background-color', color);
                $('.vicvacvoe table tr td#block7').css('background-color', color);
                vic.attr('class', 'dead');
            } else if (block1 == 'user2' && block4 == 'user2' && block7 == 'user2') {
                // user2 has line 4
                // Coloring the table elements
                $('.vicvacvoe table tr td#block1').css('background-color', color);
                $('.vicvacvoe table tr td#block4').css('background-color', color);
                $('.vicvacvoe table tr td#block7').css('background-color', color);
                vic.attr('class', 'dead');
            }
            // check next line
            if (block2 == 'user1' && block5 == 'user1' && block8 == 'user1') {
                // user1 has line 5
                // Coloring the table elements
                $('.vicvacvoe table tr td#block2').css('background-color', color);
                $('.vicvacvoe table tr td#block5').css('background-color', color);
                $('.vicvacvoe table tr td#block8').css('background-color', color);
                vic.attr('class', 'dead');
            } else if (block2 == 'user2' && block5 == 'user2' && block8 == 'user2') {
                // user2 has line 5
                // Coloring the table elements
                $('.vicvacvoe table tr td#block2').css('background-color', color);
                $('.vicvacvoe table tr td#block5').css('background-color', color);
                $('.vicvacvoe table tr td#block8').css('background-color', color);
                vic.attr('class', 'dead');
            }
            // check next line
            if (block3 == 'user1' && block6 == 'user1' && block9 == 'user1') {
                // user1 has line 6
                // Coloring the table elements
                $('.vicvacvoe table tr td#block3').css('background-color', color);
                $('.vicvacvoe table tr td#block6').css('background-color', color);
                $('.vicvacvoe table tr td#block9').css('background-color', color);
                vic.attr('class', 'dead');
            } else if (block3 == 'user2' && block6 == 'user2' && block9 == 'user2') {
                // user2 has line 6
                // Coloring the table elements
                $('.vicvacvoe table tr td#block3').css('background-color', color);
                $('.vicvacvoe table tr td#block6').css('background-color', color);
                $('.vicvacvoe table tr td#block9').css('background-color', color);
                vic.attr('class', 'dead');
            }
            // check next line
            if (block1 == 'user1' && block5 == 'user1' && block9 == 'user1') {
                // user1 has line 7
                // Coloring the table elements
                $('.vicvacvoe table tr td#block1').css('background-color', color);
                $('.vicvacvoe table tr td#block5').css('background-color', color);
                $('.vicvacvoe table tr td#block9').css('background-color', color);
                vic.attr('class', 'dead');
            } else if (block1 == 'user2' && block5 == 'user2' && block9 == 'user2') {
                // user2 has line 7
                // Coloring the table elements
                $('.vicvacvoe table tr td#block1').css('background-color', color);
                $('.vicvacvoe table tr td#block5').css('background-color', color);
                $('.vicvacvoe table tr td#block9').css('background-color', color);
                vic.attr('class', 'dead');
            }
            // check next line
            if (block3 == 'user1' && block5 == 'user1' && block7 == 'user1') {
                // user1 has line 8
                // Coloring the table elements
                $('.vicvacvoe table tr td#block3').css('background-color', color);
                $('.vicvacvoe table tr td#block5').css('background-color', color);
                $('.vicvacvoe table tr td#block7').css('background-color', color);
                vic.attr('class', 'dead');
            } else if (block3 == 'user2' && block5 == 'user2' && block7 == 'user2') {
                // user2 has line 8
                // Coloring the table elements
                $('.vicvacvoe table tr td#block3').css('background-color', color);
                $('.vicvacvoe table tr td#block5').css('background-color', color);
                $('.vicvacvoe table tr td#block7').css('background-color', color);
                vic.attr('class', 'dead');
            }
        });
    });
    // Now handle the File System
})();
