var Gmap = new Class({

	Implements:Options,
	
	map: null,
	
	icon: new Array(),
	
	current_icon: "normal",
	
	reasons: [],
	
	options:{
		canvas: '',
		key: '',
		errors: ''
	},
	
	initialize: function(options) {
		this.setOptions(options);
		if (GBrowserIsCompatible() && this.options.canvas != '') {
			this.map = new GMap2(document.getElementById(this.options.canvas));
			this.map.setUIToDefault();
		}
		this.reasons[G_GEO_SUCCESS]            = "Success";
		this.reasons[G_GEO_MISSING_ADDRESS]    = "Missing Address: The address was either missing or had no value.";
		this.reasons[G_GEO_UNKNOWN_ADDRESS]    = "Unknown Address:  No corresponding geographic location could be found for the specified address.";
		this.reasons[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons.";
		this.reasons[G_GEO_BAD_KEY]            = "Bad Key: The API key is either invalid or does not match the domain for which it was given";
		this.reasons[G_GEO_TOO_MANY_QUERIES]   = "Too Many Queries: The daily geocoding quota for this site has been exceeded.";
		this.reasons[G_GEO_SERVER_ERROR]       = "Server error: The geocoding request could not be successfully processed.";
		this.reasons[G_GEO_BAD_REQUEST]        = "A directions request could not be successfully parsed.";
		this.reasons[G_GEO_MISSING_QUERY]      = "No query was specified in the input.";
		this.reasons[G_GEO_UNKNOWN_DIRECTIONS] = "The GDirections object could not compute directions between the points.";
	},
	
	
	addtomap: function(point, info, icon) {
		if (point) {
			var marker = new GMarker(point, this.icon[icon]);
			if(info != '' && typeof(info) != "undefined") {
				GEvent.addListener(marker, "click", function() {
					marker.openInfoWindowHtml(info);
				});
			}
			this.map.addOverlay(marker);
		} else {
			if(typeof(console) != "undefined") {
				console.log("address not found: "+address);
			}
		}
	},
	
	setpoint: function(address, info, icon) {
		if(typeof(icon) == "undefined") {
			var icon = "normal";
		}
		if(typeof(address)=='object'&&(address instanceof Array)) {
			var point = new GLatLng(address[0], address[1]);
			this.addtomap(point, info, icon);
		} else {
			geocoder = new GClientGeocoder();
			geocoder.getLatLng(
				address,
				function(point) {
					this.addtomap(point, info, icon);
				}.bind(this)
			);
		}
	},
		
		
	setcenter: function(address, zoomlevel) {
		if(typeof(address)=='object'&&(address instanceof Array)) {
			var point = new GLatLng(address[0], address[1]);
			this.map.setCenter(point, zoomlevel);
		} else {
			geocoder = new GClientGeocoder();
			geocoder.getLatLng(
				address,
				function(point) {
					if (point) {
						this.map.setCenter(point, zoomlevel);
					}
				}.bind(this)
			);
		}
	},

	seticon: function(image, shadow, sizes, name) {
		if(typeof(name) == "undefined") {
			var name = "normal";
		}
		this.icon[name] = new GIcon();
		this.icon[name].image = image;
		this.icon[name].shadow = shadow;
		this.icon[name].iconSize = new GSize(sizes["icon_x"], sizes["icon_y"]);
		this.icon[name].shadowSize = new GSize(sizes["shadow_x"], sizes["shadow_y"]);
		this.icon[name].iconAnchor = new GPoint(sizes["anchor_x"], sizes["anchor_y"]);
		this.icon[name].infoWindowAnchor = new GPoint(sizes["info_x"], sizes["info_y"]);
	},
	
	directions: function(from, to, output) {
		$('errors').set('html', "");
		var gdir = new GDirections(this.map, document.getElementById(output));
		GEvent.addListener(gdir, "error", function() {
			var code = gdir.getStatus().code;
			var reason = "Code "+code;
			if (this.reasons[code]) {
			  reason = this.reasons[code]
			}
			$('errors').set('html', "Failed to obtain directions, "+reason);
		}.bind(this));
		gdir.load("from: "+from+" to: "+to);
	}
});