// JavaScript Document

function play_snd(x){
	if (document.all){
		snd.src=document.embeds[x+"_snd"].src
	}else{
		//document.embeds[x+"_snd"].play();
	}
}


//Snakes and Ladders script by Birgit Ferran (birgit@eoiweb.com) More scripts for creating interactive language learning activities can be found on the web at http://www.eoiweb.com/EoiTeach
//Please feel free to use and adapt this script but please leave this message intact. A link to EoiTeach would also be appreciated.
//If you would like to share the activities you have created with other teachers, please send me an email with the URL of your activity.

//Variables you can edit
//I have created a set of variables for the text messages that appear. If you want to translate this game into another language you will need to edit them.
//Please remember that if you need to use an apostrophe, it should be preceded by a backslash.

var lastQuestion1 = 'Way to go '; //player's name will go here
var lastQuestion2 = '! One more question and you have won the game!';
var winner1 = 'Congratulations '; //player's name will go here
var winner2 = '! You have won the game!'; //Please leave in the \n. It will put the text that follows on a new line.
var anotherTurn = 'You can have another turn, '; //player's name will go here
var wellDone = 'Well done, '; //player's name will go here
var pleaseAnswer = 'You need to answer the question, '; //player's name will go here
var sorryMsg = 'Sorry, I\'m afraid that\'s not right.\n'; //your feedback will go here. (Please leave in the \n. It will put the text that follows on a new line.)
var howMany = 'How many players?';
var ask4Name = 'Please write in your name, ';

var instructions = '<h3 align="center">INSTRUCTIONS</h3><ul><li>Select the number of players that are going to play the game.</li><li>Take turns rolling the die and answering the questions.</li><li>If you answer incorrectly, you go back to the square where you came from.</li><li>If you land on the foot of a LADDER and you answer correctly you go up the ladder.</li><li>If you land on the top of a SNAKE and you answer incorrectly you go down the snake.</li>';
instructions += '<li>If you land on one of the HOTSPOTS (Sun or Snake) and you answer correctly you get another turn, but if you answer incorrectly you go back to SQUARE 1.</li><li>The first player to reach SQUARE 64 and answer the question correctly is the winner.</li></ul><form><p align="center">GOOD LUCK!<br>I HOPE YOU ENJOY THE GAME!';
var player = 'Player ';
var players = 'Players';
var currentPlayerHeading = 'Current Player: ';
var rollingDice = '     Rolling.....    ';
var pleaseRoll = 'Roll the die...';
var startGame = 'Start Game';
var seeInstructions = 'See Instructions';

//You do not need to change anything below this line

//Script to make sure the layers can be viewed with all browsers
//Taken from The JavaScript Source http://javascript.internet.com/bgeffects/write-layer.html
//I have made a few changes so that in addition to showing/hiding the layers it can be used to move the players

var ie = (document.all)?true:false;
var ns4 = (document.layers)?true:false;
var ns6 = ((document.getElementById)?true:false) && !ie;

function refPlayer(name){
if (ie) {return eval('document.all.'+name+'.style');}
if (ns4) {return document.layers[name];}
if (ns6 && !ie) {return eval('document.getElementById("'+name+'").style');}
}

function capaON(name){
refPlayer(name).visibility = 'visible';
}

function capaOFF(name){
refPlayer(name).visibility = 'hidden';
}

function writetolayer(name,txt) {
if (ie) {
document.all[name].innerHTML = txt;
}
if (ns4) {
document[name].document.write(txt);
document[name].document.close();
}
if (ns6) {
over = document.getElementById([name]);
range = document.createRange();
range.setStartBefore(over);
domfrag = range.createContextualFragment(txt);
while (over.hasChildNodes()) {
over.removeChild(over.lastChild);
}
over.appendChild(domfrag);
}
capaON(name)
}

//Global variables

var p1x=35;
var p1y=520;
var p2x=60;
var p2y=520;
var p3x=35;
var p3y=495;
var p4x=60;
var p4y=495;

var d1;
var headback;
var ctr=0;
var goOnMoving=true;
var possibleWinner=false;

var qIdx  = 0;
var nextQ = '';
var answered;
var numPlayers = 1;
var nextPlayer=false;

var Player1='';
var Player2='';
var Player3='';
var Player4='';

//Function which defines which layer is moved where

function whichX(){
if(currentPlayer=="one"){return p1x}
if(currentPlayer=="two"){return p2x}
if(currentPlayer=="three"){return p3x}
if(currentPlayer=="four"){return p4x}
}

function whichY(){
if(currentPlayer=="one"){return p1y}
if(currentPlayer=="two"){return p2y}
if(currentPlayer=="three"){return p3y}
if(currentPlayer=="four"){return p4y}
}

