/* 
 * PlanetSolar - Geo Scripts for the public map
 *
 * Copyright (c) 2010 Alexis Domjan - adomjan@planetsolar.org
 *
 * $Id: geo.js,v 1.8 2010-08-11 14:34:26 disco Exp $ 
 *
 */

var data = new Array();

/*function createMarker(point, html) {
   var marker = new GMarker(point); 
   GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(html); }); 
   return marker; 
} */


// Download callback
function download_callback(json, responseCode) {

   // Get data in JSON format
   data = eval('(' + json + ')');

   // An array for the polyline
   var pts = new Array();

   function myClosure(marker, infowindow){return function(){
      infowindow.open(map,marker);
   }}

   // Parsing points to create the markers
   var point;
   for (var i=0; i<data.markers.length; i++) { 
      // Set the location of the Marker
      point = new google.maps.LatLng(data.markers[i].lat, data.markers[i].lng); 

      // Set Marker's options
      var marker = new google.maps.Marker({
         position: point,
	 map: map,
      });

      // At the last position we put the boat
      if (i == data.markers.length - 1) {
         marker.icon = '/geo/images/planetsolar-boat-icon-64px.png';
      } 

      // Define icon
      var image = new google.maps.MarkerImage('/geo/images/yellow-dot.png',
      // This marker is 20 pixels wide by 32 pixels tall.
      new google.maps.Size(10, 10),
      // The origin for this image is 0,0.
      new google.maps.Point(0,0),
      // The anchor for this image is the base of the flagpole at 0,32.
      new google.maps.Point(5, 5));

      marker.icon = image;

      // Set the InfoWindow text
      // Is there an image at this position ?
      if (data.markers[i].tag) {
         htmltext = "<p class=onmap>Lat/Lng: " + data.markers[i].lat + ", " + data.markers[i].lng + "<br>Date: " + data.markers[i].date + "<img width=\"320\" src=\"/logbook/uploads/" + data.markers[i].tag + "\"></img>";
         marker.icon = '/geo/images/marker-yellow-photo.png';
      } else {
         htmltext = "<p class=onmap>Lat/Lng: " + data.markers[i].lat + ", " + data.markers[i].lng + "<br>Date: " + data.markers[i].date;
      }

      // Display the marker on the map
      marker.setMap(map);

      infowindow = new google.maps.InfoWindow({
          content: htmltext
      });

      google.maps.event.addListener(marker, 'click', myClosure(marker, infowindow));

      // Keep the point to later display of the Polyline 
      pts.unshift(point);
   } 

   map.setCenter(point); 



   // Create the PolyLine
   var boatTrack = new google.maps.Polyline({
      path: pts,
      strokeColor: "#ffb400",
      strokeOpacity: 0.9,
      strokeWeight: 3,
      geodesic: true,
      map: map,
      zIndex: 2
   });

   // Create the PolyLine
   var boatTrack2 = new google.maps.Polyline({
      path: pts,
      strokeColor: "#000000",
      strokeOpacity: 0.75,
      strokeWeight: 5,
      geodesic: true,
      map: map,
      zIndex: 0
   });


}



// Initialize the map
function geo_init() {
   var latlng = new google.maps.LatLng("47.00", "7.00");
   var mapOptions = {
      zoom: 8,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.TERRAIN
   };

   // Create the Map
   map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
  
   // Get the JSON file and returns
   downloadUrl("http://www.planetsolar.org/cgi-geo/geo.pl?a=gen", download_callback);
   
   return true;
}


// Display the form to edit the logbook entry at the specified marker position
function add_logbook_entry(num) {
   // Get today's date
   today = new Date();
   function leading_zero(a) {
      if (a < 10) {
         return "0" + a;
      } else {
         return a;
      }
   }

   var strdate = today.getDate() + "." + (today.getMonth()+1) + "." + (today.getYear()+1900) + " " + leading_zero(today.getHours()) + ":" + leading_zero(today.getMinutes());
   document.getElementById('editor').style.display = 'block';
   document.getElementById('media').style.display = 'none';
   document.getElementById('geo_id').value = data.markers[num].id;
   document.getElementById('geo_date').value = data.markers[num].date;
   document.getElementById('geo_lat').value = data.markers[num].lat;
   document.getElementById('geo_lng').value = data.markers[num].lng;
   document.getElementById('date_edit').value = strdate;
   return true;
}

