/*

    VANIXON TIC TAC TOE JS
    
    --> TICTACTOE INIT <--
    

    Created by Vanixon GmbH, Pascal Stoop <http://www.vanixon.com>

*/   

    /***********************************************        
        INIT                                                
    ************************************************/
    
    var extTriviaType = 3;      // 0 => general;  1 => geography;  2 => food; 3 => movie 4 => history 5 => sport
    var extNrPlayer = 0;        // 0 => 1 player; 1 => 2 player
    
    var extPlayerImgNameSuffix = new Array();
    extPlayerImgNameSuffix[0] = "_0";
    extPlayerImgNameSuffix[1] = "_1";

    var extTriviaImgSuffix = new Array();
    extTriviaImgSuffix[0] = "_general";
    extTriviaImgSuffix[1] = "_geography";
    extTriviaImgSuffix[2] = "_food";
    extTriviaImgSuffix[3] = "_movie";
    extTriviaImgSuffix[4] = "_history";
    extTriviaImgSuffix[5] = "_sport";

    
    
    /***********************************************        
        EXT METHODS                                              
    ************************************************/
    function selectCategory(category_id) {
        if (refTictactoeGame !== null) {
            extTriviaType = category_id;
            if (refTictactoeGame.showInstructions[refTictactoeGame.actualPlayer]) {
                transitionToInstructions();
            }
            else {
                transitionToTicTacToe();
            }
        }

    }
    function transitionToTicTacToe() {
      if (refTictactoeGame !== null) {        
        $jq.mobile.changePage('#tictactoe', 'flip');
      }    
    } 
    function transitionToTicTacToeWinner() {
        $jq.mobile.changePage('#tictactoe_winner', 'flip');    
    } 
    function transitionToScore(){
      $jq.mobile.changePage('#tttScore', 'flip');
    }
    function transitionToInstructions() {
        $jq.mobile.changePage('#tttShowInstructions', 'flip');
    }
    function transitionToQuestion() {
        $jq.mobile.changePage('#tttQuestion', 'flip');
    }
    function transitionInstructionToTictactoe(showAgain) {
        if (refTictactoeGame !== null) {
            if (!showAgain) {
                refTictactoeGame.showInstructions[refTictactoeGame.actualPlayer] = false;
            }
            $jq.mobile.changePage('#tictactoe', 'flip');
        }
        
    }


    $jq(document).ready(function (e) {

        $jq('.box').mousedown(function (event) {

            if (refTictactoeGame !== null) {
                var tempy = parseInt($jq('img#' + event.target.id).attr('data-y-value'));
                var tempx = parseInt($jq('img#' + event.target.id).attr('data-x-value'));

                if (refTictactoeGame.gamePanelDatas[tempy][tempx] == -1) {
                    //$jq('div#' + event.target.id).css({'background-image':'url(images/box_open' + extTriviaImgSuffix[extTriviaType] + '_pressed'+ extPlayerImgNameSuffix[refTictactoeGame.actualPlayer] +'.png)'});
                    $jq('img#' + event.target.id).attr('src', 'images/box_open' + extTriviaImgSuffix[extTriviaType] + '_pressed' + extPlayerImgNameSuffix[refTictactoeGame.actualPlayer] + '.png');
                }
            }
        });

        $jq('.box').mouseup(function (event) {
            if (refTictactoeGame !== null) {
                var tempy = parseInt($jq('img#' + event.target.id).attr('data-y-value'));
                var tempx = parseInt($jq('img#' + event.target.id).attr('data-x-value'));

                if (refTictactoeGame.gamePanelDatas[tempy][tempx] == -1) {
                    $jq('img#' + event.target.id).attr('src', 'images/box_open' + extTriviaImgSuffix[extTriviaType] + extPlayerImgNameSuffix[refTictactoeGame.actualPlayer] + '.png');

                    //set active field
                    refTictactoeGame.activeFieldX = tempx;
                    refTictactoeGame.activeFieldY = tempy;

                    //update question texts              

                    //change page to question
                    transitionToQuestion();
                }
            }
        });

        $jq('.box').mouseout(function (event) {
            if (refTictactoeGame !== null) {
                var tempy = parseInt($jq('div#' + event.target.id).attr('data-y-value'));
                var tempx = parseInt($jq('div#' + event.target.id).attr('data-x-value'));


                /*if (refTictactoeGame.gamePanelDatas[tempy][tempx] == -1) {
                //$jq('div#' + event.target.id).css({'background-image':'url(images/box_open' + extTriviaImgSuffix[extTriviaType] + extPlayerImgNameSuffix[refTictactoeGame.actualPlayer] +'.png)'});
                $jq('img#' + event.target.id).attr('src','images/box_open' + extTriviaImgSuffix[extTriviaType] + extPlayerImgNameSuffix[refTictactoeGame.actualPlayer] +'.png');
                }*/
            }
        });


        /* ANSWER BUTTON PRESS -> Player gives an answer */
        $jq('.answer_button').mousedown(function (event) {

            if (refTictactoeGame !== null) {
                // Der event wird an den answer_button weiter geleitet, das eigentlich Target ist jedoch der <span>
                // in dem die Antwort steht
                // Darum wird event.target.parentNode.parentNode.id genommen
                //alert(isNaN(parseInt(event.target.parentNode.parentNode.id)));

                if (isNaN(parseInt(event.target.parentNode.parentNode.id))) {
                    refTictactoeGame.checkAnswer(event.target.parentNode.id);
                }
                else {
                    refTictactoeGame.checkAnswer(event.target.parentNode.parentNode.id);
                }
            }
        });


        $jq('#tttShowInstruction_continue').mouseup(function (event) {

            if (refTictactoeGame !== null) {

            }
        });




        $jq(window).resize(function () {
            if (refTictactoeGame !== null) {
                refTictactoeGame.recalculateGraphicDatas();
            }
        });


        $jq('#tictactoe').bind('pagebeforeshow', function () {
            if (refTictactoeGame !== null) {
                if (refTictactoeGame.isInPlay) {
                }
                
            }
        });
        $jq('#tttShowInstructions').bind('pagebeforeshow', function () {
            if (refTictactoeGame !== null) {
                if (!refTictactoeGame.isInPlay) {
                    refTictactoeGame.refresh();
                }
            }
        });

        $jq('#tictactoe_winner').bind('pagebeforeshow', function () {
            if (refTictactoeGame !== null) {
                refTictactoeGame.showWinningPlayer();
            }
        });


        $jq('#tictactoe').bind('pageshow', function () {
            if (refTictactoeGame !== null) {
                if (!refTictactoeGame.isInPlay) {
                    refTictactoeGame.isInPlay = true;
                }
            }
        });
        $jq('#tttQuestion').bind('pagebeforeshow', function () {
            if (refTictactoeGame !== null) {
                refTictactoeGame.removeClassesFromButton();
                refTictactoeGame.prepareQuestionForPlayer();
                refTictactoeGame.countdown_counter = 11;
                answer_given = false;


            }
        });
        $jq('#tttQuestion').bind('pageshow', function () {
            if (refTictactoeQuestionThread !== null) {
                refTictactoeQuestionThread.setPauseState(false);
                refTictactoeQuestionThread.setShutdownState(false);
                refTictactoeQuestionThread.run();

                answer_given = false;
            }
        });
        $jq('#tictactoe_winner').bind('pageshow', function () {
            if (refTictactoeGame !== null) {
                window.setTimeout("transitionToScore()", 2000);
            }
        });


        refTictactoeGame = new TictactoeGame();
        refTictactoeQuestionThread = new TictactoeQuestionThread();
        refTictactoeQuestionThread.setTimerMS(1000);

    });
