
function init(page, lang, error, focus) {
  // Mostrar mensaje de error, si se ha especificado
  if (error)
    alert(error);
    
  // Dar el foco al elemento especificado, en su caso
  if (focus)
    document.getElementById(focus).focus();

  // Evento para guardar cookie al cambiar de idioma
  document.getElementById('change-lang').onclick = function() {
    newLang = lang == 'ca' ? 'es' : 'ca';
    document.cookie = 'lang=' + newLang + '; expires=' + new Date(31536000000000).toGMTString() + '; path=/';
    return true;
  }
  
  // Evento para mostrar/ocultar mapa
  showMap = document.getElementById('show-map');
  mapDiv = document.getElementById('map');
  showMap.onclick = toggleMap;

  // Evento para mostrar/ocultar aviso legal
  showLegal = document.getElementById('show-legal');
  legalDiv = document.getElementById('legal');
  showLegal.onclick = toggleLegal;

  // Rellenar div de aviso legal
  var xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest : new ActiveXObject('Microsoft.XMLHTTP');
  xmlhttp.open("GET", '/legal_' + lang + '.xml', true);
  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
      legalDiv.innerHTML = xmlhttp.responseText;
  };
  xmlhttp.send(null);

  if (page == 'index') {
    // Eventos para eliminar texto inicial del campo email de la home
    var emailInput = document.getElementById('email');
    emailInput.onfocus = function() {
      this.style.color = '#eee';
      this.value = '';
      this.onfocus = null;
    }
    emailInput.form.onsubmit = function() {
      if (emailInput.onfocus)
        emailInput.value = '';
      return true;
    }

  } else if (page == 'events') {
    // Evento para mostrar/ocultar aviso legal
    var showPrivacy = document.getElementById('show-privacy');
    if (showPrivacy)
      showPrivacy.onclick = toggleLegal;
    
    // Evento para mostrar/ocultar eventos anteriores
    var showEvents = document.getElementById('show-previous-events');
    if (showEvents) {
      var eventsDiv = document.getElementById('previous-events');
      showEvents.onclick = function() {
        if (eventsDiv.style.display === 'none') {
          showEvents.parentNode.className = 'expanded';
          eventsDiv.style.display = '';
        } else {
          showEvents.parentNode.className = '';
          eventsDiv.style.display = 'none';
        }
        return false;
      };
    }
  }
}


function toggleMap(show) {
  if (typeof show !== 'boolean') {
    show = mapDiv.style.display === 'none';
  } else if (show === (mapDiv.style.display != 'none')) {
    return false;
  }
  if (show) {
    toggleLegal(false);
    showMap.saveText = showMap.innerHTML;
    showMap.innerHTML = 'ocultar mapa';
    mapDiv.style.display = '';
    mapDiv.onclick = toggleMap;
  } else {
    showMap.innerHTML = showMap.saveText;
    mapDiv.style.display = 'none';
    mapDiv.onclick = null;
  }
  return false;
}


function toggleLegal(show) {
  if (typeof show !== 'boolean') {
    show = legalDiv.style.display === 'none';
  } else if (show === (legalDiv.style.display != 'none')) {
    return false;
  }
  if (show) {
    toggleMap(false);
    showLegal.style.color = '#eee';
    showLegal.style.textDecoration = 'underline';
    legalDiv.style.display = '';
    legalDiv.onclick = toggleLegal;
  } else {
    showLegal.style.color = showLegal.style.textDecoration = '';
    legalDiv.style.display = 'none';
    legalDiv.onclick = null;
  }
  return false;
}
