/**
 * google mapa 
 * vojtech.sir@perus.cz  
 */ 
function gmap(lat, lng, zoom, map_type, name, c_zoom, c_search, c_overview, c_switch, map_drag, markers_drag, add_markers, sidebar, select_name) {
   
   this.map = null;
   this.name = name;
   this.lat = lat;
   this.lng = lng;
   this.zoom = zoom;
   this.map_type = map_type; 
   this.map_types = {0: G_NORMAL_MAP, 1: G_SATELLITE_MAP, 2: G_PHYSICAL_MAP, 3: G_HYBRID_MAP};
   this.markers = {};
   this.control_zoom = c_zoom;
   this.control_search = c_search;
   this.control_overview = c_overview;
   this.control_switch = c_switch;
   this.map_drag = map_drag;
   this.markers_drag = markers_drag;
   this.markers_count = 0;
   this.add_markers = add_markers;
   this.sidebar = sidebar;
   this.select_name = select_name;
  
   this.initialize = function() {
      if (GBrowserIsCompatible()) {     
         this.map = new GMap2(getEl('g' + this.name), {googleBarOptions: {showOnLoad: true, suppressInitialResultSelection: true}});
         this.map.addMapType(G_PHYSICAL_MAP);
         this.map.setMapType(this.map_types[this.map_type]);
         this.map.setCenter(new GLatLng(this.lat, this.lng), this.zoom);
         this.map.enableContinuousZoom();
         if (!this.map_drag) {
            this.map.disableDragging();
         }
         if (this.markers_drag) {
            var map = this.map;
            GEvent.addListener(this.map, 'zoomend', function() {
               getEl('google_zoom').value = map.getZoom();            
            });
         }
         if (this.control_zoom) this.map.addControl(new GLargeMapControl());
         if (this.control_switch) this.map.addControl(new GMapTypeControl());
         if (this.control_search) this.map.enableGoogleBar();
         if (this.control_overview) {
            var ov = new GOverviewMapControl(new GSize(120, 120));
            //ov.setMapType(this.map_types[this.ov_type]);
            this.map.addControl(ov);
         }
         if (this.add_markers) this.addMarkers(this.add_markers);
      }
   }

   this.addMarker = function() {
      
      if (!this.markers_count) {
         var marker_icon = new GIcon(G_DEFAULT_ICON);
         var center = this.map.getCenter();
         var pos = new GLatLng(center.lat(), center.lng());
         this.createMarker(this.markers_count, pos, marker_icon);
         this.markers_count++;
      }
      else {
         var center = this.map.getCenter();
         this.markers[0].setLatLng(new GLatLng(center.lat(), center.lng()));
      }
      GEvent.trigger(this.markers[0], 'dragend');
      return false;
   }

   this.createMarker = function(num, pos, marker_icon) {
      var marker = new GMarker(pos, {draggable: this.markers_drag, icon: marker_icon});         
      var map = this.map;
      if (this.markers_drag) {
         GEvent.addListener(marker, "dragend", function() {
            var marker_pos = marker.getLatLng();
            getEl('google_lat').value = marker_pos.lat();
            getEl('google_lng').value = marker_pos.lng();
            getEl('google_zoom').value = map.getZoom();
         });
      }
      this.map.addOverlay(marker);         
      this.markers[num] = marker;
   }


   this.addMarkers = function(markers) {
      if (this.sidebar) {
         var sidebar = '&nbsp;&nbsp;<strong>' + this.sidebar + '</strong>&nbsp;<select class="valign-middle" size="1" onchange="' + this.name + '.triggerClick(this.value);"><option value="select">' + this.select_name + '</option>';
      }
      var i = 0;
      for (var key in markers) {
         var m = markers[key];
         var pos = new GLatLng(m[0], m[1]);
         var num = parseInt(key);
         var marker_icon = new GIcon(G_DEFAULT_ICON);
         this.createMarker(num, pos, marker_icon);
         if (m[3].length) {
            this.setMarkerWindow(num, m[3]);
            sidebar += '<option value="' + key + '">' + m[4] + '</option>';
         }
         i++;
      }
      if (i && this.sidebar) {
         sidebar += '</sidebar>';
         getEl('g' + this.name + '_sidebar').innerHTML = sidebar;
      }
   }

   this.setMarkerWindow = function(num, text) {
      var marker = this.markers[num];
      var map = this.map;
      if (text && text.length) {
         GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml(text);
         });
      }
   }

   this.markerRemove = function(num) {
      if (confirm('Skutečně odstranit ukazatel?')) {
         var el = getEl('div_marker_' + num);
         getEl('markers_div').removeChild(el);
         this.map.removeOverlay(this.markers[num]);
         delete this.markers[num];
      }
      return false;
   }

   this.markerToCenter = function(num) {
      var center = this.map.getCenter();
      var pos = new GLatLng(center.lat(), center.lng());
      this.markers[num].setLatLng(pos);
      getEl('marker' + num + '_lat').value = center.lat();
      getEl('marker' + num + '_lng').value = center.lng();
      return false;
   }

   this.getLatLng = function() {
      getEl('google_zoom').value = this.map.getZoom();
      var center = this.map.getCenter();
      getEl('google_lat').value = center.lat();
      getEl('google_lng').value = center.lng();
      return false;
   }

   this.triggerClick = function(num) {
      if (num != 'select') {
         GEvent.trigger(this.markers[num], 'click');
      }
   }

   window[this.name] = this;
   var map = this.name;
   window.attachEvent ? window.attachEvent('onload', function() {window[map].initialize();}) : window.addEventListener('load',  function() {window[map].initialize();}, false);
}
