var GoogleMap = Class.create({
  
	initJourneyMap: function(markers, site) {
	  this.map = new google.maps.Map2($('gmap'));
	  this.map.addControl(new GSmallMapControl());
    this.map.addControl(new GMapTypeControl());
    this.markers_array = [];
	  var map = this.map;
	  var geocoder = new GClientGeocoder();
	  var ballparks = new GMarkerManager(map);
	  var hotels = new GMarkerManager(map);
	  var camping_sites = new GMarkerManager(map);
	  var parking_spaces = new GMarkerManager(map);
	  var foods = new GMarkerManager(map);
	  var parties = new GMarkerManager(map);
	  
	  var baseball_icon = new GIcon(G_DEFAULT_ICON);
	  baseball_icon.image = '/images/gmap/baseball.png';
	  baseball_icon.shadow = '';
	  baseball_icon.iconAnchor = new GPoint(10, 23);
	  baseball_icon.iconSize = new GSize(20, 23);
	  baseball_icon.infoWindowAnchor = new GPoint(116,23);
	  
	  var ballparks_icon = new GIcon(baseball_icon);
	  ballparks_icon.image = '/images/gmap/ballpark.png';
	  
	  var hotels_icon = new GIcon(baseball_icon);
	  hotels_icon.image = '/images/gmap/hotel.png';
	  
    var camping_sites_icon = new GIcon(baseball_icon);
    camping_sites_icon.image = '/images/gmap/camping.png';
    
	  var parking_spaces_icon = new GIcon(baseball_icon);
	  parking_spaces_icon.image = '/images/gmap/parking.png';
	  
	  var foods_icon = new GIcon(baseball_icon);
	  foods_icon.image = '/images/gmap/food.png';
	  
	  var parties_icon = new GIcon(baseball_icon);
	  parties_icon.image = '/images/gmap/party.png';
	  
	  if(site == 'Ballpark') {
	    map.setCenter(new google.maps.LatLng(49.0259097338659, 12.14040756225586), 15);
	  } else {
	    if(site == 'Nightlife') {
  	    map.setCenter(new google.maps.LatLng(49.017622, 12.09717), 15);
	    } else {
	      map.setCenter(new google.maps.LatLng(49.017607638243824, 12.100753784179688), 12);
	    }
	  };
	  
	  var instance = this;
	  $H(markers).each(function(marker) {
	    switch(marker[0]) {	        
	      case 'hotels':
	        hotels = instance.addMarkerToManager(marker[1], hotels, hotels_icon);
          break;
          
	      case 'camping_sites':
	        camping_sites = instance.addMarkerToManager(marker[1], camping_sites, camping_sites_icon);
	        break;
	        
	      case 'parking_spaces':
	        parking_spaces = instance.addMarkerToManager(marker[1], parking_spaces, parking_spaces_icon);
          break;
          
        case 'foods':
	        foods = instance.addMarkerToManager(marker[1], foods, foods_icon);
          break;
          
        case 'parties':
	        parties = instance.addMarkerToManager(marker[1], parties, parties_icon);
          break;
          
        case 'ballparks':
	        ballparks = instance.addMarkerToManager(marker[1], ballparks, ballparks_icon);
	        break;
	    }
	  });
	  
	  ballparks.refresh();
	  hotels.refresh();
	  camping_sites.refresh();
	  parking_spaces.refresh();
	  foods.refresh();
	  parties.refresh();
	},
	
	addMarkerToManager: function(markers, manager, icon) {
	  var instance = this;
	  markers.each(function(marker) {
	    gmarker = new GMarker(new GLatLng(marker.lat, marker.lng), {icon: icon, zIndexProcess: this.zFunction});
	    gmarker.type = marker.category;
      manager.addMarker(gmarker, 0);
      instance.markers_array.push(gmarker);
      
      new GEvent.addListener(gmarker, 'click', function() {
        instance.showMarkerOverlay(this, marker);
      });
    }.bind(this));
    
    return manager;
	},
	
	zFunction: function() {
	  if (this.icon.image == '/images/gmap/ballpark.png'){
	    return 10;
	  }
	  return 1;
	},
	
	showMarkerOverlay: function(marker, marker_info) {
    html = '<div style="padding: 7px 12px 4px 12px;" class="container ' + marker_info.category + '">';
    if (marker_info.url != '') {
      html+=  '<h3 style="font-size: 16px; margin: 0; padding-bottom: 3px; padding-left: 21px"><a href="' + marker_info.url + '">' + marker_info.title + '</a></h3>';
    } else {
      html+=  '<h3 style="font-size: 16px; margin: 0; padding-bottom: 3px; padding-left: 21px">' + marker_info.title + '</h3>';
    }
    if (marker_info.description != '') {
      html+=  '<p style="margin-bottom: 10px">' + marker_info.description;
      if (marker_info.url != '') {
        html += '<br /><small><a href="' + marker_info.url + '">Weitere Informationen</a></small>';
      };
      html+= '</p>';
    };
    html+= '</div>';	  
    marker.openExtInfoWindow(this.map,"gmap_window",html,{beakOffset:0, paddingX:15, paddingY:30});
   
	},
	
	loadExtInfoWindowJS: function(path) {
	  var script_tag = new Element('script', {
	    src:    path,
	    type:   'text/javascript'
	  });
	  $$("head")[0].insert(script_tag);
	},
	
	showCategoryMarker: function(categories, link_id) {
	  instance = this;
	  
	  $$('.gnav_item').each(function(e){
	    e.removeClassName('active');
	  });
	  
	  active_link = $(link_id).addClassName('active');
	  if (instance.map.getExtInfoWindow()) {
	    instance.map.closeExtInfoWindow();
	    instance.map.getExtInfoWindow();
	  };
	  
	  instance.markers_array.each(function(marker){
	    marker.hide();
	  });
	  
	  categories.each(function(category){
	    instance.markers_array.each(function(marker){
	      if (marker.type == category) {
	        marker.show();
	      };
	    });
	  });
    return false;
	}
});