/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
//google.load("maps", "2.x");
function GMaps(id)
{
	if( id != null && id != undefined)
	{
		this.id = id;
		this._Init();
	}
	this.MapMarkers = new Array();
	this.MapMarkersHtml = new Array();
}

GMaps.prototype._Init = function()
{	
	this.map = new google.maps.Map2(document.getElementById(this.id));
	this.map.enableScrollWheelZoom();
	this.MarkerRect = new GLatLngBounds();
}

GMaps.prototype.ShowAddressOnMap = function(Address)
{
	if( this.map != null )
	{
		var geocode = new GClientGeocoder();
		var self = this;
		geocode.getLatLng(Address, function(point) { self._SetPointCallback(point, self, '<p>' + Address + '</p>'); });
	}
}

GMaps.prototype.ClearMarkers = function()
{
	this.map.clearOverlays();
	this.MapMarkers = new Array();
	this.MarkerRect = new GLatLngBounds();
	this.MapMarkersHtml = new Array();
}

GMaps.prototype.ShowInfoWindowForMarker = function(id)
{
	if( this.MapMarkers[id] != null )
	{
		this.MapMarkers[id].openInfoWindowHtml(this.MapMarkersHtml[id]);
	}
}

GMaps.prototype.AddMarker = function(point, text, CleanUp)
{
	if(point != null)
	{
		if(CleanUp == true)
		{
			this.map.clearOverlays();
			this.ClearMarkers();
		}
		this._SetPointCallback(point, this, text);
	}
}

GMaps.prototype.AddDraggableMarker = function(point, callback, CleanUp)
{	
	if(point != null)
	{
		if(CleanUp == true)
		{
			this.map.clearOverlays();
			this.ClearMarkers();
		}
		this.MapMarkers[point] = new GMarker(point, { draggable: true});
		this.MarkerRect.extend(point);
		this.map.setCenter(this.MarkerRect.getCenter(), this.map.getBoundsZoomLevel(this.MarkerRect));
		this.map.addOverlay(this.MapMarkers[point]);
		if( callback != null && callback != undefined )
		{
			GEvent.addListener(this.MapMarkers[point], "dragend", callback);
		}
	}
}

GMaps.prototype.AddMarkerWithId = function(id, point, html, CleanUp)
{
	if(point != null)
	{
		if(CleanUp == true)
		{
			//this.map.clearOverlays();
			//this.ClearMarkers();
		}
		this.MapMarkers[id] = new GMarker(point);
		this.MarkerRect.extend(point);
		this.map.setCenter(this.MarkerRect.getCenter(), this.map.getBoundsZoomLevel(this.MarkerRect));
		this.map.addOverlay(this.MapMarkers[id]);
		if( html != null && html != undefined )
		{
			this.MapMarkersHtml[id] = html;
			this.MapMarkers[id].bindInfoWindowHtml(html);
		}
	}
}

GMaps.prototype.DrawPolyline = function(from, to, color, width)
{
	var polyline = new GPolyline([ from, to], color, width);
	this.map.addOverlay(polyline);
}

GMaps.ToGLatLng = function(Lat, Lng)
{
	return new GLatLng(Lat,Lng);
}

GMaps.prototype._SetPointCallback = function(point, self, html)
{
	if( self.map != null && point != null )
	{		
		self.MapMarkers[point] = new GMarker(point);		
		if( html != null && html != undefined )
		{
			this.MapMarkersHtml[point] = html;
			self.MapMarkers[point].bindInfoWindowHtml(html);
		}
		self.MarkerRect.extend(point);		
		self.map.setCenter(self.MarkerRect.getCenter(), self.map.getBoundsZoomLevel(self.MarkerRect));
		self.map.addOverlay(self.MapMarkers[point]);
	}
}

GMaps.prototype.SetClickCallback = function(callback)
{
	GEvent.addListener(this.map, "click", function(overlay, point) { callback(overlay, point); } );
}

GMaps.prototype.ShowController = function()
{
	this.map.addControl(new GLargeMapControl());
}
