var savedPlz = "";
var map;

/**
 * Initialization. Mandatory function.
 */
function init() {
  if(document.getElementById("gbname").value.length>0)
    document.getElementById("gbtext").focus();
  else
    document.getElementById("gbname").focus();
  plz();
  initMap();
}

/**
 * Checks the form input and submits them if they
 * are correct.
 *
 * @return Whether or not one should proceed submitting the form.
 */
function checkAndSubmit() {
  // There has to be a name.
  var name = document.getElementById("gbname").value;
  if(name.length==0) {
    alert("Es muss ein Name angegeben werden.");
    document.getElementById("gbname").focus();
    return false;
  }

  // There has to be a text.
  var text = document.getElementById("gbtext").value;
  if(text.length==0) {
    alert("Bitte einen Text eingeben.");
    document.getElementById("gbtext").focus();
    return false;
  }

  // Ok. Submit the form.
  return true;
}

/**
 * Looks up the postal code on the server and displays its
 * city name.
 */
function plz() {
  var city = document.getElementById("city");

  // Do we have a postal code at all?
  var plz = document.getElementById("plz").value.replace(/ +/g,"");
  if(plz.length==0) {
    city.innerHTML = "";
  } else {
    // Has the postcal code changed?
    if(plz!=savedPlz) {
      savedPlz = plz;

      // Perform server request.
      var xmlHttp = new XMLHttpRequest();
      xmlHttp.open("GET","plz.php?plz="+plz,true);
      xmlHttp.onreadystatechange = function() {
        if(xmlHttp.readyState == 4) {
          city.innerHTML = xmlHttp.responseText;
        }
      }
      xmlHttp.send(null);
    }
  }

  setTimeout("plz()",1000);
}

/**
 * Writes an opening email link tag to the page.
 *
 * @param name The part before the at sign.
 * @param domain The part after the at sign with dots replaced by pound characters.
 */
function tagOpen(name,domain) {
  var address = "mai" + "lto:" + name + "@" + domain.replace(/#/g,".");
  document.write('<a href="'+address+'">');
}

/**
 * Writes a closing link tag to the page.
 */
function tagClose() {
  document.write("</a>");
}

/**
 * Initializes the Google map.
 */
function initMap() {
  if(!GBrowserIsCompatible()) {
    alert("Dein Browser kann leider keine Karten anzeigen. Sorry.");
    return;
  }

  // Display the map.
  map = new GMap2(document.getElementById("gbmap"));
  map.addControl(new GSmallMapControl());
  map.addControl(new GMapTypeControl());
  map.setCenter(new GLatLng(51.1104, 10.0195), 5);
  map.myMarkers = new Array();

  // Display the concert locations.
  //var linePoints = [];
  for(var index in concerts)
    if(concerts[index]["latitude"]) {
      var pos = new GLatLng(concerts[index]["latitude"],concerts[index]["longitude"]);
      map.addOverlay(createConcertMarker(index,pos));
      //linePoints.push(pos);
    }
  //map.addOverlay(new GPolyline(linePoints));

  // Display the guestbook entries.
  for(var index in entries)
    if(entries[index]["latitude"]) {
      var pos = new GLatLng(entries[index]["latitude"],entries[index]["longitude"]);
      var marker = createMarker(index,pos);
      map.addOverlay(marker);
      map.myMarkers[index] = marker;
    }
}

/**
 * Creates a marker for a guestbook entry which opens an info window when clicked.
 *
 * @param index The index of the marker.
 * @param pos The position of the marker.
 */
function createMarker(index,pos) {
  var marker = new GMarker(pos);
  GEvent.addListener(marker, "click", function() {
    // Load entry detail from database.
    var id = entries[index]["id"];
    GDownloadUrl("mapentry.php?id="+id, function(data, responseCode) {
      marker.openInfoWindowHtml(data);
      location.href = "#id" + id;
    });
  });
  return marker;
}

/**
 * Opens the marker of a guestbook entry.
 *
 * @param id The ID of the entry.
 */
function openEntry(id) {
  // Search for entry.
  for(var index in entries)
    if(entries[index]["id"]==id)
      break;

  // Open the marker.
  var marker = map.myMarkers[index];
  GDownloadUrl("mapentry.php?id="+id, function(data, responseCode) {
    marker.openInfoWindowHtml(data);
  });
}

/**
 * Creates a marker for a concert which opens an info window when clicked.
 *
 * @param index The index of the marker.
 * @param pos The position of the marker.
 */
function createConcertMarker(index,pos) {
  var icon = new GIcon();
  icon.image = "concerticon.php?key="+concerts[index]["key"];
  icon.iconSize = new GSize(140, 35);
  icon.iconAnchor = new GPoint(4, 18);
  icon.infoWindowAnchor = new GPoint(70, 0);
  var marker = new GMarker(pos,icon);
  GEvent.addListener(marker, "click", function() {
    // Load concert detail from database.
    var id = concerts[index]["key"];
    GDownloadUrl("mapconcert.php?key="+id, function(data, responseCode) {
      marker.openInfoWindowHtml(data);
    });
  });
  return marker;
}

/**
 * Converts all line-breaks into line break HTML tags.
 *
 * @param text Text to be converted.
 * @return The converted text.
 */
function nl2br(text) {
  return text.replace(/\n/,"<br/>");
}

