var RS = RS || {};

RS.Maps = {

	DEFAULT: {
		ZOOM: 15,
		POSITION: new GLatLng(-34.611781,-58.417309) /* Buenos Aires */
	},
	
	MAP_ID: "map_canvas",

	CONTAINER_ID: "map_container",

	mapCanvas: null,

	mapLoaded: {},

	panel: null,

	init: function()
	{
		this.panel = new YAHOO.widget.Panel(this.CONTAINER_ID, {
			width:"620px", 
			fixedcenter: true, 
			constraintoviewport: true,
			modal: true, 
			underlay:"none", 
			close:true, 
			visible:false, 
			draggable:true} );
		this.panel.render(document.body);
		this.mapCanvas = document.getElementById(this.MAP_ID);
	},

	show: function(title, address)
	{
		address = address.replace('\\', '');

		if(this.mapCanvas == null)
		{
			this.mapCanvas = false;
			if (GBrowserIsCompatible())
			{
				try
				{
					this.mapCanvas.innerHTML = "Cargando mapa...";
				}
				catch(e)
				{
				}
			}
			else
			{
				return;
			}
		}
		if(!this.mapCanvas)
		{
			return;
		}
		if(!this.mapLoaded[address])
		{
			this.getAddress(address);
		}
		this.panel.setHeader(title);
		this.panel.show();
	},

	loadMap: function(canvas, position, zoom)
	{
		canvas = canvas || this.mapCanvas;
		position = position || RS.Maps.DEFAULT.POSITION; 
		zoom = zoom || RS.Maps.DEFAULT.ZOOM;
		var map = new GMap2(canvas);
		map.setCenter(position, zoom);
		map.setUIToDefault();
		return map;
	},

	map: function(address, canvas, zoom, defaultPosition)
	{
		if(typeof(canvas) == "string")
		{
			canvas = document.getElementById(canvas);
		}
		var map = this.loadMap(canvas, defaultPosition, zoom);
		this.getAddress(address, map, zoom);
	},
	
	getAddress: function(address, map, zoom)
	{
		var geocoder = new GClientGeocoder();
		map = map || this.loadMap();
		zoom = zoom || RS.Maps.DEFAULT_ZOOM;
		if (geocoder) {
			geocoder.getLatLng(
				address,
				function(point) {
					RS.Maps.mapLoaded[address] = true;
					if (!point)
					{
						return false;
					}
					else
					{
						map.setCenter(point, zoom);
						var marker = new GMarker(point);
						map.addOverlay(marker);
					}
				}
			);
		}
	}			
};
