// object
function MapPoint(latitude, longitude, text)
{
	this.point = new GLatLng(latitude, longitude);
	this.text = text;
}

// object
function GoogleMap(domObject)
{
	/* properties / constructor */
	this.points = [];
	this.map = null;
	this.gdir = null;
	this.dirFrom = null;
	this.dirTo = null;
	this.address = null;
	this.bounds = null;
	this.sidebar = [];

	if( GBrowserIsCompatible() )
	{
		this.map = new GMap2(domObject);
		this.map.addControl(new GSmallMapControl());
		this.map.addControl(new GMapTypeControl());
		this.map.setCenter(new GLatLng(0, 0), 9);
	}

	/* Methods */
	this.AddPoint = function(latitude, longitude, text, sidebarText)
	{
		this.points.push(new MapPoint(latitude, longitude, text));
		if(sidebarText != null){
			var index = this.points.length-1;
			var letter = String.fromCharCode("A".charCodeAt(0) + index);
			sidebarText = sidebarText.split('[letter]').join(letter);
			sidebarText = sidebarText.split('[id]').join(index);
			this.sidebar.push(sidebarText);
		}
	}
	
	this.Sidebar = function(elmt){
		elmt.innerHTML = this.sidebar.join('');
	}

	this.ClearPoints = function()
	{
		this.points.length = 0;
	}
	
	this.CreateMarker = function(point, text, isLetter, index)
	{
		var baseIcon = new GIcon(G_DEFAULT_ICON);
		baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
		baseIcon.iconSize = new GSize(20, 34);
		baseIcon.shadowSize = new GSize(37, 34);
		baseIcon.iconAnchor = new GPoint(9, 34);
		baseIcon.infoWindowAnchor = new GPoint(9, 2);

		var markerOptions = {};
		if(isLetter){
			var letter = String.fromCharCode("A".charCodeAt(0) + index);
			var letteredIcon = new GIcon(baseIcon);
			letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";
			markerOptions = { icon: letteredIcon };
		}
		var marker = new GMarker(point, markerOptions);
		var _map = this.map;
		GEvent.addListener(marker, "click", function(){_map.openInfoWindowHtml(point, text);});
		this.map.addOverlay(marker);
	}

	this.Render = function(isLetter)
	{
		if( this.map )
		{
			var minLat = 10000;
			var maxLat = -10000;
			var minLng = 10000;
			var maxLng = -10000;

			for( var i = 0; i < this.points.length; i++ )
			{
				var lat = this.points[i].point.lat();
				var lng = this.points[i].point.lng();

				if( lat > maxLat ) maxLat = lat;
				if( lat < minLat ) minLat = lat;
				if( lng > maxLng ) maxLng = lng;
				if( lng < minLng ) minLng = lng;

				this.CreateMarker(this.points[i].point, this.points[i].text, isLetter, i);
			}
			
			this.bounds = new GLatLngBounds(new GLatLng(minLat, minLng), new GLatLng(maxLat, maxLng));
			this.map.checkResize();
			//this.map.setCenter(this.bounds.getCenter(), this.map.getBoundsZoomLevel(this.bounds)-1);
			this.map.setCenter(this.bounds.getCenter(), this.map.getBoundsZoomLevel(this.bounds)-5);
		}
	}
	
	this.GetMap = function(){
		return this.map;
	}
	
	this.SetDirections = function(fromAddress, toAddress, locale) {
		if(fromAddress != ''){
			this.dirFrom = fromAddress;
			this.dirTo = toAddress;
	    	this.gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": "en_US" });
		} else {
			this.dirFrom = null;
			this.dirTo = null;
			//mapRef.removeOverlay();
			//map.AddPoint(<?=$module->properties->latitude?>, <?=$module->properties->longitude?>, html);
		}
		
    }
	
	this.Directions = function(elmt){
		this.gdir = new GDirections(this.map, elmt);
		//GEvent.addListener(gdir, "load", onGDirectionsLoad);
		var gdir = this.gdir;
        GEvent.addListener(this.gdir, "error", function(){
        	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
				alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
			else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
				alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
			
			else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
				alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
			
			//   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
			//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
			
			else if (gdir.getStatus().code == G_GEO_BAD_KEY)
				alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
			
			else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
				alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
			
			else alert("An unknown error occurred.");
        });
    }
    
    this.PrintMap = function(){
		var address = (this.dirTo != null && this.dirFrom != null) ? 'from: '+this.dirFrom+' to: '+this.dirTo : this.address ;
		var page = "http://maps.google.com/?ie=UTF8&q="+escape(address)+"&ll="+this.bounds.getCenter().lat()+","+this.bounds.getCenter().lng()+"&z=14&pw=2";
		var childWindow = window.open(page, "_blank", "status=1, width=800, height=600, resizable=1, scrollbars=1");
		childWindow.opener = self;
	}
	
	this.HandleErrors = function(){
		alert(this.gdir);
	   /*if (this.gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	     alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + this.gdir.getStatus().code);
	   else if (this.gdir.getStatus().code == G_GEO_SERVER_ERROR)
	     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + this.gdir.getStatus().code);
	   
	   else if (this.gdir.getStatus().code == G_GEO_MISSING_QUERY)
	     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);

	//   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
	//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
	     
	   else if (this.gdir.getStatus().code == G_GEO_BAD_KEY)
	     alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + this.gdir.getStatus().code);

	   else if (this.gdir.getStatus().code == G_GEO_BAD_REQUEST)
	     alert("A directions request could not be successfully parsed.\n Error code: " + this.gdir.getStatus().code);
	    
	   else alert("An unknown error occurred.");*/
	   
	   alert("An unknown error occurred.");
	   
	}
}
