var GoogleMap = new Class({
	
	Implements: Options,
	
	options: {
		center: new Array(),
		zoom: 13,
		icon: "default",
		controls: {
			zoom: "large",
			type: true
		}
	},
	
	initialize: function(element, latitude, longitude, opts){
	
		this.setOptions(opts);
		
		this.server = "http://dev.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()) {
			
			// Set Center and insert map
			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);
			
			// Make an icon
			if(this.options.icon == "default"){
				this.addMarker(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());
			}
		}
	},
	
	moveTo: function(lat, lng){
		this.map.panTo(new GLatLng(lat, lng));
	},
	
	addMarker: function(lat, lng, icon){
		var marker = new GMarker(new GLatLng(lat, lng));
		if(!icon){
			icon = new GIcon();
		}
		this.map.clearOverlays();
		this.map.addOverlay(marker, icon);
	},
	
	getIcon: 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);
	},
	
	showInfo: function(lat, lng, html){
		this.map.openInfoWindow(new GLatLng(lat, lng), html);
	},
	
	loadDirections: function(el, start, end){
		var directions = new GDirections(this.map, el);
		directions.load("from: "+start+" to: "+end);
	}
	
});