function showPlayer(name){
refPlayer(name).left = whichX();
refPlayer(name).top = whichY();
}

function mouCapa(){
var currenty=whichY();
if(currenty==520||currenty==400||currenty==280||currenty==160||currenty==495||currenty==375||currenty==255||currenty==135)
mouCapaRight()
else
mouCapaLeft()
}

function mouCapaRight(){
var currentx=whichX();
if(currentx<455){
moveforward();
}
else{
moveup();
}
showPlayer(currentPlayer);
d1--;
if(d1>0){
setTimeout("mouCapa()", 150);
}
else{
buildQuestion();
}
}

function mouCapaLeft(){
var currentx=whichX();
var currenty=whichY();
if(currenty==100||currenty==75)
{if(currentx==95||currentx==120)
{writetolayer('questions',lastQuestion1 + who() + lastQuestion2)
d1=0;
possibleWinner=true;
}
}
if(currentx>94){
moveback();
}
else{
moveup();
}
showPlayer(currentPlayer);
d1--;
if(d1>0){
setTimeout("mouCapa()", 150);
}
else{
buildQuestion();
}
}

function moveforward(){
switch (currentPlayer){
case "one":
p1x += 60;
break;
case "two":
p2x += 60;
break;
case "three":
p3x += 60;
break;
case "four":
p4x += 60;
break;
}
}

function moveback(){
switch (currentPlayer){
case "one":
p1x -= 60;
break;
case "two":
p2x -= 60;
break;
case "three":
p3x -= 60;
break;
case "four":
p4x -= 60;
break;
}
}

function movedown(){
switch (currentPlayer){
case "one":
p1y += 60;
break;
case "two":
p2y += 60;
break;
case "three":
p3y += 60;
break;
case "four":
p4y += 60;
break;
}
}

function moveup(){
switch (currentPlayer){
case "one":
p1y -= 60;
break;
case "two":
p2y -= 60;
break;
case "three":
p3y -= 60;
break;
case "four":
p4y -= 60;
break;
}
}

//When the student answers a question correctly

function ladders(){
laddergo=0;
if(possibleWinner==true){
	play_snd("win");
	var newGame = confirm(winner1 + who() + winner2);
	if(newGame)document.location.href=document.location.href+"?";
	else window.close();
}
var currentx=whichX();
var currenty=whichY();
if(currentx==155||currentx==180){
if(currenty==520||currenty==495){
ladderOne();
laddergo=1;
}
}
if(currentx==455||currentx==480){
if(currenty==400||currenty==375){
ladderTwo();
laddergo=1;
}
}
if(currentx==215||currentx==240){
if(currenty==340||currenty==315){
ladderThree();
laddergo=1;
}
}
if(currentx==35||currentx==60){
if(currenty==220||currenty==195){
ladderFour();
laddergo=1;
}
}
if(currentx==35||currentx==60){
if(currenty==460||currenty==435){
extraTurn();
}
}
if(currentx==335||currentx==360){
if(currenty==100||currenty==75){
extraTurn();

}
}
if(currentx==335||currentx==360){
if(currenty==220||currenty==195){
extraTurn();
}
}
if(currentx==215||currentx==240){
if(currenty==160||currenty==135){
extraTurn();
}
}
laddergo?play_snd("ladder"):play_snd("right");
stayPut();
}

function ladderOne(){
switch (currentPlayer){
case "one":
p1x=95;
p1y=400;
break;
case "two":
p2x=120;
p2y=400;
break;
case "three":
p3x=95;
p3y=375;
break;
case "four":
p4x=120;
p4y=375;
break;
}
}

function ladderTwo(){
switch (currentPlayer){
case "one":
p1x=395;
p1y=280;
break;
case "two":
p2x=420;
p2y=280;
break;
case "three":
p3x=395;
p3y=255;
break;
case "four":
p4x=420;
p4y=255;
break;
}
}

function ladderThree(){
switch (currentPlayer){
case "one":
p1x=275;
p1y=160;
break;
case "two":
p2x=300;
p2y=160;
break;
case "three":
p3x=275;
p3y=135;
break;
case "four":
p4x=300;
p4y=135;
break;
}
}

function ladderFour(){
switch (currentPlayer){
case "one":
p1x=95;
p1y=100;
break;
case "two":
p2x=120;
p2y=100;
break;
case "three":
p3x=95;
p3y=75;
break;
case "four":
p4x=120;
p4y=75;
break;
}
}

function extraTurn(){
play_snd("right");
switch (currentPlayer){
case "one":
extraTurn1();
break;
case "two":
extraTurn2();
break;
case "three":
extraTurn3();
break;
case "four":
extraTurn4();
break;
}
}

