function searchKeyPress(e,button){
    // look for window.event in case event isn't passed in
    if (window.event) { e = window.event; }
    if (e.keyCode == 13){
        document.getElementById(button).click();
    }
}

    var map = null;
    var geocoder = null;
    var ifclicked = false;
    var setLon = 57.148161;
    var setLat = 24.301758;
    var mapZoom = 7;
    // A TextualZoomControl is a GControl that displays textual "Zoom In"
    // and "Zoom Out" buttons (as opposed to the iconic buttons used in
    // Google Maps).
//    function TextualZoomControl() {
//    }
//    TextualZoomControl.prototype = new GControl();
//
//    // Creates a one DIV for each of the buttons and places them in a container
//    // DIV which is returned as our control element. We add the control to
//    // to the map container and return the element for the map class to
//    // position properly.
//    TextualZoomControl.prototype.initialize = function(map) {
//      var container = document.createElement("div");
//
//      var zoomInDiv = document.createElement("div");
//      this.setButtonStyle_(zoomInDiv);
//      container.appendChild(zoomInDiv);
//      zoomInDiv.appendChild(document.createTextNode("Pietuvināt"));
//      GEvent.addDomListener(zoomInDiv, "click", function() {
//        map.zoomIn();
//      });
//
//      var zoomOutDiv = document.createElement("div");
//      this.setButtonStyle_(zoomOutDiv);
//      container.appendChild(zoomOutDiv);
//      zoomOutDiv.appendChild(document.createTextNode("Attālināt"));
//      GEvent.addDomListener(zoomOutDiv, "click", function() {
//        map.zoomOut();
//      });
//
//      map.getContainer().appendChild(container);
//      return container;
//    }

    // By default, the control will appear in the top left corner of the
    // map with 7 pixels of padding.
//    TextualZoomControl.prototype.getDefaultPosition = function() {
//      return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 7));
//    }
//
//    // Sets the proper CSS for the given button element.
//    TextualZoomControl.prototype.setButtonStyle_ = function(button) {
//      button.style.textDecoration = "none";
//      button.style.color = "#7794b1";
//      button.style.backgroundColor = "white";
//      button.style.font = "small Arial";
//      button.style.border = "1px solid black";
//      button.style.padding = "2px";
//      button.style.marginBottom = "3px";
//      button.style.textAlign = "center";
//      button.style.width = "6em";
//      button.style.cursor = "pointer";
//    }

    function initialize(location,t) {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById('map_canvas'));
        geocoder = new GClientGeocoder();
        if(location==''){
            map.setCenter(new GLatLng(57.148161,24.301758), 7);
        }else{
            showAddress(location,t);
        }

        map.addControl(new GLargeMapControl());
        map.addControl(new GScaleControl(200));
        //map.addControl(new GOverviewMapControl());
        _mPreferMetric=true;
        //map.hoverControls();
        map.addControl(new GMapTypeControl());
        //map.addControl(new TextualZoomControl());

      }
    }

    function showAddress(location,t) {
      if(t=='a'){
          if (geocoder) {
            geocoder.getLatLng(
              location,
              function(point){
                if (!point) {
                  //jQuery('#map_canvas').hide();
                  //var setLon = 57.148161;
                  //var setLat = 24.301758;
                  if(ifclicked==true)jAlert('Adrese nav atrasta! Mēģiniet citādāk vai arī atzīmējiet kartē vietu pats!','Paziņojums');
                  map.setCenter(new GLatLng(setLon,setLat), 7);

                  //alert(address + " not found");
                  mapZoom = 7;
                } else {
                  mapZoom = 13;
                  map.setCenter(point, 13);
                  //alert(point);
                  var str = new String(point);
                  str = str.substring(1,str.length-1);
                  var tmp = str.split(',');
                  setLon = tmp[0];
                  setLat = tmp[1];
                  var marker = new GMarker(point);
                  map.addOverlay(marker);
                  marker.openInfoWindowHtml(google_map_address);
                }
              }
            );
          }
      }else{
            //alert(location+'  '+t);
            mapZoom = 13;
            var tmp = location.split(',');
            setLon = tmp[0];
            setLat = tmp[1];
            map.setCenter(new GLatLng(setLon, setLat), 13);
            var point = new GLatLng(setLon, setLat);
		    var point = new GMarker(point);
		    map.addOverlay(point);
            point.openInfoWindowHtml(google_map_address);
      }
    }

    function findLocation(location,t){
        ifclicked = true;
        showAddress(location,t);

    }

//    function submitKeyPress(e,button){
//        // look for window.event in case event isn't passed in
//        if (window.event) { e = window.event; }
//        if (e.keyCode == 13){
//            document.getElementById(button).click();
//            map.clearOverlays();
//        }
//    }

    function change_input_type(t){
        if(t=='m'){
            if(map){map.clearOverlays();}
            noMore = false;
            jQuery('#marker_image').show();
            jQuery('#manual_input').show();
            jQuery('#automatic_input').hide();
        }else{
            if(map){map.clearOverlays();}
            jQuery('#manual_input').hide();
            jQuery('#automatic_input').show();

        }
    }
/**
 * follow() function
 * @author: Esa 2008
 */

var images = [
  "http://maps.google.com/mapfiles/marker.png",
  "http://maps.google.com/mapfiles/dd-start.png",
  "http://maps.google.com/mapfiles/dd-end.png"
];

  var marker;
  var dog = true;
  var noMore = false;

function follow(imageInd){


  var mouseMove = GEvent.addListener(map, 'mousemove', function(cursorPoint){
    if(!noMore){
      jQuery('#coordinates').val(cursorPoint);

      marker = new GMarker(cursorPoint,{draggable:true, autoPan:false});
      map.addOverlay(marker);
      marker.setImage(images[imageInd]);
      noMore = true;
      jQuery('#marker_image').hide();
	  // This function deletes the marker when dragged outside map
	  GEvent.addListener(marker, 'drag', function(markerPoint){
        jQuery('#coordinates').val(markerPoint);
        var str = new String(markerPoint);
        str = str.substring(1,str.length-1);
        var tmp = str.split(',');
        setLon = tmp[0];
        setLat = tmp[1];
	    if(!map.getBounds().containsLatLng(markerPoint)){
          map.clearOverlays();
          noMore = false;
          jQuery('#marker_image').show();
        }
      });
    }
    if(dog){
      jQuery('#coordinates').val(cursorPoint);
      var str = new String(cursorPoint);
      str = str.substring(1,str.length-1);
      var tmp = str.split(',');
      setLon = tmp[0];
      setLat = tmp[1];
      marker.setLatLng(cursorPoint);
    }
  });
  var mapClick = GEvent.addListener(map, 'click', function(){
    dog = false;
    // 'mousemove' event listener is deleted to save resources
    GEvent.removeListener(mouseMove);
    GEvent.removeListener(mapClick);
  });
}

function checkmap()
{
 if (map) {
    map.checkResize();
    map.setCenter(new GLatLng(setLon, setLat), mapZoom);
 }
}


