var MOMMap = function() {
	this.mapOptions = {
    'map': null,
    'geocoder': null, 
    'mapCanvas': null, 
    'singletonMarker': null
  };
	
	this.formOptions = {
		'gpsLatInputElemId' : 'marker_gps_lat',
    'gpsLngInputElemId' : 'marker_gps_lng',
    'boundsLatSW' : 'marker_bounds_lat_sw',
    'boundsLngSW' : 'marker_bounds_lng_sw',
    'boundsLatNE' : 'marker_bounds_lat_ne',
    'boundsLngNE' : 'marker_bounds_lng_ne',
    'additionalData' : 'additional_data',
    'additionalDataVisible' : false
	}
	
	this.showElementWithSlide = function(elId) {
    if (!this.formOptions['additionalDataVisible']) {
      Effect.SlideDown(elId, { duration: 1.0 });
    }
    this.formOptions['additionalDataVisible'] = true;
  }
	
	this.initializeMap = function(map_canvas, register_marker){
		this.mapOptions['mapCanvas'] = map_canvas;
    this.mapOptions['geocoder'] = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(52.00, 19.08);
    var myOptions = {
      zoom: 2,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    this.mapOptions['map'] = new google.maps.Map(document.getElementById(this.mapOptions['mapCanvas']), myOptions);
    if (register_marker) {
      this.registerSingletonMarkerListener();
      this.registerZoomListener();
    }
  }
	
	this.queueInitMap = function(map_canvas, register_marker){
		var thiz = this;
		document.observe('dom:loaded', function(){
			thiz.initializeMap(map_canvas, register_marker);
		});
	}

  this.queuePlaceMarkerInitBounds = function(lng, lat, boundsLatSW, boundsLngSW, boundsLatNE, boundsLngNE){
    var thiz = this;
    Event.observe(window, 'load', function() {
      thiz.placeSingletonMarker(new google.maps.LatLng(lat, lng), false);
      thiz.mapOptions['map'].fitBounds(new google.maps.LatLngBounds(new google.maps.LatLng(boundsLatSW, boundsLngSW), new google.maps.LatLng(boundsLatNE, boundsLngNE)));
    });
  }

  this.placeSingletonMarker = function(location, showElements){
    var clickedLocation = new google.maps.LatLng(location);
    
    if (this.mapOptions['singletonMarker']) {
      this.mapOptions['singletonMarker'].setMap(null);
    }
    
    this.mapOptions['singletonMarker'] = new google.maps.Marker({
      position: location,
      map: this.mapOptions['map']
    });
    
    this.mapOptions['map'].setCenter(location);
    if (showElements) {
    	this.showElementWithSlide(this.formOptions['additionalData']);
    }
  }

  this.setPositionToInputsAndGeocodeProvince = function(latLng){
    var valLat = latLng.lat();
    var valLng = latLng.lng();
    this.setPositionToInputs(valLat, valLng);
  }
  
  this.setPositionToInputs = function(valLat, valLng){
    var gpsLatEl = $(this.formOptions['gpsLatInputElemId']);
    var gpsLngEl = $(this.formOptions['gpsLngInputElemId']);
    gpsLatEl.value = valLat;
    gpsLngEl.value = valLng;
  }

  this.registerZoomListener = function(){
    var thiz = this;
    google.maps.event.addListener(this.mapOptions['map'], 'bounds_changed', function(event){
      thiz.setBoundsToInputs(thiz.mapOptions['map'].getBounds());
    });
  }

  this.registerSingletonMarkerListener = function(){
    var thiz = this;
    google.maps.event.addListener(this.mapOptions['map'], 'click', function(event){
      thiz.placeSingletonMarker(event.latLng, true);
      thiz.setPositionToInputsAndGeocodeProvince(event.latLng);
    });
  }

  this.setBoundsToInputs = function(bounds) {
    var boundsLatSW = $(this.formOptions['boundsLatSW']);
    var boundsLngSW = $(this.formOptions['boundsLngSW']);
    var boundsLatNE = $(this.formOptions['boundsLatNE']);
    var boundsLngNE = $(this.formOptions['boundsLngNE']);
    boundsLatSW.value = bounds.getSouthWest().lat();
    boundsLngSW.value = bounds.getSouthWest().lng();
    boundsLatNE.value = bounds.getNorthEast().lat();
    boundsLngNE.value = bounds.getNorthEast().lng();
  }

  this.codeAddress = function(address){
    var thiz = this;
    if (thiz.mapOptions['geocoder']) {
      this.mapOptions['geocoder'].geocode({
        'address': address
      }, function(results, status){
        if (status == google.maps.GeocoderStatus.OK) {
          thiz.mapOptions['map'].setCenter(results[0].geometry.location);
          thiz.mapOptions['map'].fitBounds(results[0].geometry.viewport);
        }
      });
    }
  }
  
  this.isMarkerSet = function() {
  	return this.mapOptions['singletonMarker'] != null;
  }
}

var momMap = new MOMMap();

