﻿function GOOGLE_MAP(args){
    this.args = args;
}

GOOGLE_MAP.prototype.args = null;
GOOGLE_MAP.prototype.map=null;
GOOGLE_MAP.prototype.gmap_controls=[];
GOOGLE_MAP.prototype.geocoder=null;

//bogdan should be global, because of gmap internals
function showAddress(address) {
  framework.google_map.geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        alert(address + " not found");
      } else {
        framework.google_map.map.setCenter(point, 13);
        var marker = new GMarker(point);
        framework.google_map.map.addOverlay(marker);
        marker.openInfoWindowHtml(address);
      }
    }
  );
}

//bogdan should be global, because of gmap internals
function addAddressToMap(response) {
  framework.google_map.map.clearOverlays();
  if (!response || response.Status.code != 200) {
    //alert("\"" + address + "\" not found");
    console.log("GOOGLE_MAP->addAddressToMap","\"" + address + "\" not found");   
  } else {
    var place = response.Placemark[0];
    var point = new GLatLng(place.Point.coordinates[1],
                        place.Point.coordinates[0]);
    framework.google_map.map.setCenter(point, 13);
    
    var marker = new GMarker(point);
    framework.google_map.map.addOverlay(marker);
    var address = place.AddressDetails.Country.AdministrativeArea.Locality.Thoroughfare.ThoroughfareName + ' '
    var  addr = place.AddressDetails.Country.AdministrativeArea.Locality.Thoroughfare.ThoroughfareName 
                + ', ' + place.AddressDetails.Country.AdministrativeArea.Locality.LocalityName
                + ', ' + place.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName
                + '%40' + place.Point.coordinates[1] + '%2C'+ place.Point.coordinates[0];
                
    marker.openInfoWindowHtml(
		    '<div class="gmap-address">'
		    +'<div class="gs-street gs-addressLine">' + place.AddressDetails.Country.AdministrativeArea.Locality.Thoroughfare.ThoroughfareName + ',' + '</div>'
		    +'<div class="gs-city gs-addressLine">' + place.AddressDetails.Country.AdministrativeArea.Locality.LocalityName + ', ' + place.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName + ', ' + place.AddressDetails.Country.AdministrativeArea.Locality.PostalCode.PostalCodeNumber + '</div>'
		    +'<br>' //+'<div class="gs-country">' + place.AddressDetails.Country.CountryNameCode + '</div>'
		    +'</div>'
		    +'<div class="gmap-directions">'
		    +'<span class="gs-label">Get directions: </span>'
		    //+'<div class="gs-secondary-link">'
		    +'<a href="http://www.google.com/maps?source=uds&amp;daddr='
		    + addr
		    + '&amp;iwstate1=dir%3Ato" class="gs-secondary-link" target="_blank">To here</a>'
		    //+'</div>'
		    +'<span class="gs-spacer"> - </span>'
		    //+'<div class="gs-secondary-link">'
		    +'<a href="http://www.google.com/maps?source=uds&amp;saddr=' 
            + addr
		    + '&amp;iwstate1=dir%3Afrom" class="gs-secondary-link" target="_blank">From here</a>'
		    //+'</div>'
		    +'</div>'
    );
  }
}

GOOGLE_MAP.prototype.add_gmap_controls = function(controls){
    for(var i = 0; i < controls.length; i++){
        this.map.addControl(controls[i]);
    }
}

GOOGLE_MAP.prototype.remove_gmap_controls = function(controls){
    for(var i = 0; i < controls.length; i++){
        this.map.removeControl(controls[i]);
    }
}

GOOGLE_MAP.prototype.load = function(id, use_venue_location) {
    //default values
    if(!id) id = this.args.id;
    if(!use_venue_location) use_venue_location = false;
    if (GBrowserIsCompatible()) {
        if(this.args.venue_coords != null && !use_venue_location){
            // Create and Center a Map
            this.map = new GMap2(document.getElementById(id));
		    this.map.setMapType(G_SATELLITE_MAP);
    		
    		var split = this.args.venue_coords.split(";");
		    var bounds = new GLatLngBounds(new GLatLng(split[0], split[1]), new GLatLng(split[2], split[3]));
    		
            this.map.setCenter(bounds.getCenter(), this.map.getBoundsZoomLevel(bounds));

        }else if(this.args.venue_location != null){ 
            this.map = new GMap2(document.getElementById(id));
            this.geocoder = new GClientGeocoder();

            this.geocoder.getLocations(this.args.venue_location, addAddressToMap);
            
        }else{
            console.log("ZOOMER->load_google_map","venue_location and venue_coords are not defined"); 
            return;       
        }
        
        //set controls
        this.gmap_controls.push(new GLargeMapControl());
        this.gmap_controls.push(new GMapTypeControl());
        this.add_gmap_controls(this.gmap_controls);
    }
}