/* GOOGLE MAPS API */

  var map = null;
  var geocoder = null;
  var zoom = 1;
  var markers = new Array();

  function customMarker()
    {
      this.lat = 0;
      this.lng = 0;

      this.address = '';
      this.title = '';
      this.icon = ''

      this.setIcon = function (icon)
      {
        this.icon = icon;
      }
      this.setAddress = function (address)
      {
        this.address = address;
      }
      this.setTitle = function (title)
      {
        this.title = title;
      }
      this.setLat = function (lat)
      {
        this.lat = lat;
      }
      this.setLng = function (lng)
      {
        this.lng = lng;
      }
      this.getIcon = function ()
      {
        return this.icon;
      }
      this.getAddress = function ()
      {
        return this.address;
      }
      this.getTitle = function ()
      {
        return this.title;
      }
      this.getLat = function ()
      {
        return this.lat;
      }
      this.getLng = function ()
      {
        return this.lng;
      }
    }

  /**
  * Initialize map
  **/
  function initialize(map_div)
  {
    if (GBrowserIsCompatible())
    {
      map = new GMap2(document.getElementById(map_div));
      //map.addControl(new GLargeMapControl());
      //map.addControl(new GMapTypeControl());
      map.setUIToDefault();
      geocoder = new GClientGeocoder();
    }
  }

  function addMarker(id, address, title, icon)
  {
    var obj_marker = new customMarker();
    obj_marker.setTitle(title);
    obj_marker.setAddress(address);
    obj_marker.setIcon(icon);

    markers[id] = obj_marker;
  }

  function addMarkerLatLng(id, x, y, title, icon)
  {
    var obj_marker = new customMarker();
    obj_marker.setLng(x);
    obj_marker.setLat(y);
    obj_marker.setTitle(title);
    obj_marker.setIcon(icon);

    markers[id] = obj_marker;
  }

  function putMarkersOnMap()
  {
    $.each(markers, function(index, item){

      if (item !== undefined)
      {
        geocoder.getLocations(item.getAddress(), function(response){
          if (!response || response.Status.code != 200)
          {
            //alert("Status Code:" + response.Status.code);
          }
          else
          {
            place = response.Placemark[0];

            point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);

            if (item.getIcon() != '')
            {
              var marker = new GMarker(point, {icon: createCustomIcon(item.getIcon())});
            }
            else
            {
              var marker = new GMarker(point);
            }

            if (item.getTitle() != '')
            {
              GEvent.addListener(marker, "mouseover", function() {
                marker.openInfoWindowHtml(item.getTitle());
              });
              GEvent.addListener(marker, "mouseout", function() {
                marker.closeInfoWindow();
              });
            }

            map.addOverlay(marker);
          }
        });
      }
    });
  }

function putMarkersOnMapLatlng()
  {
    $.each(markers, function(index, item){

      if (item !== undefined)
      {

          point = new GLatLng(item.getLat(), item.getLng());

          if (item.getIcon() != '')
          {
            var marker = new GMarker(point, {icon: createCustomIcon(item.getIcon())});
          }
          else
          {
            var marker = new GMarker(point);
          }

          if (item.getTitle() != '')
          {
            GEvent.addListener(marker, "mouseover", function() {
              marker.openInfoWindowHtml(item.getTitle());
            });
            GEvent.addListener(marker, "mouseout", function() {
              marker.closeInfoWindow();
            });
          }

          map.addOverlay(marker);
      }
    });
  }

  function centerOnEurope()
  {
    map.setCenter(new GLatLng(54.813348417419284, 24.186950717121363), 5);
  }

  function centerOnMarker(id, zoom)
  {
    var item = markers[id];
    map.setCenter(new GLatLng(item.getLat(), item.getLng()), zoom);
  }

  function createCustomIcon(image_url, shadow_url)
  {
    if (image_url)
    {
      var icon = new GIcon();
      icon.image = image_url;
      // Top-left corner of image placed relatively to normal icon placement
      icon.iconAnchor = new GPoint(30, 30);
      icon.infoWindowAnchor = new GPoint(40, -10);
      // Icon size of image
      icon.iconSize = new GSize(60, 40);
      if (shadow_url)
      {
        icon.shadow = shadow_url;
        icon.shadowSize = new GSize(60, 40);
      }
      return icon;
    }
    else return null;
  }

  /**
  * Show selected address
  **/
  function showAddress(address, comment_over, marker_icon)
  {
    if (geocoder)
    {
      geocoder.setCache(null);
      geocoder.getLatLng(
        address,
        function(point)
        {
          if (!point)
          {
            //console.log(address + " not found");
          }
          else
          {
            map.setCenter(point, 15);
            if (marker_icon && marker_icon != '')
            {
              var marker = new GMarker(point, {icon: createCustomIcon(marker_icon)});
            }
            else
            {
              var marker = new GMarker(point);
            }

            if (comment_over && comment_over != '')
            {
              GEvent.addListener(marker, "mouseover", function() {
                marker.openInfoWindowHtml(comment_over);
              });
              GEvent.addListener(marker, "mouseout", function() {
                marker.closeInfoWindow();
              });
            }

            map.addOverlay(marker);
            //marker.openInfoWindowHtml("<div class='red'>" + address + "</div> adresas");

            map.setZoom(zoom);
          }
        }
      );
    }
  }

  function navigateTo(address, obj_config)
  {
    if (geocoder)
    {
      geocoder.setCache(null);
      geocoder.getLatLng(
        address,
        function(point)
        {
          if (point)
          {
            map.setCenter(point, 15);
            map.addOverlay(new GMarker(point));
            map.setZoom(zoom);

            $(obj_config.coord_x_div).val(point.x);
            $(obj_config.coord_y_div).val(point.y);
          }
        }
      );
    }
  }

  function gmapZoom(int_level)
  {
    zoom = int_level;
  }

/* /GOOGLE MAPS API */