var map;
var centerLatitude;
var centerLongitude;
var startZoom = 15;

function init_map() {
	
	map = new GMap2( document.getElementById("map") );
	map.addControl( new GSmallMapControl() );
	map.setCenter( new GLatLng( centerLatitude, centerLongitude ), startZoom );
	
	// Create our "tiny" marker icon
	var blueIcon = new GIcon(G_DEFAULT_ICON);
	blueIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png";
	blueIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
	blueIcon.iconSize = new GSize(35, 34);
	blueIcon.shadowSize = new GSize(37, 34);
	blueIcon.iconAnchor = new GPoint(9, 34);
	blueIcon.infoWindowAnchor = new GPoint(9, 2);
	blueIcon.infoShadowAnchor = new GPoint(18, 25);

	
	
	// Create a base icon for all of our markers that specifies the
	// shadow, icon dimensions, etc.
	var baseIcon = new GIcon();
	baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
	baseIcon.iconSize = new GSize(20, 34);
	baseIcon.shadowSize = new GSize(37, 34);
	baseIcon.iconAnchor = new GPoint(9, 34);
	baseIcon.infoWindowAnchor = new GPoint(9, 2);
	baseIcon.infoShadowAnchor = new GPoint(18, 25);

	
	
	// pass in an inital point for the center
	updateMarkers( new GLatLng( centerLatitude, centerLongitude ) );
	
	
	function updateMarkers( point ) {
		// remove the existing points
		map.clearOverlays();
		
		// create the boundary for the data to provide
		// initial filtering
		var bounds = map.getBounds();
		var southWest = bounds.getSouthWest();
		var northEast = bounds.getNorthEast();
		
		
		if ( ( city != "" ) && ( state != "" ) ) {
			var getVars = 'ne=' + northEast.toUrlValue() + '&sw=' + southWest.toUrlValue() + '&known=' + point.toUrlValue() + '&distance='+distance+ '&city=' +city+ '&state=' +state+ '&salestype=' +salestype;
		} else {
			var getVars = 'ne=' + northEast.toUrlValue() + '&sw=' + southWest.toUrlValue() + '&known=' + point.toUrlValue() + '&distance='+distance+ '&salestype=' +salestype;
		}
		
		// log the URL for testing
		//GLog.writeUrl( 'http://www.doublerainbow.com/dr/sites/all/modules/shoplocator/server_inc.php?' + getVars );
		
		// retrieve the points
		var request = GXmlHttp.create();
		request.open( 'GET', 'http://www.doublerainbow.com/dr/sites/all/modules/shoplocator/server_inc.php?' + getVars, true );
		request.onreadystatechange = function() { 
			if ( request.readyState == 4 ) {   
				var jscript = request.responseText;
				var points;
				//GLog.write( jscript );
				var index = 0;   
				  
				eval( jscript ); 
				if (points == null) {
					// there are no results within the given radius
					//alert("There are no results for your request.");
					document.getElementById('map').style.display = 'none';
					document.getElementById('map-results-text').innerHTML = 'There are no results within the distance of '+distance+' miles of your query. <a href=\'http://www.doublerainbow.com/dr/shop_locator\'>Try another search?</a>';
				}   
				// create each point from the list
				pointsArray = []; 
				   
				
				for ( i in points ) {
					var point = new GLatLng( points[i].lat, points[i].lng );
					pointsArray.push(new GLatLng( points[i].lat, points[i].lng )) ;
					var info = points[i].info;
					initializePoint( point, info )
					
					index++;
				}
				
				var newZoom = map.getBoundsZoomLevel(getMBR( pointsArray ));        
				map.setCenter(new GLatLng( centerLatitude, centerLongitude ), map.getBoundsZoomLevel(getMBR( pointsArray ))-1 ) ;
				//GLog.write( "map.getBoundsZoomLevel(getMBR( pointsArray )) = " + map.getBoundsZoomLevel(getMBR( pointsArray )));
			}
		}
		request.send( null );
	}
	
	var deselectCurrent = function() {} // empty function
	
	function initializePoint( point, info ) {
		var marker = new GMarker( point );
		
		// populate sidebar
		var listItem = document.createElement('li');
		var listItemLink = listItem.appendChild(document.createElement("a"));
		listItemLink.href = "#";
		listItemLink.innerHTML = info;
		
		
		 var focusPoint = function() {
			deselectCurrent();
			listItem.className = 'current';
			deselectCurrent = function() {
				listItem.className = '';
			}
			marker.openInfoWindowHtml( info );
			map.panTo( point );
			return false;
		}
		GEvent.addListener( marker, 'click', focusPoint );
		listItemLink.onclick = focusPoint;
		
		document.getElementById("sidebar-list").appendChild(listItem);
		map.addOverlay( marker );
	}
	
	function createMarker( point, info, index ) {
		// Create a lettered icon for this point using our icon class
		if ( index > 0 ) {
			var letter = String.fromCharCode("A".charCodeAt(0) + index-1);
			var letteredIcon = new GIcon(baseIcon);
			letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";
			markerOptions = { icon:letteredIcon };
		} else {
			markerOptions = { icon:blueIcon };
		}
		
		var marker = new GMarker(point, markerOptions);
		GEvent.addListener( marker, 'click',
			function() {
				marker.openInfoWindowHtml( info );
			}
		);
		return marker;
	}
	
	function getMBR (points) {                
		 var minx = 0;
		 var miny = 0;
		 var maxx = 0;
		 var maxy = 0;
		 
		
		 minx = points[0].lat() ;
		 miny = points[0].lng() ;
		 maxx = points[0].lat() ;
		 maxy = points[0].lng() ;
		
		 for(var i = 0; i < points.length ; i++ ) {
		 	if ( points[i].lat() > maxx )
		  	{
		   		maxx = points[i].lat() ;
				//GLog.writeUrl("i = " + i + "points[i].lat() = " + points[i].lat() + " maxx = " + maxx );
		  	}
		  	if ( points[i].lat() < minx )
		  	{
		   		minx = points[i].lat() ;
				//GLog.writeUrl( "i = " + i + "points[i].lat() = " + points[i].lat() + " minx = " + minx );
		  	}
		  	if ( points[i].lng() > maxy )
		  	{
		   		maxy = points[i].lng() ;
				//GLog.writeUrl( "i = " + i + "points[i].lat() = " + points[i].lat() + " maxy = " + maxy );
		  	}
		  	if ( points[i].lng() < miny )
		  	{
		   		miny = points[i].lng();
				//GLog.writeUrl( "i = " + i + "points[i].lat() = " + points[i].lat() + " miny = " + miny );
		  	}
			//GLog.writeUrl( "i = " + i  );
		 }
		 //GLog.write( "maxx,miny = " + maxx +","+ miny + " minx,maxy = " + minx+","+maxy );
		 return ( new GLatLngBounds(new GLatLng(maxx,miny), new GLatLng(minx,maxy)) ) ;
	}
}

