/*
This app was written by Marcel Tjandraatmadja (marcel.tjandra@gmail.com).

Originally posted at http://marcelpad.wordpress.com.
*/

/*
var centerLatitude = 57.142747;
var centerLongitude = 24.862479;
*/
var centerLatitude = map_lat;
var centerLongitude = map_long;
var startZoom = map_zoom;
var currZoom = startZoom;
var markersArray = new Array();
var map;




function init()
{
   if (GBrowserIsCompatible()) 
   {
      map = new GMap2(document.getElementById("map"));
      map.addControl(new GSmallMapControl());

      var location = new GLatLng(centerLatitude, centerLongitude);
      map.setCenter(location, startZoom);
      map.setMapType(G_HYBRID_MAP);

	  var marker = new GMarker(location);
	  map.addOverlay(marker);

	  // add to array
	  markersArray.push(marker);
    }
}

function createMarker()
{
   var latitude = trim(document.forms[0].latitude.value);
   var longitude = trim(document.forms[0].longitude.value);
   
   // simple validation
   if(!Number(latitude) || !Number(longitude))
   {
      alert("Latitude and longitude must be numbers");
      return;
   }
   
   if( latitude.length == 0 || longitude.length == 0 )
   {
      alert("Latitude and longitude cannot be empty");
      return;
   }
   
   // create new location object
   var location = new GLatLng(latitude, longitude);
   
   // place new marker
   var marker = new GMarker(location);
   map.addOverlay(marker); 
   
   // add to array
   markersArray.push(marker);
   
   // move center mantaining current zoom level
   map.setCenter(location, map.getZoom());
   
   
   // render markers list
   renderMarkerList();
}

function createMarkerGZ()
{
   var latitude = centerLatitude;
   var longitude = centerLongitude;
   
   // simple validation
   if(!Number(latitude) || !Number(longitude))
   {
      alert("Latitude and longitude must be numbers");
      return;
   }
   
   if( latitude.length == 0 || longitude.length == 0 )
   {
      alert("Latitude and longitude cannot be empty");
      return;
   }
   
   // create new location object
   var location = new GLatLng(latitude, longitude);
   
   // place new marker
   var marker = new GMarker(location);
   map.addOverlay(marker); 
   
   // add to array
   markersArray.push(marker);
   
   // move center mantaining current zoom level
   map.setCenter(location, map.getZoom());
   
   // render markers list
   //renderMarkerList();
}

function renderMarkerList()
{
   var divContents = "";
   var currMarker;
   
   
   for (var i = 0; i < markersArray.length; i++) 
   {
      currMarker = markersArray[i];
      divContents += "Marker " + i + " (" + currMarker.getLatLng().lat() + "," + currMarker.getLatLng().lng() + ") ";
      divContents += "<a href=\"#\" onClick=\"goToMarker("+ i +");\">Go there</a> <a href=\"#\" onClick=\"removeMarker("+ i +");\">Remove</a><br/>";
   }
   
   
   
   document.getElementById('currentMarkersDiv').innerHTML = divContents;
}



function goToMarker(idx)
{
   if( idx >= 0 && idx < markersArray.length )
   {
      var location = markersArray[idx].getLatLng();
      map.setCenter(location, map.getZoom());
   }
}



function removeMarker(idx)
{
   if( idx >= 0 && idx < markersArray.length )
   {
      // remove marker from map and array
      map.removeOverlay(markersArray[idx]);
      markersArray.splice(idx,1);
      
      renderMarkerList();
   }
   
}




function removeAllMarkers()
{
   for (var i = 0; i < markersArray.length; i++) 
   {
      map.removeOverlay(markersArray[i]);
   }
   
   markersArray = new Array();
   renderMarkerList();
}








// function to trim string
function trim(word)
{
   while (word.substring(0,1) == ' ')
      word = word.substring(1, word.length);

   while (word.substring(word.length-1, word.length) == ' ')
      word = word.substring(0,word.length-1);
      
   return word;
}

window.onload = init;
window.onunload = GUnload;


