var GoogleMap = new Class({
	Implements: Options,
	
	options: {
		center: [],
		zoom: 13,
		controls: {
			zoom: "large",
			type: true
		},
		icon: "fattire"
		
	},
	
	initialize: function(element, latitude, longitude, opts){
	
		this.setOptions(opts);
		
		this.server = "http://fattire.kemso.com";
		
		this.container = $(element);
		this.lat = latitude;
		this.lon = longitude;
		
		if(this.options.center.length == 0){
			this.options.center = [this.lat, this.lon];
		}
		
		this.makeMap();
	},
	
	makeMap: function(){
		if (GBrowserIsCompatible()) {
			var center = new GLatLng(this.options.center[0],this.options.center[1]);
			this.map = new GMap2(document.getElementById(this.container.get('id')));
			this.map.setCenter(center, this.options.zoom);
			
			if(this.options.icon == "fattire"){
				var markerOptions = this.fatTireIcon();
				var marker = new GMarker(new GLatLng(this.lat, this.lon), markerOptions);
				this.map.addOverlay(marker);
				this.addDirections(marker, this.lat, this.lon);
			}else{
				this.addIcon([this.lat, this.lon], this.options.icon.image, this.options.icon);
			}
			
			if(this.options.controls.type == true){
				this.map.addControl(new GMapTypeControl());
			}
			if(this.options.controls.zoom == "large"){
				this.map.addControl(new GLargeMapControl());
			} else
			if(this.options.controls.zoom == "small"){
				this.map.addControl(new GSmallZoomControl());
			}
		}
	},
	
	addIcon: function(loc, name, opts){
		var icn = new GIcon();
		icn.image = this.server+"/images/icons/maps/"+name+"_icn.png";
		icn.shadow = this.server+"/images/icons/maps/"+name+"_shadow.png";
		icn.iconSize = new GSize(opts.size[0], opts.size[1]);
		icn.shadowSize = new GSize(opts.size[0], opts.size[1]);
		icn.iconAnchor = new GPoint(opts.anchor[0], opts.anchor[1]);
		icn.infoWindowAnchor = new GPoint(Math.round(opts.size[0]/2), 1);
		
		// Set up our GMarkerOptions object literal
		var markerOptions = { icon:icn };
		
		var center = new GLatLng(loc[0],loc[1]);
		var marker = new GMarker(center, markerOptions);
		this.map.addOverlay(marker);
		
		this.addDirections(marker, center.lat(), center.lng());
	},
	
	fatTireIcon: function(){
		var fatIcon = new GIcon();
		fatIcon.image = "http://fattire.kemso.com/images/icons/maps/fattire_large.png";
		fatIcon.shadow = "http://fattire.kemso.com/images/icons/maps/fattire_large_shadow.png";
		fatIcon.iconSize = new GSize(52, 60);
		fatIcon.shadowSize = new GSize(76, 66);
		fatIcon.iconAnchor = new GPoint(26, 60);
		fatIcon.infoWindowAnchor = new GPoint(5, 1);
		
		// Set up our GMarkerOptions object literal
		var markerOptions = { icon:fatIcon };
		
		return markerOptions;
	},
	
	
	addDirections: function(marker, l, lng){
		var latlng = new GLatLng(l, lng);
		GEvent.addListener(marker,"click", function() {
			var loc = window.location.pathname.split('/');
			this.map.openInfoWindowHtml(latlng, '<a href="/'+loc[1]+'/directions/to/'+latlng.lat()+', '+latlng.lng()+'">Get Directions</a>');
		}.bind(this));
	}
	
});
