 function googleMap()
 {
  this.centerLat = 0.01;
  this.centerLng = 0.01;
  this.zoom = 0;
  this.center = false;
  this.centerPlace = "";
  this.mapCanvas = "";
  this.map;
  this.markers = {length : 0};
  this.places = {};
  this.placeSrc = "/geoItems/getGeoItems.php";
  this.blockRequest = false;
  this.lastRequestTime;
  this.singlePlace = true;
  this.dontZoom = false;
  this.mapNE = {lat: 52.530, lng: 13.420};
  this.mapSW = {lat: 52.520, lng: 13.410};
  
  
  this.iconImage = function (itemType)
  			{
				 spotImage = "images/mapspot_common.png";
				
				  switch  (itemType)
					{
						case "Gallery" : spotImage = 'images/mapspot_gallery.png';	 break;
						case "Institution" : spotImage = 'images/mapspot_institution.png';	 break;
						case "Private Collection" : spotImage = 'images/mapspot_collection.png';	 break;
						case "Further Exhibition" : spotImage = 'images/mapspot_further.png';	 break;
					}
			
				  var spot = new google.maps.MarkerImage ( spotImage,
				  new google.maps.Size(11, 9),
				  // The origin for this image is 0,0.
				  new google.maps.Point(0,0),
				  // The anchor for this image is the base of the flagpole at 0,32.  
				  new google.maps.Point(5, 7) );
				  
				  return spot;
			}

  this.init = function () {

	     if (this.centerLat == 0.01 & this.centerLng == 0.01)
		 	{
			 //geoCoder.codeAddress(this, this.centerPlace);	
			}
			
		 this.center = new google.maps.LatLng(this.centerLat,this.centerLng);
		   
		 var mapEl = document.getElementById(this.mapCanvas);
  
	     var myOptions = {
		      zoom: this.zoom,
		      mapTypeId: google.maps.MapTypeId.ROADMAP,
		      center: this.center,
			   mapTypeControlOptions: {
				  mapTypeIds: [],
				  style: null
				}
		    }
		    
	     this.map = new google.maps.Map(mapEl, myOptions);
		 this.map.myParent = this;
		 google.maps.event.addListener(this.map, 'idle', function() {
				  var currentTime = new Date();
				  if (currentTime.getTime() - this.myParent.lastRequestTime < 1000) return;			
				  
				  if (!this.myParent.singlePlace)
				  	this.myParent.askForPlaces();
				  					
				  this.myParent.lastRequestTime = currentTime.getTime();
				});	
    }

	this.putMarkers = function()
		{
			for (var i in this.places)
				{
					this.placeMarker(this.places[i]);
					if (this.places[i].polyTo)
						{
							var p1 = this.places[i];
							var p2 = this.places[p1.polyTo];
							
						  var polyCoord = [
							    new google.maps.LatLng(p1.lat, p1.lng),
							    new google.maps.LatLng(p2.orgLat, p2.orgLng)
							  ];
							  var lineTo = new google.maps.Polyline({
							    path: polyCoord,
							    strokeColor: "#555555",
							    strokeOpacity: .8,
							    strokeWeight: 1
							  });
							
							  lineTo.setMap(this.map);						
						}
				}
			
			if (this.dontZoom) return;
			
				if (this.markers.length > 1)
					{	
						var SWbound = new google.maps.LatLng(this.mapSW.lat-0.0001, this.mapSW.lng-0.0001);
						var NEbound = new google.maps.LatLng(this.mapNE.lat+0.0001, this.mapNE.lng+0.0001);
						
						this.map.fitBounds( new google.maps.LatLngBounds(SWbound, NEbound) );
					}
					else
					{
						for (var i in this.markers)
							{ 	if (i != "length")
								{	
									this.map.panTo( this.markers[i].getPosition() );
									this.map.setZoom(15);
									break;
								}
							}
					}
				
		}	

      this.placeMarker = function(markItem)
    	{
		  if (this.markers[markItem.id]){return;}
		  
		  var markerPos = new google.maps.LatLng(markItem.lat, markItem.lng);
		  var newInfowindow;
		  var markerDef = {position: markerPos,
							  map: this.map};
		  if (markItem.status)
		  	{ 
				markerDef.icon=this.iconImage(markItem.status);
			}
			
		  if (markerPos.lat() > this.mapNE.lat)  this.mapNE.lat = markerPos.lat();
		  else if (markerPos.lat() < this.mapSW.lat)  this.mapSW.lat = markerPos.lat();
		  
		  if (markerPos.lng() > this.mapNE.lng)  this.mapNE.lng = markerPos.lng();
		  else if (markerPos.lng() < this.mapSW.lng)  this.mapSW.lng = markerPos.lng();
		  
		  var newMarker = new google.maps.Marker(markerDef);
	
			if (markItem.goTo)
				{
					this.map.setCenter(markerPos);
					if (this.zoom < 10) map.setZoom(15);	
				}
	
	
		  if (markItem.title)
		  	{
				newMarker.title = markItem.title
			}
			
			if (markItem.infoText)
			{
				newInfowindow = new google.maps.InfoWindow({
					content: markItem.infoText
				});
							
				google.maps.event.addListener(newMarker, 'click', function() {
				  //newInfowindow.open(this.map, newMarker);
				  if (this.myParent.myOpenedInfoWindow) this.myParent.myOpenedInfoWindow.close();
				  this.myInfoWindow.open(this.map,this);
		  	      this.myParent.myOpenedInfoWindow = this.myInfoWindow;
				});	
				
			}
			newMarker.myInfoWindow = newInfowindow;
			newMarker.myParent = this;
			newMarker.myAddress = markItem.myAddress;
			this.markers[markItem.id] = newMarker;
			this.markers.length++;
		}	
	
    this.askForPlaces = function()
    	{			
			var boundsNE = this.map.getBounds().getNorthEast();
			var boundsSW = this.map.getBounds().getSouthWest();
			var latlngQuery = {NELat:boundsNE.lat(),NELng:boundsNE.lng(),SWLat:boundsSW.lat(),SWLng:boundsSW.lng()};
			
			$.ajax({url:this.placeSrc,data:latlngQuery, success:places, context:this, dataType : "json"});
			
			function places(datos, result) {
					  for (var i in datos.places)
						{
							if (!this.places[datos.places[i].id] & datos.places[i].lat != 0 & datos.places[i].lng != 0)							
							 this.places[datos.places[i].id] = datos.places[i];
						}
						
					  this.putMarkers();
					  
					  this.blockRequest = false;
					}		
    	}
		
	this.collectPlaces = function()
		{
			var venues = $(".venuesItem");
			this.places = {};
			for (var i=0;i<venues.length;i++)
			{
				
				this.getPlace(venues[i], false);
				
			}
			
			this.putMarkers();			
		}
		
	this.removePlace = function(it, putMarkers)
		{
				var venueName = $(it).find("#venueName");
				var id = $(venueName).attr('venueId');	
				if (this.places[id])
					{this.places[id] = null; 
					 delete this.places[id];
					}
				if (this.markers[id]) 
					{
						this.markers[id].setMap(null);
						this.markers[id] = null;
						delete this.markers[id];
					}
		}

	this.getPlace = function(it, putMarkers)
		{
				var place = {};
				var exhibitions = "<br />";
				var exhibitionsEl = $(it).find(".mapExhibitions");
				var venueName = $(it).find("#venueName");
				
				for (var j = 0; j < exhibitionsEl.length; j++)
					{
						exhibitions += "<br />" + $(exhibitionsEl[j]).html();					
					}
					
				place.id = $(venueName).attr('venueId');	
				if (this.places[place.id])
				{					
					this.places[place.id].infoText += "<div style='display:table'>"+exhibitions+"</div>"
				}
				else
				{
				place.lng = $(venueName).attr('geoLng');
				place.lat = $(venueName).attr('geoLat');
				place.myAddress = $(venueName).attr('venueAddress');				
				place.orgLng = place.lng;
				place.orgLat = place.lat;
				place.status = $(venueName).attr('venueStatus');
				place.title = $(venueName).find("a").html();
				if (!place.title) place.title = $(venueName).html(); 
				place.polyTo = false;
				for (var k in this.places)
					{
						if (Math.round(this.places[k].orgLng*1300) == Math.round(place.lng*1300))
							if (Math.round(this.places[k].orgLat*2500) == Math.round(place.lat*2500))
							{var nC = 0; 
							 if (!this.places[k].nC)
							 	{				
							 	 this.places[k].lng = Number(this.places[k].lng) + (4*Math.cos(nC))/10000;
								 this.places[k].lat = Number(this.places[k].lat) + (3*Math.sin(nC))/10000;
							 	 this.places[k].nC=1;
							 	 this.places[k].polyTo = k;
							 	 nC = 1;
							 	 
							 	}
							 else
							 	nC = this.places[k].nC;
							 	
							 place.lng = Number(place.lng) + (5*Math.cos(nC))/10000;
							 place.lat = Number(place.lat) + (3*Math.sin(nC))/10000;
							 place.polyTo = k; 
							 this.places[k].nC += 1;
							 break;
							}	
					}
				
				
				place.infoText = "<strong>" + $(venueName).html() + "</strong>" + "<div style='display:table'>"+exhibitions+"</div>";
					if (place.id > 0 && place.lng != 0) this.places[place.id] = place; 
				}					
			
				if (putMarkers) this.putMarkers();
			
		}









 }
 
 var myGeoCoder = function(caller, place)
 {
	 this.caller;
	 this.centerPlace;
	 
		this.codeAddress = function (caller, place) {
			var geocoder = new google.maps.Geocoder();
			if (geocoder) {
						  this.caller = caller;
						  this.centerPlace = place;
						  geocoder.geocode( { 'address':  this.centerPlace},  storeGeocode);
						} 
		}

	    function storeGeocode(results, status) {
							if (status == google.maps.GeocoderStatus.OK) {
							  var lat = results[0].geometry.location.lat();
							  var lng = results[0].geometry.location.lng();
							  geoCoder.caller.map.setCenter(results[0].geometry.location);
							  geoCoder.caller.map.setZoom(15);
							  $(geoCoder.caller.lat).val(lat);
							  $(geoCoder.caller.lng).val(lng);
							  var newMarker = new google.maps.Marker({
									  position: results[0].geometry.location,
									  map:  geoCoder.caller.map
									  //,icon: this.iconImage
								  });
							 // $.ajax({url:this.placeSrc,data:{cPlace: geoCoder.centerPlace, lat:lat, lng:lng}});
							} else {
							 // alert("Geocode was not successful for the following reason: " + status);
							}

						  } 
	 
 }

geoCoder = new myGeoCoder();

function toggleImg(el, keepOther)
	{
		if (!keepOther)
		$(".tmbImgX").each(function(){this.src = $(this).attr("tmbSrc"); $(this).attr("class","tmbImg")});
		
		if ($(el).attr("it") == 0)
			{el.src = $(el).attr("orgSrc");
			$(el).attr("it",1);
			$(el).attr("class","tmbImgX");
			}
		 else
		    {el.src = $(el).attr("tmbSrc");
			$(el).attr("it",0);
			}
		 
	}
