/*
 * Functions used by the map representation. Using JQuery plugins.
 */

/*
 * Initiate TaffyDB
 */

var mapDB = TAFFY();

/*
 * Global position for board
 */
var x_position = 0;
var y_position = 0;

/*
 * Create XMLHttpRequest object for all browsers.
 */
var request = false;

try {
   request = new XMLHttpRequest();
} catch (trymicrosoft) {
   try {
      request = new ActiveXObject("Msxml2.XMLHTTP");
   } catch (othermicrosoft) {
      try {
         request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (failed) {
         request = false;
      }  
   }
}

if (!request) {
   alert("Error initializing XMLHttpRequest!");
}

function handleTest() {
   if (request.readyState == 4) {
      if (request.status == 200) {
         var response = request.responseText;

         alert(response);
      }
      else if (request.status == 404) {
         alert("Request URL does not exist");
      }
      else {
         alert("Error: status code is " + request.status);
      }
   }
}

var url = "map_update.php";
request.open("POST", url, true);
request.onreadystatechange = handleMapUpdate;
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.send(null);

function changeBoard(records) {
   x_max = 4;
   x_min = -4;
   y_max = 10;   

   x = x_min;
   y = y_max; 

   // loop through records and modify class attributes
   records.each(function (r){
      // ids
      var id = x + "_" + y;
      var b_id = "b" + id;
      //current objects
      var old_b = $("#"+b_id);              
      var old_f = $("#"+id);
      //check classes
      var br = old_b.hasClass("br");
      var firstodd = old_b.hasClass("firstodd");
      var odd = old_b.hasClass("odd");
      var even = old_b.hasClass("even");

      var c = "";

      //find position classes
      if (br) { 
         c = c + " br";
      }
      if (firstodd) {
         c = c + " firstodd";
      }
      if (odd) {
         c = c + " odd";
      }
      if (even) {
         c = c + " even";
      }
   
      var class_b = c + " " + r.bclasses;
      var class_f = c + " " + r.classes;

      $("#"+b_id).attr("class", class_b);
      $("#"+id).attr("class", class_f);

      var animation = r.bimage;
      if (animation) {
         $("#"+b_id).children("img").attr("src", animation);
      }

      //edit x and y ...
      if (x >= x_max) {
         x = x_min;
         y--;
      }
      else {
         x++;
      }
   })
}

function getBoard() {
   var x_left = x_position - 4;
   var x_right = x_position + 4;
   var y_top = y_position + 10;
   var y_bottom = y_position - 10;

   var records = mapDB( {y:{lte:y_top}}, {y:{gte:y_bottom}}, {x:{gte:x_left}}, {x:{lte:x_right}} );
   
   return records;
}

function handleMapUpdate() {
   if (request.readyState == 4) {
      if (request.status == 200) {
         var response = request.responseText;

         // converts the JSON string to a JavaScript object
         // and set it to a variable
         // eval("json="+response);
         
         // insert json response into database
         mapDB.insert(response);
         
         // create selection for map based on (x_position|y_position)
         //var records = mapDB( {y:{lte:1}}, {y:{gte:-1}}, {x:{gte:-1}}, {x:{lte:1}} );
         //var records = mapDB( {y:{lte:20}}, {y:{gte:-20}}, {x:{gte:-8}}, {x:{lte:8}} );

         records = getBoard();

         //update board
         changeBoard(records);
      }
      else if (request.status == 404) {
         alert("Request URL does not exist");
      }
      else {
         alert("Error: status code is " + request.status);
      }
   }
}

$(function() {
   $(".front").mouseover(function() {
      var id = this.id;
      var coordinates = id.split("_");
      var x_coord = coordinates[0];
      var y_coord = coordinates[1];
      var str = "(".concat(x_coord).concat("|").concat(y_coord).concat(")");
      document.getElementById('coordinates').innerHTML = str;

      var bclasses = $("#b"+id).attr("class");
      document.getElementById('bclasses').innerHTML = bclasses;

      var fclasses = $("#"+id).attr("class");
      document.getElementById('fclasses').innerHTML = fclasses;
   });
});

$(function() {
   $("#frametop").click(function() {
      // move 2 steps to keep board in line.
      y_position++;
      y_position++;
      var records = getBoard();
      changeBoard(records);
   });
   $("#framebottom").click(function() {
      // move 2 steps to keep board in line.
      y_position--;
      y_position--;
      var records = getBoard();
      changeBoard(records);
   });
   $("#frameleft").click(function() {
      x_position--;
      var records = getBoard();
      changeBoard(records);      
   });
   $("#frameright").click(function() {
      x_position++;
      var records = getBoard();
      changeBoard(records);
   });
});

