var mapPoints = new Object();
 
var map = '';
var gd = '';
var gc = '';
var gm_locale = '';
 
var selectedPoint = 0;
 
$(document).ready( function () {

  // Get wanted locale from hidden DIV
  gm_locale = $("div#gm_locale").html();

  map = new GMap2( document.getElementById("map") );
  
  map.setCenter(new GLatLng(61.482323, 23.914168), 5);
  map.enableScrollWheelZoom();
 
  map.addControl(new GSmallMapControl());
  map.addControl(new GMapTypeControl());  
  map.addControl(new GOverviewMapControl());
 
  gd = new GDirections(map, document.getElementById("directions"));
  gc = new GClientGeocoder();
  

  
  var points = 0;
  var show = false;
  $.each(mapPoints, function (id, mapPoint) {
    
    if (mapPoint.googlemaps) {
      points++;
      show = true;
      var point = mapPoint.googlemaps.split(", ");
      mapPoints[id].point = new GLatLng(parseFloat(point[0]), parseFloat(point[1]));
    } 
    else {
      show = false;
      /*
      gc.getLatLng(mapPoint.address, function (gLatLng) {
        mapPoints[id].point = gLatLng;
        $("body").append( mapPoint.name +" ("+ mapPoint.address +") = "+ gLatLng +"<br />");
      });
      */
    }
    
    if (show) {
      mapPoints[id].marker = new GMarker(
        mapPoints[id].point,
        new Object({title: mapPoint.title})
      );
    
      map.addOverlay( mapPoints[id].marker );
      mapPoints[id].marker.bindInfoWindowHtml( createInfoHtml(id) );
    
      // Add to links
      $("#links").append(
        '<a class="showOnMap" id="showOnMap_'+ id +'">'+ mapPoint.title +'</a>'
      );
    
      // Add to route-select
      $("#routeTo").append('<option value="'+ id +'">'+ mapPoint.title +'</option>');
      
      // Point selected, focus & show infowindow
      if (mapPoint.selected) {
        selectedPoint = id;
        map.setCenter(mapPoints[ id ].point, 14);
        setTimeout(showInfoWindow, 1000);
      }
    }
    
    // Tampere -kludge
    if (points == 4) {
      $("#links").append(
        '<a class="showOnMap" id="showOnMap_11052535">- Tampere</a>'
        // + '<a class="showOnMap" id="showOnMap_11031331">- Nastola</a>'
      );
      $("#routeTo").append(
        '<option value="11052535">- Tampere</option>'
        // + '<option value="11031331">- Nastola</option>'
      );
    }  
    
  });
  
  
  // Reset routeTo field
  $("#routeTo").val("");
  
  $(".showOnMap").each( function () {
    var id = (this.id.split("_"))[1];
    $(this).click( function () {
      var mapPoint = mapPoints[ id ];
      map.panTo(mapPoint.point);
      map.setZoom(15);
      mapPoint.marker.openInfoWindowHtml( createInfoHtml(id) );
      $("#routeTo").val( id );
    });
  });

  
  // Show directions and hide links
  // $("#showRoute").click( function () {
  $("#route").submit( function () {
    var mapPoint = mapPoints[ $("#routeTo").val() ];
    var from = $("#routeFrom").val();
    var to = mapPoint.address +", "+ mapPoint.city;
    if (!from || !to) return false;
    var gdQuery = from +" to "+ to;
    gd.load(gdQuery, new Object({"locale": gm_locale}));
    $("#links").hide();
    $("#directions").show();
    return false;
  });

 
  // Hide directions and show links
  $("#hideDirections").click( function () {
    $("#directions").hide();
    gd.clear();
    $("#links").show();
  });
 
});

 
function showInfoWindow () {
  mapPoints[ selectedPoint ].marker.openInfoWindowHtml( createInfoHtml(selectedPoint) );
}
 
function createInfoHtml (id) {
  var mapPoint = mapPoints[id];
  var html = 
    "<b>"+ mapPoint.name +"</b><br />" +
    mapPoint.address +"<br />"+
    (mapPoint.box ? mapPoint.box +"<br />" : "") +
    (mapPoint.zip ? mapPoint.zip +" " : "") + 
    mapPoint.city +"<br />"+
    mapPoint.country +"<br />";
    
  if (gm_locale == 'fi_FI') {
    html += 
      (mapPoint.email ? 'Sähköposti: <a href="mailto:'+ mapPoint.email +'">'+ mapPoint.email +'</a><br />' : "") +
      (mapPoint.telephone ? "Puh. "+ mapPoint.telephone +"<br />" : "") +
      (mapPoint.fax ? "Faksi "+ mapPoint.fax +"<br />" : "");
  }    
  if (gm_locale == 'en_GB') {
    html += 
      (mapPoint.email ? 'e-mail: <a href="mailto:'+ mapPoint.email +'">'+ mapPoint.email +'</a><br />' : "") +
      (mapPoint.telephone ? "Tel. "+ mapPoint.telephone +"<br />" : "") +
      (mapPoint.fax ? "Fax "+ mapPoint.fax +"<br />" : "");
  }
  
  html += (mapPoint.website ? '<a href="'+ mapPoint.website +'"></a>'+ mapPoint.website +'<br />' : "");

  return html;
}

 
$(window).unload( function () {
  GUnload();
});
