/*

String.prototype.format = function(){
    var pattern = /\{\d+\}/g;
    var args = arguments;
    return this.replace(pattern, function(capture){ return args[capture.match(/\d+/)]; });
}

*/

    var SMALLCONTROL = 1;
    var BIGCONTROL = 2;
    var MAPTYPECONTROL = 3;

/*
    The following map types are commonly used in the Google Maps API:

    G_NORMAL_MAP displays the default road map view.
    G_SATELLITE_MAP displays Google Earth satellite images. *
    G_HYBRID_MAP displays a mixture of normal and satellite views.*
    G_DEFAULT_MAP_TYPES contains an array of the above three types, useful for iterative processing.
    G_PHYSICAL_MAP displays a physical map based on terrain information.
*/
    var GoogleMap = function(tagID, lat, lng){
      this.mapCanvas = tagID || 'idmap';
      this.lat = lat || 48.642632;
      this.lng = lng || 18.279501;

      var gmap;
      var geocoder;
      var center;
      var zoom = 13;

      var markerOption;

      var locations = new Array();
      var count = 0;

//      var bounds = new GLatLngBounds();

/*     alert('init');  */

      gmap = new GMap2(document.getElementById(this.mapCanvas));

      this.getPoint = function(lat, lng){
        return new GLatLng(lat, lng);
      }

      this.show = function(){
        geocoder = new GClientGeocoder();
        geocoder.getLatLng(center, this.loadMap());
      }

      this.zoom = function(number){
        zoom = number;
      }

      this.createMarker = function(point, message) {
        var marker = new GMarker(point, markerOption);
  //      marker.value = number;
        GEvent.addListener(marker, "click", function() {
          gmap.openInfoWindowHtml(point, message);
        });
        return marker;
      }

       this.addLocation = function(html, lat, lng){
         locations[count] = new Array(html, lat, lng);
//         bounds.extend(GLatLng(lat, lng));
         count = count + 1;
       }


       this.setIcon = function(ico, width, height){
        // Create our "tiny" marker icon
         var myIcon = new GIcon(G_DEFAULT_ICON);
         myIcon.iconSize=new GSize(width, height);
         myIcon.image = ico;

         markerOption = { icon:myIcon };          		// Set up our GMarkerOptions object
       }

       this.setMapControl = function(type){
         if (type == SMALLCONTROL)
           gmap.addControl(new GSmallMapControl());
         else if (type == BIGCONTROL)
           gmap.addControl(new GLargeMapControl());
         else if (type == MAPTYPECONTROL)
           gmap.addControl(new GMapTypeControl());
       }

       this.setMapType = function(type){
         gmap.setMapType(type);
       }

       this.newCenter = function(lat, lng){
         this.lat = lat;
         this.lng = lng;
       }

       this.loadMap = function(){
         center = new GLatLng(this.lat, this.lng);
         gmap.setCenter(center, zoom);
//         gmap.setCenter(bounds.getCenter(), gmap.getBoundsZoomLevel(bounds));
         for (i = 0; i < count; i++){
           var p = new GLatLng(locations[i][1], locations[i][2]);
           gmap.addOverlay(this.createMarker(p, locations[i][0]));
         }
       };
   }

