var GoogleMaps2 = new Class(
{
	options: {
	},
	id: null,
	inited: false,
	canvas: null,
	gdir: null,

	initialize:	function(id,options)
	{	
		this.setOptions(options);
		this.id		= id;	
		this._init();	
	},
	
	_init: function()
	{
		if(GBrowserIsCompatible() && !this.inited) 
		{    
			this.canvas = new GMap2($(this.id+"_canvas"));
			this.canvas.addControl(new GSmallMapControl());
			this.canvas.addControl(new GMapTypeControl());
			this.canvas.setCenter(new GLatLng(34, 0), 16);			

			this.gdir 		= new GDirections(this.canvas, $(this.id+"_directions"));
			GEvent.addListener(this.gdir, "error", function(error)
			{				
				this.handle_errors(this.gdir.getStatus().code);
				
				var resize = function()
				{
					window.fireEvent('resize');
				}.bind(this);	
				resize.delay(200);				
			}.bind(this));
			GEvent.addListener(this.gdir, "load", function()
			{
				var resize = function()
				{
					window.fireEvent('resize');
				}.bind(this);	
				resize.delay(200);
			}.bind(this));
			this.geocoder 	= new GClientGeocoder();
		}
		this.inited		= true;
	},
	
	show_location: function(location,name)
	{
		this.geocoder.getLocations(location,function(response)
		{
			this.canvas.clearOverlays();
				
			if(!response || response.Status.code != 200) 
	    	{
				this.canvas.setCenter(new GLatLng(34, 0), 1);	
	    	}else 
			{
				var place 		= response.Placemark[0];
				var point 		= new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
				this.marker 	= new GMarker(point, {clickable: true, title: place.address});
				this.canvas.addOverlay(this.marker);        
				this.marker.openInfoWindowHtml("<strong>" + name + " </strong><br/>" + location);
				GEvent.addListener(this.marker, "click", this.ShowInfoWindow);				
	      	}			
		}.bind(this));
	},
		
	get_directions: function(from,to)
	{
		this.gdir.clear();
		
		this.geocoder.getLocations(from,function(response)
		{			
			if(response.Status.code==200)
			{			
				var place 		= response.Placemark[0];
				var frompoint 	= new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
				
				this.geocoder.getLocations(to,function(response)
				{
					var place 		= response.Placemark[0];				
					var topoint 	= new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
					
					this.gdir.load("from: "+frompoint+" to: "+topoint, {"locale":this.options.language});			
					$$('#googlemap_'+this.id+' .directions').setStyle('display', 'block');
					$$('#googlemap_'+this.id+' .error').hide();
				}.bind(this));
			}else
			{
				this.handle_errors(response.Status.code);
			}
		}.bind(this));		
	},
	
	handle_errors: function(code)
	{
		var errordiv		 = $$('#googlemap_'+this.id+' .error');
		//errordiv.set('html',code);
		errordiv.show();
	}
});
GoogleMaps2.implement(new Options, new Events);
