//game information
var picOff=[];  //raised buttons 0-blank,1-green or hovering/right, 2-red or wrong
picOff[0] = new Image(50,50);
picOff[0].src = "btnNormal.png";
picOff[1] = new Image(50,50);
picOff[1].src ="btnCorrectAndHover.png";
picOff[2] = new Image(50,50);
picOff[2].src ="btnWrong.png";
var picOn=[];//pressed buttons 0-blank, 1-green or right, 2-red or wrong
picOn[0] = new Image(50,50);
picOn[0].src ="btnNormalDown.png";
picOn[1] = new Image(50,50);
picOn[1].src ="btnCorrectDown.png";
picOn[2] = new Image(50,50);
picOn[2].src ="btnWrongDown.png";

function scrambleQuestions() {
    var x;var j;var k;
    for (var i=1; i <= 10;i++) {    //for each question (except instructions question 0)
        for(var n=0; n < 10 ; n++) { //using 10 mixes
            //switch 3 with one of 0,1 or 2, over and over randomly.33 of hitting one, must hit twice, 7 is min, use 10?
            j=Math.floor(Math.random()*4) ;
            k=Math.floor(Math.random()*4) ;

            x=answer[i][j];
            answer[i][j]=answer[i][k];
            answer[i][k]=x;

            x=hintText[i][j];
            hintText[i][j]=hintText[i][k];
            hintText[i][k]=x;

            if (photoButtons){
                x=buttonPics[i][j];
                buttonPics[i][j]=buttonPics[i][k];
                buttonPics[i][k]=x;
            }
            //dont scramble zoom hints or points;
            if (!isZoom[i]){
                x=hintPic[i][j];
                hintPic[i][j]=hintPic[i][k];
                hintPic[i][k]=x
                /*dont ever scramble points
                x=points[i][j];
                points[i][j]=points[i][k];
                points[i][k]=x;            */
            }
        }
    }
}
scrambleQuestions();


var picQuestion = new Image (400,400);
var picHint=[];
picHint[0]=new Image (400,400);
picHint[1]=new Image (400,400);
picHint[2]=new Image (400,400);
picHint[3]=new Image (400,400);

var correctColor= "#00ff00";
var incorrectColor="#ff0000";

var questionShown=0;
var curGuess=0;
var curScore=0;
var curPoints=0; //for each questins there are penaties added at the end
var guessed=[] ; //did they press this button already
var buttons=[] ; //color of button - state - correct, incorrect, not quessed.

function startQuiz(){
    showQuestion(0);
}

function showQuestion (i) {     //Load and Show next(current) question -reset question
    //show loading question status bar;

    document.getElementById('cameraScreen').src="loading.gif";
    picHint[0].src="images/"+hintPic[questionShown][0];
    picHint[1].src="images/"+hintPic[questionShown][1];
    picHint[2].src="images/"+hintPic[questionShown][2];
    picHint[3].src="images/"+hintPic[questionShown][3];
    picQuestion.src="images/" + pic[questionShown];

    zoomLevel=0;
    //done loading?
//    if (i>0) questionShown=i;
    questionShown=i;
    //show picture
    //document.getElementById('pictureElement').lastChild.src="quiz1/" + pic[questionShown];
    if (isZoom[questionShown]){
        document.getElementById('cameraScreen').src=picHint[zoomLevel].src;
    } else {
        document.getElementById('cameraScreen').src=picQuestion.src;
    }
    //check that picture exists?-no
    // show question number
    if (questionShown>0){
        document.getElementById('newStat').innerHTML= " " + questionType + " " + questionShown + " of " + nQuestions ;
        shadow('newStat');
        hideNextQuestionButton();
    } else {
        showNextQuestionButton();
    }
    //show score (question= question[questionShown];)
    //reset all buttons

   guessed=[false,false,false,false];

    curGuess=0;

    //for (var q=0; q<=3;q++){
    //    buttons[q].src=buttonPics[questionShown][q];
    //}
    if (photoButtons||hideInstructionsButtons) {
        if (questionShown==0) {
            document.getElementById("button1").style.display="none";
            document.getElementById("button2").style.display="none";
            document.getElementById("button3").style.display="none";
            document.getElementById("button4").style.display="none";
        } else {
            document.getElementById("button1").style.display="block";
            document.getElementById("button2").style.display="block";
            document.getElementById("button3").style.display="block";
            document.getElementById("button4").style.display="block";
        }

    }
    buttons=[0,0,0,0];
    dim(1);
    dim(2);
    dim(3);
    dim(4);

    curPoints=0;

    adjustZoomButtons();

   //  highlight(1);

    //highlight(2);
    //highlight(3);
   // highlight(4);

}


