var m = {
	
	min_zoom : 11,
	
	max_zoom : 15,
	
	popup_width : 630,
	
	markers : new Array(),
	
	locations : new Array(),

	init : function( lid ) {

		if ( GBrowserIsCompatible() ) {
	
			//init map
			m.map = new GMap2( $("map") ); //initialise map
			m.map.getPane( G_MAP_FLOAT_SHADOW_PANE ).style.visibility = "hidden"; //hide shadows 
			m.map.setCenter( new GLatLng( 0,0 ), 10 ); //arbitrary init, changed later, 10 == zoom level
			m.map.disableDoubleClickZoom();
			var overviewMapControl =  new GOverviewMapControl();
			m.map.addControl( overviewMapControl ); //overview control in bottom right corner
			
			//set up boundaries
			m.bounds = new GLatLngBounds();
			
			//set up tooltip for markers
			m.tooltip = document.createElement("div");
			$("map").appendChild( m.tooltip );
			m.tooltip.style.visibility = "hidden";
	  
			//initialise markers
			for( ml in m.locations ) {
	
				if( !ml.match( /^[0-9]+$/ ) ) continue;
			
				var point = new GLatLng( m.locations[ml].lat, m.locations[ml].lon );
				m.markers[ml] = m.create_marker( point, ml );
				m.map.addOverlay( m.markers[ml] );			
	
				m.bounds.extend( point );
			
			} //end for
	
			//get the zoom to fit everything in (but within the min and max levels)
			m.zoom_val = m.get_zoom_level( m.map.getBoundsZoomLevel( m.bounds ) );
			
			//set center and zoom based on bounds
			m.map.setCenter( m.bounds.getCenter(), m.zoom_val );
		
			//center on a specific location if supplied
			if( lid > 0 ) m.center_on_location( lid );

			//zoom slider
			m.zoom_slider = new Control.Slider( 'slider-handle', 'slider-track', {
				range: $R( m.min_zoom, m.max_zoom + 0.1),
				//values: [6,7,8,9,10,11,12,13,14,15],
				values: [11,12,13,14,15],
				sliderValue: m.zoom_val,
				onChange: function( val ) {
					if( val >= m.min_zoom && val <= m.max_zoom && val != m.zoom_val ) m.update_zoom( val, false );
				}
			} );


		} //end if
	
	},

	create_marker : function( point, id ) {

		var marker = new GMarker( point );
		marker.tooltip = '<div class="map-tooltip">' + m.locations[id].title + '</div>';
		GEvent.addListener( marker, "click", function() { m.center_on_location( id ); /*m.show_details( id );*/ } );
		GEvent.addListener( marker, "mouseover", function() { m.show_tooltip( marker ); } );
		GEvent.addListener( marker, "mouseout", function() { m.tooltip.style.visibility = "hidden"; } );
	
		return marker;

	},
    
	show_tooltip : function( marker ) {
		
		m.tooltip.innerHTML = marker.tooltip;
		
		var point = m.map.getCurrentMapType().getProjection().fromLatLngToPixel( m.map.getBounds().getSouthWest(), m.map.getZoom() );
		var offset = m.map.getCurrentMapType().getProjection().fromLatLngToPixel( marker.getPoint(), m.map.getZoom() );
		var anchor = marker.getIcon().iconAnchor;
		var width = marker.getIcon().iconSize.width;
		var pos = new GControlPosition( G_ANCHOR_BOTTOM_LEFT, new GSize( offset.x - point.x - anchor.x + width,- offset.y + point.y +anchor.y ) ); 
		pos.apply( m.tooltip );
		m.tooltip.style.visibility = "visible";
	
	},	
	
	get_zoom_level :  function( val ) {

		if( val < m.min_zoom ) return m.min_zoom;
		if( val > m.max_zoom ) return m.max_zoom;
	
		return val;
	
	},
	
	register : function( id, title, lat, lon ) {
	
		m.locations[ id ] = { title: title, lat : lat, lon : lon }
	
	},

	set_map_type : function( type ) {
		
		if( type == 'hybrid' ) m.map.setMapType( G_HYBRID_MAP );
		else if( type == 'satellite' ) m.map.setMapType( G_SATELLITE_MAP ); 
		else m.map.setMapType( G_NORMAL_MAP );
	
	},

	center_on_location : function( id ) {
		
		var point = new GLatLng( m.locations[id].lat, m.locations[id].lon );
		m.map.panTo( point ); //pan
		//m.map.setCenter( point, m.zoom_val ); //jump
		
		m.show_details( id );
		
	},

	show_details : function( id ) {

		popup_tools.open( 'location-' + id, m.popup_width );
		
	},

	update_zoom : function( val, update_slider ) {
	
		if( val < m.min_zoom || val > m.max_zoom || val == m.zoom_val ) return;
	
		m.map.setZoom( val );
		
		//update the slider
		if( update_slider ) m.zoom_slider.setValue( val );
	
		m.zoom_val = val;
	
	}
	
}