var Mbx_googleDirections = new Class({
	
	Implements : [Options, Events],

	options: {
		'mapContainerSelector': null,
		'directionsContainerSelector': null,
		'formFromSelector' : null,
		'mapSize' : {
			'width' : '300',
			'height' : '300'
		},
		'destination' : null,
		'locale' : 'de',
		onSubmitHook: $empty,
		onErrorHook: $empty
	},
	
	'map' : null,
	'directions' : null,
	
	initialize : function(p_opts)
	{

		if(!p_opts.mapContainerSelector || !p_opts.directionsContainerSelector || !p_opts.formFromSelector || !p_opts.destination || !$(p_opts.mapContainerSelector) || !$(p_opts.directionsContainerSelector))
		{
			alert('please specify correct options for mapContainerSelector, directionsContainerSelector and destination');
			return;
		}
		
		this.setOptions(p_opts);

	    if(GBrowserIsCompatible())
	    {
	    	this.initMap();
	    	this.initDirections();
	        this.addClassEvents();
	    }
	},
	
	initMap : function()
	{
		this.map = new GMap2($(this.options.mapContainerSelector), {
			'size' : new GSize(this.options.mapSize.width, this.options.mapSize.height)
		});
		
		this.showDestination();
	},

	showDestination : function()
	{
		//default: show destination
		geocoder = new GClientGeocoder();
        geocoder.getLatLng(
			this.options.destination,
			function(point)
			{
				if (!point)
				{
			    	alert(this.options.destination + " not found");
			    }
			    else
			    {
			    	this.map.setCenter(point, 15);
			        var marker = new GMarker(point);
			        this.map.addOverlay(marker);
			    }
			}.bind(this)
        );
	},
	
	initDirections : function()
	{
		this.directions = new GDirections(this.map, $(this.options.directionsContainerSelector));
	    GEvent.addListener(this.directions, 'error', this.onDirectionsError.bind(this));
	},

    addClassEvents : function()
    {
    	$(this.options.formFromSelector).getNext().addEvent('click', function(cEv) {
    		new Event(cEv).stop();
    	
    		this.fireEvent('onSubmitHook');
	   		this.setDirections($(this.options.formFromSelector).get('value'));
    	}.bind(this));
    	
    	$(this.options.formFromSelector).addEvent('keydown', function(keydownEv){
    		if(keydownEv.key == 'enter')
    		{
    			new Event(keydownEv).stop();
    			this.fireEvent('onSubmitHook');
    			this.setDirections($(this.options.formFromSelector).get('value'));
    		}
    	}.bind(this));
    },
	
	setDirections : function(p_fromAddress)
	{
    	this.directions.load("from: " + p_fromAddress + " to: " + this.options.destination, { "locale": this.options.locale });
    },
	
	onDirectionsError : function()
	{
		var errorMessage = '';
		switch(this.directions.getStatus().code)
		{
			case G_GEO_UNKNOWN_ADDRESS:
				errorMessage = 'Die von Ihnen angegebene Adresse konnte leider nicht gefunden werden.';
				break;
/*
			case G_GEO_SERVER_ERROR:
				errorMessage = '';
				break;
*/
			case G_GEO_MISSING_QUERY:
				errorMessage = 'Bitte geben Sie einen Suchbegriff ein.';
				break;
/*
			case G_GEO_BAD_KEY:
				errorMessage = '';
				break;
			case G_GEO_BAD_REQUEST:
				errorMessage = '';
				break;
*/
				default:
				errorMessage = '';
				break;
		}
		
		if(errorMessage != '')
		{
			this.fireEvent('onErrorHook', errorMessage);
		}
	}
});

//extending SVGAnimatedString-Object by the "contans" - function
//@see https://mootools.lighthouseapp.com/projects/2706/tickets/526-problem-in-function-when-using-with-svg
if($type(SVGAnimatedString.prototype.contains) != 'function')
{
	SVGAnimatedString.prototype.contains = function(value) {
		return this.baseVal.contains( value);
	};
}