function nextQuestion(){
    questionShown++;
    if (questionShown < question.length) {
        showQuestion(questionShown);
    } else { // end quiz
        endQuiz();
    }
}

function endQuiz(){
    document.getElementById('cameraScreen').src="endQuiz.jpg";
    hideNextQuestionButton();
    adjustZoomButtons();

    document.getElementById("button1").style.display="none";
    document.getElementById("button2").style.display="none";
    document.getElementById("button3").style.display="none";
    document.getElementById("button4").style.display="none";

    document.getElementById("finalScore").innerHTML="Final Score<br>" + curScore;
    shadow("finalScore");
    document.getElementById('newStat').innerHTML= "" ;
    shadow("newStat");
    document.getElementById("score").innerHTML= "";
    shadow("score");
    createCookie("current["+quizName+"]", curScore, 365);
    var high=0+readCookie("high["+quizName+"]");
    if (curScore>high)
        createCookie("high["+quizName+"]", curScore, 365);
}

function showNextQuestionButton() {
    document.getElementById('nextButton').style.display="block";
}

function hideNextQuestionButton() {
    document.getElementById('nextButton').style.display="none";
}

function press(i) { //i is the number 1-4 of the button pressed
    if (questionShown>0) { //press doesnt work on help
        if (!guessed[i-1]) { //if first time for this choice do score stuff
            //if correct
           curGuess++;

            if (hintText[questionShown][i-1]=="Correct"){   //they got it right!
                //set other buttons to red
                buttons= [2,2,2,2]
                //set correct button to green

                buttons [i-1] = 1;
                zoomLevel=3;
                if (isZoom[questionShown] ) {//scoring is based on number of guesses, 3 is correct
                    curPoints= curPoints + points[questionShown][3];
                } else {  //points for current question
                    // if not zoom, change question picture to the (correct) answer picture.
                    picQuestion.src=picHint[i-1].src;
                    //curPoints = curPoints + points[questionShown][i-1];
                    curPoints= curPoints + points[questionShown][3];
                }
                 curScore = curScore + curPoints ;
                //indicate you got (points + curPoints) points
                //make it so you cannot lose any points
                curPoints=0 ;
                //points=[0,0,0,0] ;
                //no more guesses allowed
                guessed=[true,true,true,true];
                curGuess=3;//**todo should this be 4?
                //if there are more questions **todo
                if (questionShown>0){
                    answer[questionShown][i-1]=answer[questionShown][i-1] + " - CORRECT!";
                }
                showNextQuestionButton(true);
                showScore();
                dim(1);dim(2);dim(3);dim(4);
            }  else { // incorrect
                /*if (isZoom[questionShown] ) {//scoring is based on number of guesses, not which one   */
                    curPoints= curPoints + points[questionShown][curGuess-1];
                /*} else {
                    curPoints = curPoints + points[questionShown][i-1];
                } */
                if (questionShown>0) {
                    answer[questionShown][i-1]= answer[questionShown][i-1] + " (incorrect)";
                }
                //set button to red
                buttons[i-1]=2;
                //if zoom, re-arrange hints so this one is the highest un-guessed one
                zoomLevel=curGuess ;
            }
        }
        guessed[i-1]=true;
        // if second(or first) time show related picture
        if (isZoom[questionShown]) {
            document.getElementById('cameraScreen').src=picHint[zoomLevel].src;
        } else {
            document.getElementById('cameraScreen').src=picHint[i-1].src;
        }
        // show pressed button
        if (photoButtons) {
            if (hintText[questionShown][i-1]=="Correct"){
                document.getElementById("button" + i).src="buttons/"+buttonPics[questionShown][i-1]+"_UP.png";
            } else {
                document.getElementById("button" + i).src="buttons/"+buttonPics[questionShown][i-1]+"_WRONG.png";
            }
        } else {
            document.getElementById("button" + i).src=picOn[buttons[i-1]].src;
        }
        if (buttons[i-1]==2) {
            document.getElementById('answer').style.color=incorrectColor;
        } else {
            document.getElementById('answer').style.color=correctColor;
        }
        document.getElementById('answer').innerHTML=hintText[questionShown][i-1];
        shadow('answer');
        adjustZoomButtons();

    }
}