function extraTurn1(){
alert(anotherTurn+ '' +Player1+' !')
currentPlayer="four";
document.current.player.value="four";
document.images['currentplayer'].src="player4.gif";
}
function extraTurn2(){
alert(anotherTurn+ '' +Player2+' !')
currentPlayer="one";
document.current.player.value="one";
document.images['currentplayer'].src="player1.gif";
}

function extraTurn3(){
alert(anotherTurn+ '' +Player3+' !')
currentPlayer="two";
document.current.player.value="two";
document.images['currentplayer'].src="player2.gif";
}

function extraTurn4(){
alert(anotherTurn+ '' +Player4+' !')
currentPlayer="three";
document.current.player.value="three";
document.images['currentplayer'].src="player3.gif";
}

//When the student answers a question incorrectly

function snakes(){
startOver();
var snake=1;
var currentx=whichX();
var currenty=whichY();
if(goOnMoving==true){
	if(currentx==155||currentx==180){
		if(currenty==400||currenty==375){
			snakeOne();
			snake=0;
		}
	}
	if(currentx==335||currentx==360){
		if(currenty==340||currenty==315){
			snakeTwo();
			snake=0;
		}
	}
	if(currentx==395||currentx==420){
		if(currenty==100||currenty==75){
			snakeThree();
			snake=0;
		}
	}
	if(currentx==155||currentx==180){
		if(currenty==100||currenty==75){
			snakeFour();
			snake=0;
		}
	}
	if(snake==1){
		goBackCapa();
		play_snd("wrong");
	}else{
		play_snd("snake");
	}
}
}

function stayPut(){
showPlayer(currentPlayer);
goOnMoving=false;
changePlayer();
}

function startOver(){
var currentx=whichX();
var currenty=whichY();
if(currentx==35||currentx==60){
if(currenty==460||currenty==435){
toFirstSquare()
}
}
if(currentx==335||currentx==360){
if(currenty==100||currenty==75){
toFirstSquare()
}
}
if(currentx==335||currentx==360){
if(currenty==220||currenty==195){
toFirstSquare()
}
}
if(currentx==215||currentx==240){
if(currenty==160||currenty==135){
toFirstSquare()
}
}
}

function snakeOne(){
switch (currentPlayer){
case "one":
p1x=275;
p1y=520;
break;
case "two":
p2x=300;
p2y=520;
break;
case "three":
p3x=275;
p3y=495;
break;
case "four":
p4x=300;
p4y=495;
break;
}
stayPut();
}

function snakeTwo(){
switch (currentPlayer){
case "one":
p1x=455;
p1y=520;
break;
case "two":
p2x=480;
p2y=520;
break;
case "three":
p3x=455;
p3y=495;
break;
case "four":
p4x=480;
p4y=495;
break;
}
stayPut();
}

function snakeThree(){
switch (currentPlayer){
case "one":
p1x=455;
p1y=220;
break;
case "two":
p2x=480;
p2y=220;
break;
case "three":
p3x=455;
p3y=195;
break;
case "four":
p4x=480;
p4y=195;
break;
}
stayPut();
}

function snakeFour(){
switch (currentPlayer){
case "one":
p1x=35;
p1y=340;
break;
case "two":
p2x=60;
p2y=340;
break;
case "three":
p3x=35;
p3y=315;
break;
case "four":
p4x=60;
p4y=315;
break;
}
stayPut();
}

function toFirstSquare(){
play_snd("snake");
switch (currentPlayer){
case "one":
p1x=35;
p1y=520;
break;
case "two":
p2x=60;
p2y=520;
break;
case "three":
p3x=35;
p3y=495;
break;
case "four":
p4x=60;
p4y=495;
break;
}
stayPut();
}

function goBackCapa(){
var currenty=whichY();
if(currenty==520||currenty==400||currenty==280||currenty==160||currenty==495||currenty==375||currenty==255||currenty==135)
goBackCapaLeft()
else
goBackCapaRight()
}

function goBackCapaRight(){
var currentx=whichX();
if(currentx<455){
moveforward();
}
else{
movedown();
}
showPlayer(currentPlayer);
headback--;
if(headback>0){
setTimeout("goBackCapa()", 150);
}
else
stayPut();
}

function goBackCapaLeft(){
var currentx=whichX();
if(currentx>94){
moveback();
}
else{
movedown();
}
showPlayer(currentPlayer);
headback--;
if(headback>0){
setTimeout("goBackCapa()", 150);
}
else
stayPut();
}

//Sets the number of players

