var Gmaps = Class.create();
Gmaps.prototype = {
	
	initialize: function( containerName ){
		
			// some default settings
			this.width = 500;
			this.height = 400;
			this.zoomLevel = 15;
			this.showTypeSelect = true;
			this.showSmallMap = true;
			this.smallControls = false;
			this.largeControls = true;
			this.container = $(containerName);
			this.latitude = -33.83209212904;
			this.longitude = 151.184994183803;
			
			// other variables
			this.returnPoint = "";
			this.markerImg = "";
			this.markerWidth = 12;
			this.markerHeight = 20;
			this.markerShadow = "";
			this.markerShadowWidth = 22;
			this.markerShadowHeight = 20;
			
	}, 	
	
	showMap: function(){
		
			// prepares the container and loads a new instance of the map into it
			this.container.style.width = this.width+"px";
			this.container.style.height = this.height+"px";
			this.map = new GMap2(this.container);
			this.centerMap();
		
	},
	
	
	centerMap: function( gGLatLng ){
		
			// centers the map to the specified longitude and latitude
			if ( gGLatLng != null )
					this.map.setCenter(gGLatLng, this.zoomLevel);	
			else
					this.map.setCenter(new GLatLng(this.latitude, this.longitude), this.zoomLevel);	
					
			if ( this.largeControls ) {
						this.showLargeControl();
						this.smallControls = false;
			}
			
			if ( this.smallControls ) {
						this.showSmallControl();
						this.largeControls = false;
			}
					
			if ( this.showTypeSelect )
					this.showMapTypeSelect();
					
			if ( this.showSmallMap )
					this.smallMap();
		
	},
	
	
	showSmallControl: function(){
		
			// shows the map zoom and directional controls
			this.map.addControl(new GSmallMapControl());
		
	},
	
	showLargeControl: function(){
		
			// shows the map zoom and directional controls
			this.map.addControl(new GLargeMapControl());
		
	},
	
	smallMap: function(){
		
			// shows the small corner map
			this.map.addControl(new GOverviewMapControl());
		
	},
	
	
	showMapTypeSelect: function(){
		
			// shows the Map/Satelite/Hybrid button selectors
			this.map.addControl(new GMapTypeControl());
		
	},
	
	
	geocode: function( address, functionName ){
			geocoder = new GClientGeocoder();
			geocoder.getLatLng(address, functionName);
	},	
	
	addMarker: function( lat, lon, html, triggerEvent ){
		
			// adds a marker to the selected point and returns a reference to the marker
			var icon;
			var point = new GLatLng(lat,lon);
			var marker;
			
			// if there is a custom icon specified, create the icon
			if ( this.markerImg.length > 0 ) {
					icon = new GIcon();
					icon.image = this.markerImg;
					if ( this.markerShadow.length > 0 )
							icon.shadow = this.markerShadow;
					icon.iconSize = new GSize(this.markerWidth, this.markerHeight);
					icon.shadowSize = new GSize(this.markerShadowWidth, this.markerShadowHeight);
					icon.iconAnchor = new GPoint(6, 20);
					icon.infoWindowAnchor = new GPoint(5, 1);
					marker = new GMarker(point, icon);
					this.map.addOverlay(marker);
			} else {
					marker = new GMarker(point);
					this.map.addOverlay(marker);
			}	
			
			if ( triggerEvent != null ) {
					if ( triggerEvent == "now" ) {
							marker.openInfoWindowHtml(html);
					} else {
							GEvent.addListener(marker, triggerEvent, function() {
								marker.openInfoWindowHtml(html);
							});	
					}
			} else {
					GEvent.addListener(marker, "mouseover", function() {
						marker.openInfoWindowHtml(html);
					});	
			}
			
			return marker;
	},
	
	addMarkerMgr: function( padding, zoom, track ){
		
			var sBorderPadding;
			var sMaxZoom;
			var bTrackMarkers = false;
			
			// use variables if they have been supplied, or set some defaults if not
			if ( padding != null )
					sBorderPadding =  padding;
			else
					sBorderPadding =  50;
					
			if ( zoom != null )
					sMaxZoom =  zoom;
			else
					sMaxZoom =  15;
					
			if ( track != null )
					bTrackMarkers =  track;
			
			var mgrOptions = { borderPadding: sBorderPadding, maxZoom: sMaxZoom, trackMarkers: bTrackMarkers };
			this.markerMgr = new GMarkerManager(this.map, mgrOptions);
		
			return this.markerMgr;
	},
	
	getCurrentZoomLevel: function(){
			return this.map.getZoom();
	},
	
	getBounds: function(){
			return this.map.getBounds();
	},
	
	getCentre: function(){
			return this.map.getCenter()
	},
	
	setBounds: function( bounds ){
			// centers the map to the specified longitude and latitude using bounds setting
			this.map.setCenter(new GLatLng(bounds), this.zoomLevel);	
			
			if ( this.showControls )
						this.showControl();
					
			if ( this.showTypeSelect )
					this.showMapTypeSelect();
	},
	
	listenZoom: function( functionName ){
			return GEvent.addListener(this.map, "zoomend", functionName);
	},
	
	setType: function( type ){
			// changes the view type of the map
			if ( type == "default" || type == "map" )
					this.map.setMapType(G_MAP_TYPE);
			if ( type == "sat" )
					this.map.setMapType(G_SATELLITE_TYPE);
			if ( type == "hybrid" )
					this.map.setMapType(G_HYBRID_TYPE);
	},
	
	setZoom: function( level ){
			this.map.setZoom( level )
	},
	
	
	
	clear: function(){
			this.map.clearOverlays();
	},
	
	unload: function(){
			GUnload();
	}
	
};	
