// VARIABLES DECLARATIONS
//-------------------------------------------------------------------------------

var question = 0;             // int  Index of the country to find.
var game_started = false;     // bool True if the game is in progress.
var num_left = 0;             // int  Num of countries you still have to find.
var num_tot = 0;              // int  Total num of countries you are playing with.
var score = 0;                // int  Player's score.
var right_answers = 0;
var wrong_answers = 0;
var passed_answers = 0;
var process = false;
var countries = 33;
var myTime = "0:0"

var left_array = new Array(countries);  // True if you still have to find the country.
for(var i=0; i<countries; i++) left_array[i]=false;   // Initialized to false.

var passed_array = new Array(countries);  // True if you passed the country.
for(var i=0; i<countries; i++) passed_array[i]=false;   // Initialized to false.

var names = new Array(countries);             // Names of the countries.
for(var i=0; i<countries; i++) names[i]="";   //  Initialized with empty strings.
// ARRAY
names[0]="the Cyclades";
names[1]="Paros";
names[2]="the Aegean Sea";
names[3]="the Ionian Sea";
names[4]="the Myrtoum Sea";
names[5]="Cythera";
names[6]="Melos";
names[7]="Zacythos";
names[8]="Arcadia";
names[9]="Achaea";
names[10]="Elis";
names[11]="Argolis";
names[12]="Messenia";
names[13]="Laconia";
names[14]="Euboea";
names[15]="Andros";
names[16]="Cephalonia";
names[17]="Corcyra";
names[18]="Attica";
names[19]="Boeotia";
names[20]="Aetolia";
names[21]="Epirus";
names[22]="Thessaly";
names[23]="Macedonia";
names[24]="Thebes";
names[25]="Corinth";
names[26]="Athens";
names[27]="Argos";
names[28]="Sparta";
names[29]="Messene";
names[30]="Olympia";
names[31]="Scyros";
names[32]="Mount Olympus";
names[33]="Thermopylae";

//-------------------------------------------------------------------------------
var details = new Array(countries);         // Individual Info pages for IFRAME
details[0]="iframes/cyclades.html"
details[1]="iframes/paros.html"
details[2]="iframes/aegean.html"
details[3]="iframes/ionian.html"
details[4]="iframes/myrtoum.html"
details[5]="iframes/cythera.html"
details[6]="iframes/melos.html"
details[7]="iframes/zacthos.html"
details[8]="iframes/arcadia.html"
details[9]="iframes/achaea.html"
details[10]="iframes/elis.html"
details[11]="iframes/argolis.html"
details[12]="iframes/messenia.html"
details[13]="iframes/laconia.html"
details[14]="iframes/euboea.html"
details[15]="iframes/andros.html"
details[16]="iframes/cephalonia.html"
details[17]="iframes/corcyra.html"
details[18]="iframes/attica.html"
details[19]="iframes/boeotia.html"
details[20]="iframes/aetolia.html"
details[21]="iframes/epirus.html"
details[22]="iframes/thessaly.html"
details[23]="iframes/macedonia.html"
details[24]="iframes/thebes.html"
details[25]="iframes/corinth.html"
details[26]="iframes/athens.html"
details[27]="iframes/argos.html"
details[28]="iframes/sparta.html"
details[29]="iframes/messene.html"
details[30]="iframes/olympia.html"
details[31]="iframes/scyros.html"
details[32]="iframes/olympus.html"
details[33]="iframes/thermophylae.html"
//-------------------------------------------------------------------------------
var rand = new RandomNumberGenerator();         // Generates random numbers.
// FUNCTION : PICK COUNTRY
//-------------------------------------------------------------------------------
function pick_country() {
  if (num_left>0)
  {  var pick = Math.ceil( num_left * rand.next() );   // random country.
     for (var i=0; i<countries; i++)
     {   if (left_array[i] == true)
         {   pick--;
             if (pick < 1)
             {   question = i;
                 num_left--;
                 left_array[i] = false;
                 break;
     }   }   }
     update_stats();
     add_to_message ( names[question] );
  }
  else { play_passed();  }
}
//-------------------------------------------------------------------------------
function play_passed() {
  message("");
  for (var i=0; i<countries; i++)
  {   if (true == passed_array[i] )
      {   passed_array[i] = false;  left_array[i] = true;  num_left++;  }
  }
  var st = "Do you want to give another shot\nat the countries you passed ?";
  if ( (num_left > 0) && confirm( st ))
  {   message("Find ");  pick_country();  }
  else
  {   game_over();  }
}
//-------------------------------------------------------------------------------
function game_over() {
  message ( "Game Over.  Click 'New Game' to Start" )
  num_left--;  game_started = false;
  StopTimer();
  update_stats();
  showStatistics();
}
//-------------------------------------------------------------------------------
// FUNCTION : NEW GAME
//-------------------------------------------------------------------------------
function new_game() {
  if (game_started)                                // Ends the current game.
  {   if (confirm ("Are you sure you want to quit this game ?"))
      {   game_over();  }
  }
  else
  {   game_started = true;
      var process = false;
      score = 0;
      right_answers = 0;
      wrong_answers = 0;
      passed_answers = 0;
      num_tot = 0;
      for(var i=0; i<countries; i++) {  left_array[i] = false;  }
      for(var i=0; i<countries; i++) {  passed_array[i] = false;  }

      enable_cont( 0, 32); num_tot += countries;

      num_left = num_tot;
      if (num_left == 0)
      {   alert("Select at least one continent and click 'New Game'");
          game_started = false;
      }
      else
      {   message( "Find " );
          pick_country();
  }   }
}

