/* GoogleMaps class
 - infoWindowLbls(array):
   ['PrefixPhone']
   ['PrefixFax']
   ['Website']
   ['Routeplanner']
   ['ToHere']
   ['FromHere']
   ['ViewAllLocations']
   ['More']
*/
function GoogleMaps3(instanceName,mapCanvasId,locations,zoomCorrection,infoWindowLbls)
{
	this.instanceName = instanceName;
	this.map;
	this.bounds;
	this.geocoder;
	this.locations = locations;
	this.currentGeocodedLocation = 0;
	this.mapCanvasId;
	this.infowindow;
	this.zoomCorrection = zoomCorrection;
	this.infoWindowLbls = infoWindowLbls;
	this.initialize = function()
	{
		var myOptions = {
			mapTypeId: google.maps.MapTypeId.ROADMAP
		}
		this.map = new google.maps.Map(document.getElementById(mapCanvasId), myOptions);
		this.bounds = new google.maps.LatLngBounds();
		this.geocoder = new google.maps.Geocoder();
		this.startCheckAndSetLatLngsInArray();
	}
	
	this.initializeWithDelay = function(milliseconds)
	{
		setTimeout(instanceName+".initialize()",milliseconds);
	}
	this.addMarkersToMap = function()
	{
		this.infowindow = new google.maps.InfoWindow();
		for (var locationIndex in locations)
		{
			var currentLocation = locations[locationIndex];
			var currentLatLng = new google.maps.LatLng(currentLocation.lat,currentLocation.lng);
			
				/*alert('test');
				currentLocation.text.replace(/\n/g, "<br />");*/
			if ((currentLocation.name != "LoopClose") && (typeof(currentLocation.imageURI)!="undefined"))
			{
				this.bounds.extend(currentLatLng);
				
				var currentImage = null;
				var currentShadow = null;
				
				if (currentLocation.imageURI.length > 1 && !currentLocation.imageURI.match('shownoimg'))
				{
					currentImage = new google.maps.MarkerImage(
						currentLocation.imageURI,
						new google.maps.Size(currentLocation.imageWidth,currentLocation.imageHeight),
						new google.maps.Point(currentLocation.imageOriginX,currentLocation.imageOriginY),
						new google.maps.Point(currentLocation.imageAnchorX,currentLocation.imageAnchorY)
						
					);
					
					if (currentLocation.shadowURI.length > 1 && !currentLocation.shadowURI.match('shownoimg'))
					{
						currentShadow = new google.maps.MarkerImage(
							currentLocation.shadowURI,
							new google.maps.Size(currentLocation.shadowWidth,currentLocation.shadowHeight),
							new google.maps.Point(currentLocation.shadowOriginX,currentLocation.shadowOriginY),
							new google.maps.Point(currentLocation.shadowAnchorX,currentLocation.shadowAnchorY)
						);
					}
				}
				
				var currentShape = null;
				
				if (currentLocation.shapeType.length > 1)
				{
					currentShape = {
						coord: eval("[" + currentLocation.shapeCoord + "]"),
						type: currentLocation.shapeType
					};
				}
        		
				var currentMarker = new google.maps.Marker({
					position: currentLatLng,
					map: this.map,
					shadow: currentShadow,
					icon: currentImage,
					shape: currentShape,
					title: currentLocation.name
				});
				
				var currentLocationNameHideClass = "";
				var currentLocationStreetHideClass = "";
				var currentLocationZipHideClass = "";
				var currentLocationCityHideClass = "";
				var currentLocationStateHideClass = "";
				var currentLocationCountryHideClass = "";
				var currentLocationPhoneHideClass = "";
				var currentLocationFaxHideClass = "";
				var currentLocationEmailHideClass = "";
				var currentLocationWebsiteHideClass = "";
				var currentLocationRouteplannerHideClass = "";
				var currentLocationDataItemTitleHideClass = "";
				var currentLocationMoreHideClass = "";
				var currentLocationTextHideClass = "";
				
				if (currentLocation.name.length < 1 || currentLocation.name == " ")
				{
					currentLocationNameHideClass = "GM-Hidden";
				}
				if (currentLocation.street.length < 1 || currentLocation.street == " ")
				{
					currentLocationStreetHideClass = "GM-Hidden";
				}
				if (currentLocation.zip.length < 1 || currentLocation.zip == " ")
				{
					currentLocationZipHideClass = "GM-Hidden";
				}
				if (currentLocation.city.length < 1 || currentLocation.city == " ")
				{
					currentLocationCityHideClass = "GM-Hidden";
				}
				if (currentLocation.state.length < 1 || currentLocation.state == " ")
				{
					currentLocationStateHideClass = "GM-Hidden";
				}
				if (currentLocation.country.length < 1 || currentLocation.country == " ")
				{
					currentLocationCountryHideClass = "GM-Hidden";
				}
				if (currentLocation.phone.length < 1 || currentLocation.phone == " ")
				{
					currentLocationPhoneHideClass = "GM-Hidden";
				}
				if (currentLocation.fax.length < 1 || currentLocation.fax == " ")
				{
					currentLocationFaxHideClass = "GM-Hidden";
				}
				if (currentLocation.email.length < 1 || currentLocation.email == " ")
				{
					currentLocationEmailHideClass = "GM-Hidden";
				}
				if (currentLocation.website.length < 1 || currentLocation.website == " ")
				{
					currentLocationWebsiteHideClass = "GM-Hidden";
				}
				if (currentLocation.Routeplanner.length < 1 || currentLocation.Routeplanner == " ")
				{
					currentLocationRouteplannerHideClass = "GM-Hidden";
				}
				if (currentLocation.moreURI.length < 1 || currentLocation.moreURI == " ")
				{
					currentLocationMoreHideClass = "GM-Hidden";
				}
				if (currentLocation.dataItemTitle.length < 1 || currentLocation.dataItemTitle == " ")
				{
					currentLocationDataItemTitleHideClass = "GM-Hidden";
				}
				if(currentLocation.text.length < 1 || currentLocation.text == " ")
				{
					currentLocationTextHideClass = "GM-Hidden";
				}
				
				var infoWindowLbl_PrefixPhone = "T: ";
				var infoWindowLbl_PrefixFax = "F: ";
				var infoWindowLbl_Website = "Website";
				var infoWindowLbl_Routeplanner = "Routeplanner";
				var infoWindowLbl_ToHere = "hier naar toe";
				var infoWindowLbl_FromHere = "hier vandaan";
				var infoWindowLbl_ViewAllLocations = "Bekijk alle locaties";
				var infoWindowLbl_More = "Meer...";
				
				if (this.infoWindowLbls["PrefixPhone"])
				{
					infoWindowLbl_PrefixPhone = this.infoWindowLbls["PrefixPhone"];
				}
				if (this.infoWindowLbls["PrefixFax"])
				{
					infoWindowLbl_PrefixFax = this.infoWindowLbls["PrefixFax"];
				}
				if (this.infoWindowLbls["Website"])
				{
					infoWindowLbl_Website = this.infoWindowLbls["Website"];
				}
				if (this.infoWindowLbls["Routeplanner"])
				{
					infoWindowLbl_Routeplanner = this.infoWindowLbls["Routeplanner"];
				}
				if (this.infoWindowLbls["ToHere"])
				{
					infoWindowLbl_ToHere = this.infoWindowLbls["ToHere"];
				}
				if (this.infoWindowLbls["FromHere"])
				{
					infoWindowLbl_FromHere = this.infoWindowLbls["FromHere"];
				}
				if (this.infoWindowLbls["ViewAllLocations"])
				{
					infoWindowLbl_ViewAllLocations = this.infoWindowLbls["ViewAllLocations"];
				}
				if (this.infoWindowLbls["More"])
				{
					infoWindowLbl_More = this.infoWindowLbls["More"];
				}
				
				var currentContentString = ''+
					'<div class="GM-Balloon">'+
						'<span class="GM-Title '+currentLocationNameHideClass+'">'+currentLocation.name+'</span>'+
						'<div class="GM-DataItems">'+
							'<span class="GM-DataItem GM-DataItemTitle '+currentLocationDataItemTitleHideClass+'">'+currentLocation.dataItemTitle+'</span>'+
							'<span class="GM-DataItem GM-Street '+currentLocationStreetHideClass+'">'+currentLocation.street+'</span>'+
							'<span class="GM-DataItem GM-Zip '+currentLocationZipHideClass+'">'+currentLocation.zip+' </span>'+
							'<span class="GM-DataItem GM-City '+currentLocationCityHideClass+'">'+currentLocation.city+'</span>'+
							'<span class="GM-DataItem GM-State '+currentLocationStateHideClass+'">'+currentLocation.state+'</span>'+
							'<span class="GM-DataItem GM-Country '+currentLocationCountryHideClass+'">'+currentLocation.country+'</span>'+
							'<span class="GM-DataItem GM-Website '+currentLocationRouteplannerHideClass+'"><a target="_blank" href="'+currentLocation.Routeplanner+'">'+infoWindowLbl_Routeplanner+'</a></span>'+
							'<span class="GM-DataItem GM-Phone '+currentLocationPhoneHideClass+'"><strong>'+infoWindowLbl_PrefixPhone+'</strong>'+currentLocation.phone+'</span>'+
							'<span class="GM-DataItem GM-Fax '+currentLocationFaxHideClass+'"><strong>'+infoWindowLbl_PrefixFax+'</strong>'+currentLocation.fax+'</span>'+
							'<span class="GM-DataItem GM-Email '+currentLocationEmailHideClass+'"><a href="mailto:'+currentLocation.email+'">'+currentLocation.email+'</span>'+
							'<span class="GM-DataItem GM-Website '+currentLocationWebsiteHideClass+'"><a target="_blank" href="'+currentLocation.website+'">'+infoWindowLbl_Website+'</a></span>'+
							'<span class="GM-DataItem GM-description-text '+currentLocationTextHideClass+'">'+currentLocation.text+'</span>'+
						'</div>'+
						
					'</div>';
				
				this.attachInfoWindowToMarker(currentMarker,currentContentString,this.map,this.infowindow);
				
				this.map.setCenter(currentLatLng);
        
			}
		}
		
		this.attachZoomChangedListener(this.map);
		this.map.setCenter(this.bounds.getCenter());
		this.map.fitBounds(this.bounds);
		
		if ((this.map.zoom - this.zoomCorrection) > 0)
		{
			this.map.setZoom(this.map.zoom - this.zoomCorrection);
		}
		else
		{
			this.map.setZoom(0);
		}
	}
	
	// Adjusts zoom level to view all
	this.zoomToAll = function()
	{
		this.map.fitBounds(this.bounds);
	}
	
	this.attachZoomChangedListener = function(map)
	{
		google.maps.event.addListener(map, 'zoom_changed', function()
		{
			
		});
	}
	
	this.attachInfoWindowToMarker = function(marker,contentString,map,infowindow)
	{
		google.maps.event.addListener(marker, 'click', function()
		{
			infowindow.content = contentString;
			infowindow.close();
			infowindow.open(map,marker);
		});
	}
	
	this.startCheckAndSetLatLngsInArray = function()
	{
		this.currentGeocodedLocation = 0;
		this.checkAndSetLatLngsInArray();
	}
	this.nextCheckAndSetLatLngsInArray = function()
	{
		if (this.currentGeocodedLocation < (this.locations.length))
		{
			if (this.currentGeocodedLocation == (this.locations.length) - 1)
			{
				this.addMarkersToMap();
			}
			else
			{
				this.currentGeocodedLocation++;
				this.checkAndSetLatLngsInArray();
			}
		}
	}
	
	this.checkAndSetLatLngsInArray = function()
	{
		var locationsWithLatLng = this.locations;
		
		if (this.geocoder)
		{
			var address = "";
			
			if (this.locations[this.currentGeocodedLocation].zip.length > 1)
			{
				address += this.locations[this.currentGeocodedLocation].zip + " ";
			}
			if (this.locations[this.currentGeocodedLocation].street.length > 1)
			{
				address += this.locations[this.currentGeocodedLocation].street + " ";
			}
			if (this.locations[this.currentGeocodedLocation].city.length > 1)
			{
				address += this.locations[this.currentGeocodedLocation].city + " ";
			}
			if (this.locations[this.currentGeocodedLocation].state.length > 1)
			{
				address += this.locations[this.currentGeocodedLocation].state + " ";
			}
			if (this.locations[this.currentGeocodedLocation].country.length > 1)
			{
				address += this.locations[this.currentGeocodedLocation].country;
			}
			
			if (this.locations[this.currentGeocodedLocation].lat.length < 2 || this.locations[this.currentGeocodedLocation].lng.length < 2)
			{
				if (this.locations[this.currentGeocodedLocation].name != "LoopClose")
				{
					this.geocoder.geocode({'address': address},function(results, status)
					{
						if (status == google.maps.GeocoderStatus.OK)
						{
							if (status != google.maps.GeocoderStatus.ZERO_RESULTS)
							{
								eval(instanceName+".locations["+instanceName+".currentGeocodedLocation].lat = results[0].geometry.location.lat();");
								eval(instanceName+".locations["+instanceName+".currentGeocodedLocation].lng = results[0].geometry.location.lng();");
							}
							else
							{
								alert("No results found");
							}
						}
						else
						{
							alert("Geocode was not successful for the following reason: " + status);
						}
						
						eval(instanceName+".nextCheckAndSetLatLngsInArray()");
					});
				}
				else
				{
					eval(instanceName+".nextCheckAndSetLatLngsInArray()");
				}
			}
			else
			{
				this.nextCheckAndSetLatLngsInArray();
			}
		}
		
		return locationsWithLatLng;
	}
}
function print_r(theObj)
{
	if(theObj.constructor == Array || theObj.constructor == Object)
	{
		document.write("<ul>")
		for(var p in theObj)
		{
			if(theObj[p].constructor == Array|| theObj[p].constructor == Object)
			{
				document.write("<li>["+p+"] => "+typeof(theObj)+"</li>");
				document.write("<ul>")
				print_r(theObj[p]);
				document.write("</ul>")
			} else 
			{
				document.write("<li>["+p+"] => "+theObj[p]+"</li>");
			}
		}
		document.write("</ul>")
	}
}

