var GoogleMaps = new Class({

	locale: null,
	map: null,

	Implements: [Options, Events],
		
	options: {
		container: 'googleMaps',
		map: 'map',
		form: 'tx_directions',
		directions: 'directions',
		calculate: 'submit',
		locale: 'nl',
		fromAddress: '',
		toAddress: '',
		coordinateX: 0,
		coordinateY: 0,
		delay: 500
	},

	initialize: function(options) {
		this.setOptions(options);	
		this.map = $(this.options.map);
		
		this.locale = $$('meta[name=boot-locale]').get('content').toString();		
		this.setupMap();
		this.setupDirection();
		this.printFunction();
	},
 
	setupMap: function(  ) {
		
		
		if ( this.map ) {
	
			
			if ( GBrowserIsCompatible() ) {
			
				var map = new GMap2( this.map );
				map.addControl(new GSmallMapControl());
	        	map.addControl(new GMapTypeControl());	
	        	
	        	this.directions = $(this.options.directions);
	        	this.gdir = new GDirections( map, this.directions );
	        		        		
	        	GEvent.addListener(this.gdir, "load", this.onGDirectionsLoad.bind(this));
        		GEvent.addListener(this.gdir, "error", this.handleErrors.bind(this));
	        	
				this.setDirections(this.options.toAddress, this.options.toAddress, this.options.locale);
			}
					
		}
		
	},
		
	setupDirection: function() {
		
		var directionForm = $$( '.' + this.options.form );
		if ( directionForm ) {
			
			var parent = this;					
			var submit = directionForm.getElement('input[type=submit]');
			
			submit.addEvent( 'click', function(e) {
				e = new Event( e ).stop();
				if ( parent.mapsError == true ) {					
					parent.setupMap();
				}
				parent.setDirections( $(parent.options.fromAddress).get('value'), parent.options.toAddress, parent.options.locale );								
			});
			
		}
		/**/
	
	},
	
	setDirections: function(fromAddress, toAddress, locale) {		
		this.directions.innerHTML = '';
		this.gdir.load('from:' + fromAddress + ' to: ' + toAddress, { 'locale': locale });		
	},
	
	onGDirectionsLoad: function() {
		this.repositionContact();
	}, 
		
	handleErrors: function() {
		
		if ( this.locale == 'nl' )	var header = 'Er is een fout opgetreden';
		else						var header = 'An error occured';
		
		var statusMessage = new Array();
		
		this.mapsError = true;
		if ( !this.map.hasClass('googleMapsError') ) {
			this.map.addClass('googleMapsError');		
		}
		
		this.repositionContact();											
		var statusCode = this.gdir.getStatus().code;
		var statusMessage = null;
		
		if (statusCode == G_GEO_UNKNOWN_ADDRESS) {
			
			if ( this.locale == 'nl' ) {
				statusMessage = "<p>Er is geen adres gevonden. Een reden kan zijn dat het adres nog relatief nieuw is en nog niet is opgenomen in de databases. "
							  + "Probeer het opnieuw door alleen uw postode in te voeren.</p>";			
			}
			
			else {
				statusMessage = "<p>No address was found. This can occur because the address is new and not available in the database. "
							  + "Try again by inserting your ZIP code.</p>";				
			}
			
		}
		
		else if (statusCode == G_GEO_SERVER_ERROR) {

			if ( this.locale == 'nl' ) {
				statusMessage = "<p>Een geocoding of route verzoek kon niet worden voltooid.<br />"
							  + "De exacte reden van de fout is onbekend.</p>";
			} 
			
			else {
				statusMessage = "<p>A geocoding or directions request could not be completed.<br />"
							  + "The exact reason for the error is unknown.</p>";
			}
		}
	   
		else if (statusCode == G_GEO_MISSING_QUERY) {
			
			if ( this.locale == 'nl' ) {
				statusMessage = "<p>De HTTP parameter ontbreekt of heeft geen waarde. "
							  + "Voor geocoder verzoeken: er is een leeg adres opgegeven. "
							  + "Voor route verzoeken: er is geen query opgegeven.</p>";
			}
			
			else {
				statusMessage = "<p>The HTTP parameter is missing or does not have a value. "
							  + "For geocoding requests: no address was inserted. "
							  + "For direction requests: no query was given.</p>";
			}
							
		}
     
		else if (statusCode == G_GEO_BAD_KEY) {
			
			if ( this.locale == 'nl') { 
				statusMessage = "<p>De Google Maps API key is ongeldig of matched niet met het domein.</p>";							  
			}
			
			else {
				statusMessage = "<p>The Google Maps API key is incorrect or doesn't match with the domain.</p>";							  
			}
			
		}

		else if (statusCode == G_GEO_BAD_REQUEST) {
			
			if ( this.locale == 'nl') { 
				statusMessage = "<p>Een route verzoek kon niet worden uitgevoerd.</p>";
			}
			
			else {
				statusMessage = "<p>Request for directions could not be executed.</p>";
			}
								 
	    }
	    
	   	else { 
	   		
			if ( this.locale == 'nl') { 			
				statusMessage = "<p>Een onbekend fout is opgetreden.</p>";
			}
			
			else {
				statusMessage = "<p>An unknown error occured.</p>";	
			}
			
	   	}
		
		
		this.map.innerHTML = "<h6>Google Maps: " + header + "</h6>" + statusMessage + "<p><b>Code:</b> " + statusCode + "</p>";
	
	},
	
	
	printFunction: function() {
		var self = this;
		var printButton = $$('input[name=print]');	
		if ( printButton ) {
			printButton.addEvent('click', function(e) {
				e.stop();
				//window.print();
									
			    var printDocument = window.open("","","");

				printDocument.document.open();
				printDocument.document.write('<html><head><title>' + $$('title').get('html') + '</title>');
			    printDocument.document.write('</head><body onLoad="window.print();window.close()"><center>');
				printDocument.document.write('<div style="width:500px; height: 400px; border: 1px solid #000; position: relative;">'+ self.map.get('html') + '</div><br /><br />');
				printDocument.document.write('<div style="font-family: Arial; font-size: 12px;">'+ self.directions.get('html') + '</div>');
				printDocument.document.write('</body></html>');
				printDocument.document.close();
				printDocument.focus(); 				
			});
		}
		
	},
	
	repositionContact: function() {
		App.repositionContact.delay(this.options.delay);	
	}
	
});