// FUNCTION : CHECK ANSWER
//-------------------------------------------------------------------------------
function checkanswer (answer) {
  if (process == false)
  {
      process = true;
      if (game_started)
      {   if (answer == question)  {  right_answers++;  right_answ();  }
          else  {  wrong_answers++;  wrong_answ( answer );  }
      }
      else
      {   message( "This is " + names[answer] )  }
      process = false;
  }
//   Added line to put appropriate info page in IFRAME
     document.getElementById("info").src  = details[answer];
}
//-------------------------------------------------------------------------------
function right_answ() {
  message( "Right! Now Find " );
  score += 100;  pick_country();
}
//-------------------------------------------------------------------------------
function wrong_answ( answer ) {
  message ( "This is " + names[answer] + ". Find " + names[question] );
  score -= 20;  myTime +=10; update_stats()
}
//-------------------------------------------------------------------------------
// FUNCTION : PASS
//-------------------------------------------------------------------------------
function pass () {
  if (game_started)
  {   if ( confirm("Pass this country?") )
      {   passed_array[question] = true;
          passed_answers++;
          message ( "Find " );
          pick_country();
  }   }
}
// FUNCTION : UPDATE STATISTICS
//-------------------------------------------------------------------------------
function update_stats () {
  document.display.m_score.value = ("" + score);
  document.display.m_left.value  = ("" + (num_left+1) + " of " + num_tot);
}
// FUNCTION : ENABLE CONTINENT
//-------------------------------------------------------------------------------
function enable_cont (n1, n2) {
  for (var i = n1; i <= n2; i++)
  {   left_array[i] = true;   }
}
//-------------------------------------------------------------------------------
function format ( string, num ) {
  answer = "" + string;
  for (i = answer.length; i < num; i++ )
  {   answer = "  " + answer;  }
  return( answer );
}
// FUNCTION : RANDOM NUMBER GENERATOR
//-------------------------------------------------------------------------------
function NextRandomNumber()  {
  var hi   = this.seed / this.Q;
  var lo   = this.seed % this.Q;
  var test = this.A * lo - this.R * hi;
  if (test > 0)
    this.seed = test;
  else
    this.seed = test + this.M;
  return (this.seed * this.oneOverM);
}
function RandomNumberGenerator() {
  var d = new Date();
  this.seed = 2345678901 + (d.getSeconds() * 0xFFFFFF) + (d.getMinutes() * 0xFFFF);
  this.A = 48271;
  this.M = 2147483647;
  this.Q = this.M / this.A;
  this.R = this.M % this.A;
  this.oneOverM = 1.0 / this.M;
  this.next = NextRandomNumber;
  return this;
}

function message( string ) {
  document.display.m_message.value = string;
}

function add_to_message ( string ) {
  document.display.m_message.value += string;
}

//End of MAP Script -- Opacity follows
function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}
//change the opacity for different browsers

function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}
//TIMER Script Follows
// please keep these lines on when you copy the source
// made by: Nicolas - http://www.javascript-page.com

var timerID = 0;
var tStart  = null;

function UpdateTimer() {
   if(timerID) {
      clearTimeout(timerID);
      clockID  = 0;
   }

   if(!tStart)
      tStart   = new Date();

   var   tDate = new Date();
   var   tDiff = tDate.getTime() - tStart.getTime();

   tDate.setTime(tDiff);

   myTime = + tDate.getMinutes() + ":"
            + tDate.getSeconds();

   document.display.clock.value = ""
                                   + tDate.getMinutes() + ":"
                                   + tDate.getSeconds();

   timerID = setTimeout("UpdateTimer()", 1000);
}

function StartTimer() {
   tStart   = new Date();

   document.display.clock.value = "0:0";

   timerID  = setTimeout("UpdateTimer()", 1000);
}

function StopTimer() {
   if(timerID) {
      clearTimeout(timerID);
      timerID  = 0;
   }

   tStart = null;
}

// FUNCTION : STATS
//-------------------------------------------------------------------------------
function showStatistics(){
  document.getElementById("info").src  = "iframes/hide.html";
  document.details.style.visibility = "hidden";
  document.stats.style.visibility = "visible";
  document.stats.q1.value = (num_tot);
  document.stats.r1.value = (right_answers);
  document.stats.w1.value = (wrong_answers);
  document.stats.p1.value = (passed_answers);
  document.stats.t1.value = (myTime);
  document.stats.m1.value = (100*num_tot);
  document.stats.s1.value = (score);
  var p = .1;
    if (right_answers != 0) { p = .01 * Math.round (100 * score / right_answers); }
  else        { p = 0; }
  document.stats.c1.value = (p+"%");
  }

function hideAll() {
  document.getElementById("info").src  = "iframes/hide.html";
  document.details.style.visibility = "hidden";
  document.stats.style.visibility = "hidden";
  document.details.style.visibility = "hidden";
  document.info.style.visibility = "hidden";
}

function showDetails() {
  document.getElementById("info").src  = "iframes/show.html";
  document.stats.style.visibility = "hidden";
  document.details.style.visibility = "visible";
}

function showRules() {
  document.getElementById("info").src  = "iframes/rules.html";
  document.stats.style.visibility = "hidden";
  document.details.style.visibility = "visible";
}

// FUNCTION : MOVE CONTROL PANELS BACK TO ORIGINAL POS AFTER END GAME
//-------------------------------------------------------------------
function endReset1(obj, mvTop1, mvLeft1) {
	obj.style.position = "absolute";
	obj.style.top = mvTop1;
	obj.style.left = mvLeft1;
}
function endReset2(obj, mvTop2, mvLeft2) {
	obj.style.position = "absolute";
	obj.style.top = mvTop2;
	obj.style.left = mvLeft2;
}