function showScore(){
    if (questionShown>0){
        document.getElementById("score").innerHTML="Score<br>" + curScore;
        shadow('score');
    }

}

function      shadow(strID) {
   var t=document.getElementById(strID).innerHTML;
   document.getElementById(strID+"b1").innerHTML=t;
   document.getElementById(strID+"b2").innerHTML=t;
   document.getElementById(strID+"b3").innerHTML=t;
   document.getElementById(strID+"b4").innerHTML=t;

   document.getElementById(strID+"b5").innerHTML=t;
   document.getElementById(strID+"b6").innerHTML=t;
   document.getElementById(strID+"b7").innerHTML=t;
   document.getElementById(strID+"b8").innerHTML=t;

}



function zoomOut(){
    zoomLevel--;
    if (zoomLevel < 0) zoomLevel=0;
    showQuestionPick();
    adjustZoomButtons();

}

function zoomIn(){
    zoomLevel++;
    if (zoomLevel>curGuess) zoomLevel=curGuess;
    showQuestionPick();
    adjustZoomButtons();

}

function adjustZoomButtons() { //show/hide depending on zooms available

    if (showZoomButtons){
        if (isZoom[questionShown]){
            if (zoomLevel>0) {
                document.getElementById('wideButton').style.display="block"; //block";
            } else {
                document.getElementById('wideButton').style.display="none";
            }
            if (zoomLevel < curGuess) {
                document.getElementById('teleButton').style.display="block";
            } else {
                document.getElementById('teleButton').style.display="none";
            }
        } else {
            document.getElementById('teleButton').style.display="none";
            document.getElementById('wideButton').style.display="none";
        }
    } else {
        var e=document.getElementById('teleButton');
        if (e) e.style.display="none";
        e=document.getElementById('wideButton');
        if (e) e.style.display="none";
    }
}

function highlight(i){ // un press
    // if it is blank, use green up

    if (photoButtons) {
        if (buttons[i-1]==2)
            document.getElementById("button" + i).src="buttons/"+buttonPics[questionShown][i-1]+"_WRONG.png";
        else if (buttons[i-1]==0){
            document.getElementById("button" + i).src="buttons/"+buttonPics[questionShown][i-1]+"_OVER.png";
        } else {
            document.getElementById("button" + i).src="buttons/"+buttonPics[questionShown][i-1]+"_OVER.png";
        }

        if (buttons[i-1]==2) {
            document.getElementById('answer').style.color=incorrectColor;
        } else {
                document.getElementById('answer').style.color=correctColor;
        }
        document.getElementById('answer').innerHTML=answer[questionShown][i-1];
        shadow('answer');

    }
    else{
    if (buttons[i-1]==0){

        document.getElementById("button" + i).src=picOff[1].src;
    } else {
        document.getElementById("button" + i).src=picOff[buttons[i-1]].src;
    }

     if (buttons[i-1]==2) {
        document.getElementById('answer').style.color=incorrectColor;
    } else {
            document.getElementById('answer').style.color=correctColor;
     }
     document.getElementById('answer').innerHTML=answer[questionShown][i-1];
     shadow('answer');

     }
     //show question pic

     showQuestionPick();
}

function showQuestionPick(){

    if (isZoom[questionShown]) {
        document.getElementById('cameraScreen').src=picHint[zoomLevel].src;

    } else {

         document.getElementById('cameraScreen').src=picQuestion.src;
    }
}

function dim(i) { //normal - unhighlight
    if (photoButtons){
        if (questionShown>0) {
            if (buttons[i-1]<2)
                document.getElementById("button" + i).src="buttons/"+buttonPics[questionShown][i-1]+"_UP.png";
            else
                document.getElementById("button" + i).src="buttons/"+buttonPics[questionShown][i-1]+"_WRONG.png";
        }
    }
    else
        document.getElementById("button" + i).src=picOff[buttons[i-1]].src;

    document.getElementById('answer').innerHTML="";
    shadow('answer');
}

function createCookie(name, value, days)
{
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
    }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
  var ca = document.cookie.split(';');
  var nameEQ = name + "=";
  for(var i=0; i < ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
  return null;
}
