function GoogleMap(interfaceMapInstance)
{
  this.map = new GMap2($('.housesMap').get(0));
  this.pointer = null;
  this.geocoder = new GClientGeocoder();
  this.interfaceMapInstance = interfaceMapInstance;
  this.DEFAULT_REGION_ZOOM_LEVEL = 10;
  this.DEFAULT_STREET_ZOOM_LEVEL = 16;

  var googleMapObject = this;
  GEvent.addListener(this.map, 'click', function(overlay, latlng)
  {
    googleMapObject.insertPointer(latlng, true);
  });
}

GoogleMap.prototype.renderMap = function()
{
   this.map.addControl(new GLargeMapControl());

   var map_center_x = this.interfaceMapInstance.mapParams['map_center_x'];
   var map_center_y = this.interfaceMapInstance.mapParams['map_center_y'];

   if(!map_center_x  || !map_center_y)
   {
     var map = this.map;
     var DEFAULT_REGION_ZOOM_LEVEL = this.DEFAULT_REGION_ZOOM_LEVEL;
     this.geocoder.getLatLng(this.interfaceMapInstance.mapParams['default_region_name'] ,
     function(point) { map.setCenter(point, DEFAULT_REGION_ZOOM_LEVEL);});
   }
   else
   {
     var point = new GLatLng(map_center_y, map_center_x);
     this.map.setCenter(point, this.DEFAULT_STREET_ZOOM_LEVEL);
     this.insertPointer(point, true);
   }
};


GoogleMap.prototype.insertPointer = function(latlng, saveLatLng)
{
  var googleMapObject = this;

  if (!this.pointer)
  {
    var houseIcon = new GIcon();
    houseIcon.image = '/images/map/houseMarkerRed.png';
    houseIcon.iconSize = new GSize(59, 52);
    houseIcon.iconAnchor = new GPoint(15, 50);

    this.pointer =  new GMarker(latlng, { icon:houseIcon, draggable:true });
    this.pointer.enableDragging();
    GEvent.addListener(this.pointer, "dragend", function()
    {
      cords = this.getLatLng();

      googleMapObject.updateStatusBar( cords );

      if( saveLatLng )
      {
        $('#adFormField-LONGITUDE').val(cords.lng());
        $('#adFormField-LATITUDE').val(cords.lat());
      }
    });
    this.map.addOverlay(this.pointer);
  }
  else
    this.pointer.setLatLng(latlng);

  this.updateStatusBar(latlng);

  if( saveLatLng )
  {
    $('#adFormField-LONGITUDE').val(latlng.lng());
    $('#adFormField-LATITUDE').val(latlng.lat());
  }

  return false;
}

GoogleMap.prototype.updateMarker = function( saveLatLng )
{
  var addressLine = $('#adFormField-CITY').val() + ' ' + $('#adFormField-STREET').val() + ' ' + $('#adFormField-NUM').val();

  if(jQuery.trim(addressLine).length == 0)
  {
    if( $("#adFormField-ID_COUNTRY > [selected]").html() == 'Россия' || $("#adFormField-ID_COUNTRY > [selected]").html() == '')
      addressLine = this.interfaceMapInstance.mapParams['default_region_name'];
    else
      addressLine = $("#adFormField-ID_COUNTRY > [selected]").html() + ' ' + $("#adFormField-ID_REGION > [selected]").html();
  }
  else
    addressLine = $("#adFormField-ID_COUNTRY > [selected]").html() + ' ' + addressLine;

  var pointer = this.pointer;
  var map = this.map;
  var googleMapObject = this;

  this.geocoder.getLatLng(addressLine, function(foundPoint)
  {
      if (foundPoint)
      {
        googleMapObject.insertPointer(foundPoint, saveLatLng);
        map.setCenter(foundPoint);
        map.setZoom(16);
      }
   }
   );
}

GoogleMap.prototype.getAddress = function(result)
{
  var address = {
    city: '',
    street: '',
    house: ''
  };

  if (result.Status.code == G_GEO_SUCCESS)
  {
    var placemark = result.Placemark[0];
    var data = placemark.AddressDetails.Country.AdministrativeArea;

    if(data)
    {
      if( data.SubAdministrativeArea )
        data = placemark.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea;

      if(data.Locality)
      {
        address.city = data.Locality.LocalityName;
        address.city = address.city.replace('город', '');		        

        var locality = (data.Locality.DependentLocality) ? data.Locality.DependentLocality : data.Locality;

        if( locality.Thoroughfare && locality.Thoroughfare.ThoroughfareName)
        {
          address.street = locality.Thoroughfare.ThoroughfareName;			  
		  		 		  		  		 
          var fulladdress = placemark.address;
          var index = fulladdress.indexOf(address.street);
          if (index != -1)
          {		    
            address.house = fulladdress.substr(index, address.street.length);
            address.house = address.house.replace(/[^,]*, /, '');			
			if(address.house.indexOf(' ') != -1)
			  address.house = '';
          }
		  
		  index = address.street.indexOf(',');
		  if(index != -1)
		    address.street = address.street.substr(0, index);
        }
      }
    }
  }

  return address;
}

GoogleMap.prototype.updateStatusBar = function(latlng){
  var googleMapObject = this;

  this.geocoder.getLocations(latlng, function(result){
    var address = googleMapObject.getAddress(result);
    var addressString = '';

    if( address.city )
      addressString = address.city;

    if( address.street )
      addressString += ', ' + address.street;

    if( address.house )
      addressString += ' дом ' + address.house;

    if( !addressString.length )
      addressString = 'не найден';

    if( addressString.length > 60 )
      addressString = addressString.substr(0, 60) + '...';

    $('.housesMapStatus .housesMapSelectedAddress .address').html(addressString);
  });
}

GoogleMap.prototype.updateFields = function()
{
  if(!this.pointer)
    return false;

  var googleMapObject = this;

  this.geocoder.getLocations(this.pointer.getLatLng(), function (result)
  {
    
    var address = googleMapObject.getAddress(result);
    
    if( address.city.length )
      $('#adFormField-CITY').val(address.city);

    if( address.street.length )
      $('#adFormField-STREET').val(address.street);

    if( address.house.length )
      $('#adFormField-NUM').val(address.house);
  });
}