function howManyPlayers(n){
capaON("one")
Player1=prompt(ask4Name+player+'1',player+'1')
if(n>=2){
capaON("two")
Player2=prompt(ask4Name+player+'2',player+'2')}
else{
capaOFF("two")
}
if(n>=3){
capaON("three")
Player3=prompt(ask4Name+player+'3',player+'3')}
else{
capaOFF("three")
}
if(n>=4){
capaON("four")
Player4=prompt(ask4Name+player+'4',player+'4')}
else{
capaOFF("four")
}
capaON("dice")
numPlayers=n;
play_snd("gong");
capaOFF('questions')

writetolayer('cp',Player1);
}

//Dice Roller Script by Brian Gosselin http://scriptasylum.com/games/dice/dice.html
//I have modified it so it works with just one die instead of two

var images=new Array();
for(i=0;i<=6;i++){
images[i]=new Image(); images[i].src='dice'+i+'.gif';
}

function rolldice(snd){
if(answered==0){
remindPlayer();
}
else{
if(snd)play_snd("dice");
document.f.t.value='';
document.f.b.value= rollingDice;
writetolayer('questions','');
capaOFF('questions')
if(ctr<6){
showdice();
ctr++;
setTimeout('rolldice(0)',150);
}else{
ctr=0;
showdice();
document.f.t.value=d1;
document.f.b.value= pleaseRoll;
goOnMoving=true;
possibleWinner=false;
mouCapa();
}
}
}

function showdice(){
d1=Math.floor(Math.random()*6+1);
headback=d1;
document.images['d1'].src='dice'+d1+'.gif';
}

//This part of the script deals with the questions
//It is based on an example of an online test which can be found in O'Reilly's JavaScript Application Cookbook http://www.oreilly.com/catalog/jscook/



function buildQuestion() {
nextQ = '<form><B>' + units[qIdx].question + '</B><BR><BR>' +
'<input type="radio" value="a" id="a" onclick="gradeTest(\'a\')"><label for="a">' + units[qIdx].a + '</label><BR>' +
'<input type="radio" value="b" id="b" onclick="gradeTest(\'b\')"><label for="b">' + units[qIdx].b + '</label><BR>' +
'<input type="radio" value="c" id="c" onclick="gradeTest(\'c\')"><label for="c">' + units[qIdx].c + '</label><BR>' +
'<input type="radio" value="d" id="d" onclick="gradeTest(\'d\')"><label for="d">' + units[qIdx].d + '</label></FORM>';
writetolayer('questions',nextQ)
answered=0;
}

function gradeTest(val) {
  var cor=units[qIdx].answer==val?1:0;
  if (cor){
    congratulatePlayer();
    ladders();
  }else{
    sorry();
    snakes();
  }
  qIdx==units.length-1?qIdx=0:qIdx++;
  answered=1;
}


function who(){
if(currentPlayer=="one"){return Player1}
if(currentPlayer=="two"){return Player2}
if(currentPlayer=="three"){return Player3}
if(currentPlayer=="four"){return Player4}
}
function congratulatePlayer(){
writetolayer('questions','<div class="close"><a href="javascript: capaOFF(\'questions\')">close</a></div><p class="welldone">'+wellDone+'<br>'+who()+'!</p>'+(units[qIdx].support.indexOf("The correct answer was ")!=-1?"":units[qIdx].support))
}

function remindPlayer(){
alert(pleaseAnswer+who())

}

function sorry(){
writetolayer('questions','<div class="close"><a href="javascript: capaOFF(\'questions\')">close</a></div><p class="welldone">'+sorryMsg+'</p>'+(units[qIdx].support));
}

// For construction new question objects
function question(answer, support, question, a, b, c, d) {
  this.answer = answer;
    this.support = support;
  this.question = question;
  this.a = a;
  this.b = b;
  this.c = c;
  this.d = d;
  return this;
  }

// The array of questions, answers, options, and explanations
//  THE PLAY STATION @ UVSC CONVERSION PROCESS //
//loop thorough our questions.js file populateing it wit our formmat

function shuffle() {
  for (var i = 0; i < units.length; i++) {
    var j = Math.floor(Math.random() * units.length);
    var tempUnit = units[i];
    units[i] = units[j];
    units[j] = tempUnit;
  }

  if(!document.location.search)capaON('instructions');
}

function loader(){
  units = new Array();

  for (var ps=0; ps<ques.length; ps++){
    units[ps]=new question(answer[ps].toLowerCase(), (genf[ps]!=undefined?genf[ps]:"The correct answer was "+window[answer[ps].toLowerCase()+"ans"][ps]), ques[ps], aans[ps], bans[ps], cans[ps], dans[ps]);
  }
  shuffle();
}
