function researchRoom()
{
  var research = document.getElementById('research').value;

  var xhr = new XMLHttpRequest();
  xhr.open('GET', 'api/request.php?research='+encodeURI(research), true);

  xhr.onload = function() {
    var table = document.getElementById('research-results');
    table.innerHTML = '';

    document.getElementById('research-results-table').style.display = 'block';

    res = JSON.parse(xhr.responseText);

    for (var i=0; i<res.length; i++) {
      table.innerHTML += '<tr>\
        <th scope="row">\
          <a href="#' + res[i].id + '">' + res[i].name + '</a>\
        </th>\
        <td>' + '??' + '</td>\
      </tr>';
    }
  };

  xhr.send(null);
}

function httpGetAsync(theUrl, callback)
{
  var xmlHttp = new XMLHttpRequest();
  xmlHttp.onreadystatechange = function() {
    if (xmlHttp.readyState == 4) {
      callback(xmlHttp.responseText, xmlHttp.status);
    }
  }
  xmlHttp.open("GET", theUrl, true);
  xmlHttp.send(null);
}

function selectRoom(roomId) {
  var svg = document.getElementById('map');

  var selectedRooms = map.contentDocument.getElementsByClassName('current-room');
  for (var i = 0; i < selectedRooms.length; i++) {
    selectedRooms[i].classList.remove('current-room');
  }

  var room = svg.contentDocument.getElementById('salle-' + roomId);

  if (room == null) {
    console.log('Error: the room salle-'+roomId+' does not exist');
    return;
  }

  room.classList.add('current-room');

  httpGetAsync("api/request.php?salle="+room.id.substring("salle-".length), function (s,errcode){
    window.clearTimeout(document.wnsTimeout);

    if (s == "404") {
      document.getElementById('room-name').innerText = 'Salle inconnue';
      document.getElementById('room-owners').style.display = 'none';
      document.getElementById('room-description').innerHTML = "Aucune information n'est disponible pour cette salle. Si vous en avez, indiquez-les à l'adresse <a href=mailto:samy.avrillon@ens-lyon.fr>samy.avrillon@ens-lyon.fr</a> en indiquant l'identifiant de la salle : " + roomId;
      return;
    }

    salle = JSON.parse(s)
    if (room.id == "salle-" + salle["id"]) {
      if (errcode == 200) {
        titres = ((salle.aliaz.length!=0)?(salle.aliaz.join(' ou ')):"Aucun alias connu.");
        document.getElementById('room-name').innerText = titres;
        document.getElementById('room-description').innerText = salle.description;

        if (salle.locataires.length == 0) {
          document.getElementById('room-owners').style.display = 'none';
          document.getElementById('room-owners-list').innerText = '';
        } else {
          document.getElementById('room-owners').style.display = 'block';
          document.getElementById('room-owners-list').innerText = salle.locataires.join(", ");
        }

      } else {
        console.log("Error " + errcode);
      }
    }
  });
}

// Update the current room based on the current anchor
function updateCurrentRoom()
{
  // Hide room research modal
  var modal = bootstrap.Modal.getInstance(document.getElementById('room-research-modal'));
  if (modal != null) {
    modal.hide();
  }

  var id = window.location.hash.substring(1); // Remove #
  selectRoom(id);
}

function initSvgSupport()
{
  var map = document.getElementById('map');

  window.panZoom = svgPanZoom(map, {
    zoomEnabled: true,
    controlIconsEnabled: true
  });
  panZoom.fit();
  panZoom.center();

  var salles = map.contentDocument.querySelectorAll('#salles-group path');

  for (var i = 0; i < salles.length; i++) {
    salles[i].addEventListener('click', function(e) {
      var id = e.target.id.substring('salle-'.length); // Remove prefix
      selectRoom(id);
    });
  }
}


window.addEventListener('load', function() {
  // Init SVG
  var map = document.getElementById('map');
  if (map.contentDocument == null) {
  	map.addEventListener('load', initSvgSupport);
  } else {
  	initSvgSupport();
  }

  // Update the current room
  if (window.location.hash != "") {
    updateCurrentRoom();
  }
});

window.onhashchange = function() {
  updateCurrentRoom();
};

window.addEventListener('resize', function() {
  window.panZoom.resize();
  window.panZoom.fit();
  window.panZoom.center();
});
