/* Disable firebug */
if (!window.console) { window.console = { log: function() {} }; };

$(document).ready(function() {
  if (GBrowserIsCompatible()) {
    // initialize map
    var map = new GMap2(document.getElementById("map"));
    map.addControl(new GLargeMapControl3D());
    map.addControl(new GMapTypeControl());
    map.enableScrollWheelZoom();

    // show center
    var center = new GLatLng(56.9546404, 24.11404610);
    map.setCenter(center, 13);
    
    // create icon
    // var icon = new GIcon(G_DEFAULT_ICON);
    // icon.image = "http://www.europark.lv/map/marker.png";
    // icon.iconSize = new GSize(24, 37);
    // icon.shadowSize = new GSize(44, 35);

    $(MapsData).each(function(i, data) {
      // console.log("Element", i, data);

	  //iconu citu ja ir nodefinēts
	//iconu citu ja ir nodefinēts  
	  if(data.icon) {
		var icon = new GIcon(G_DEFAULT_ICON);
	    icon.image = data.icon;
	    icon.iconSize = new GSize(24, 37);
	    icon.shadowSize = new GSize(44, 35);
	 	}
	  else {
		var icon = new GIcon(G_DEFAULT_ICON);
	    icon.image = "http://www.europark.lv/map/marker.png";
	    icon.iconSize = new GSize(24, 37);
	    icon.shadowSize = new GSize(44, 35);
	  }
	
      if (data.coordinates) {
        // add marker
        var location = new GLatLng(data.coordinates[0], data.coordinates[1]);
        console.log(i, "=>", data.address, ' => "coordinates": [' + data.coordinates[0] + ', ' + data.coordinates[1] + ']');
        var marker = new GMarker(location, {
          draggable: false,
          icon: icon,
          title: data.address
        });
        map.addOverlay(marker);
        // add info window
        GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml(
            '<div>' +  
              data.address + '<br/>' + 
			  data.name + '<br/>' +
              data.comment + '<br/>' +  
              '<br/>' + 
              '<a href="' + data.link + '">Uzzini vairāk</a>' +
            '</div>'
          );
        });
      } else if (data.address) {
        // get address
        // var geocoder = new GClientGeocoder();
        // geocoder.getLocations(data.address, function(response) {
        //   if (response && response.Status.code == 200) {
        //     var place = response.Placemark[0];
        //     var coordinates = place.Point.coordinates;
        //     var location = new GLatLng(coordinates[1], coordinates[0]);
        //     console.log(i, "=>", data.address, ' => "coordinates": [' + coordinates[1] + ', ' + coordinates[0] + ']');
        //   } else {
            console.log(i, "=>", data.address, ' => NONE ');
        //   }
        // });
      } else {
        console.log(i, "=>", data.address, ' => NONE ');
      }
    });
  }
});

