// menu functions

function toggleWalkable() {
  $("walk_img").style.display = ($("walk_img").style.display == 'block') ? 'none' : 'block';
}

function toggleTerrain() {
  $("gmaps_img").style.display = ($("gmaps_img").style.display == 'block') ? 'none' : 'block';
}

function resetPath() {
  $("msg").innerHTML = "Click on a start location";
  $("overlay_img").innerHTML = '';
  $("start_img").style.display = 'none';
  $("end_img").style.display = 'none';
  startLoc = "";
  endLoc = "";
}

function expand() {
  $("expand_btn").style.display = 'none';
  $("expanded").style.display = 'inline';
}

function collapse() {
  $("expand_btn").style.display = 'inline';
  $("expanded").style.display = 'none';
}

// helper function for referencing IDs (similar to jQuery library)
function $(id) {
  return document.getElementById(id);
}

// path finding functions

var startLoc = 0;
var endLoc = 0;
var executing = false;

function doStartLoc(event) {
  if (startLoc != "") {
    doEndLoc(event);
    return;
  }
  if (executing) {
      alert("Processing...");
      return;
  }

  testLoc = mouseCoords(event || window.event);
  executing = true;
  doXMLHTTPAction(setStartLoc, "validate.php?x="+testLoc.x+"&y="+testLoc.y);
}

function doEndLoc(event) {
  if (endLoc != "" || executing) {
    alert("Processing...");
    return;
  }
  testLoc = mouseCoords(event || window.event);
  executing = true;
  doXMLHTTPAction(setEndLoc, "validate.php?x="+testLoc.x+"&y="+testLoc.y);
}

// AJAX callback methods

function setStartLoc(xy) {
  executing = false;
  if (xy == "invalid") {
    alert("Invalid start location.\n\nYou must click on walkable terrain. To view the walkable area, select (more) from the menu on the upper left and click Show Walkable.\n\nPlease try again.");
    return;
  }
  startLoc = xy.split(",");
  x = startLoc[0];
  y = startLoc[1];

  $("start_img").style.display = "block";
  $("start_img").style.left = x+"px";
  $("start_img").style.top = y+"px";

  $("msg").innerHTML = "Click on an end location";
}

function setEndLoc(xy) {
  executing = false;
  if (xy == "invalid") {
    alert("Invalid end location.\n\nYou must click on walkable terrain. To view the walkable area, click Show Walkable.\n\nPlease try again.");
    return;
  }
  endLoc = xy.split(",");
  x = endLoc[0];
  y = endLoc[1];

  $("end_img").style.display = "block";
  $("end_img").style.left = x+"px";
  $("end_img").style.top = y+"px";

  $("msg").innerHTML = "Calculating...";

  doXMLHTTPAction(newPath, "findpath.php?sx="+startLoc[0]+"&sy="+startLoc[1]+"&ex="+endLoc[0]+"&ey="+endLoc[1]);
}

// show translucent path image over campus map
function newPath(val) {
  if (val == "invalid") {
    alert("Unable to calculate path");
    return;
  }
  if (val == "spl") {
    alert("SplHeap extension not available; please use PHP v5.3 or above");
    return;
  }

  $("overlay_img").innerHTML = '<img class="upperLeft" style="z-index:4" src="'+val+'">';
  $("msg").innerHTML = "Found path!";
}

// mouse /GPS coordinates
document.onmousemove = handleMouseMove;

function handleMouseMove(evt) {
  // make compatible with mozilla / IE
  mouseX = (evt == undefined) ? window.event.clientX : evt.pageX;
  mouseY = (evt == undefined) ? window.event.clientY : evt.pageY;
  $("mouse_coords").innerHTML = "("+mouseX+","+mouseY+")";
  $("gps_coords").innerHTML = translate_xy(mouseX,mouseY);
  return false;
}

function mouseCoords(ev){
  if(ev.pageX || ev.pageY){
    return {x:ev.pageX, y:ev.pageY};
  }
  return {
    x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
    y:ev.clientY + document.body.scrollTop  - document.body.clientTop
  };
}

// GPS coordinates taken from corners of lindell, vandeventer, laclede, and grand

// upper left GPS coordinates
var gps_x = 14.453;
var gps_y = 38.326;

// GPS width dist (to lower right)
var gps_dist_x = .479;
var gps_dist_y = .160;

// upper left pixel coordinates
var pixel_x = 387;
var pixel_y = 208;

// lower right pixel coordinates
var pixel_lr_x = 1084;
var pixel_lr_y = 512;

// pixel width dist
var pixel_dist_x = pixel_lr_x-pixel_x; // 696
var pixel_dist_y = pixel_lr_y-pixel_y; // 304

// pixel -> gps factor
var x_factor = gps_dist_x / pixel_dist_x;
var y_factor = gps_dist_y / pixel_dist_y;

// rotational offset (the slu campus is offset by about -15 degrees)
var rot_y = -96/696;
var rot_x = 54/304;

// elevation (oddly enough, it only increases from west to east)
var west_elev = 486; // feet
var east_elev = 540;
var elev_diff = east_elev-west_elev;
var elev_px = elev_diff / 696;

// turn pixels to gps coords (rough approximation)
function translate_xy(x, y) {
  gx = gps_x + ((x-pixel_x) * -x_factor);
  gx += ((y-pixel_y) * rot_x) / 1000;
  str = "W 090&deg; "  + gx.toString().substring(0,6) + "<br>";

  gy = gps_y + ((y-pixel_y) * -y_factor);
  gy += ((x-pixel_x) * rot_y) / 1000;
  str += "N 038&deg; " + gy.toString().substring(0,6) + "<br>";

  elevation = west_elev + (x-pixel_x) * elev_px;

  str += "Elevation: " + elevation.toString().substring(0,3) + " feet";
  return str;
}
