/**
 * setMap.js
 * @author R.A. Ray
 * @version 0.1.1
 * @date 1/16/08 
 * 
 * @requires jquery.js
 */

var map;
var storedData = new Array;
var rawData = new Array;
var newLocation = false;

var setMap = function () 
{
	var minLat = 0;
	var maxLat = 0;
	var minLong = 0;
	var maxLong = 0;
	
	$( '#map' ).each( function ()
		{
			var state = $( this ).attr( 'title' );
			
			// Add the map to the page
			if ( GBrowserIsCompatible() ) 
			{
				map = new GMap2( this );
				
				// Default centers and zooming in case there aren't any points
				switch ( state )
				{
					case 'texas' :
						map.setCenter(new GLatLng(31.2034, -99.1406), 5);
						state = 'tx';
						
						break;
						
					case 'louisiana' :
						map.setCenter(new GLatLng(31.2034, -92.1406), 6);
						state = 'la';
						
						break;
						
					case 'georgia' :
						map.setCenter(new GLatLng(32.5, -83.1406), 6);
						state = 'ga';
						
						break;
						
					case 'south-carolina' :
						map.setCenter(new GLatLng(33.5, -80.5), 6);
						state = 'sc';
						
						break;
				}
				
				map.addControl(new GSmallMapControl());
				//map.addControl(new GMapTypeControl());
		  	}
			
			// Get addresses we haven't stored coords for yet
			$.getJSON( '/locations/getGeoLocs?state=' + state + '&region=' + region + '&store=' + store,
				function ( stores )
				{
					rawData = stores;
					
					for ( var i = 0; i < $( rawData ).length; i++ )
					{
						newLocation = true;
						
						createMarker( i, false );
					}
				}
			);
			
			// Get locations with stored coords
			$.getJSON( '/locations/getGeoLocs?state=' + state + '&region=' + region +  '&store=' + store + '&stored=true',
				function ( stores )
				{
					storedData = stores;
					
					for ( var i = 0; i < $( storedData ).length; i++ )
					{
						var point = createMarker( i, true );
						point = point.LatLong.split( ',' );
						
						// Convert to numbers
						point[0] = point[0] * 1;
						point[1] = point[1] * 1;
						
						// To find our bounding rectangle
						if ( minLat == 0 )
						{
							minLat = point[0];
							maxLat = point[0];
							minLong = point[1];
							maxLong = point[1];
						}
						
						if ( minLat > point[0] )
						{
							minLat = point[0];
						}
						
						if ( maxLat < point[0] )
						{
							maxLat = point[0];
						}
						
						if ( minLong > point[1] )
						{
							minLong = point[1];
						}
						
						if ( maxLong < point[1] )
						{
							maxLong = point[1];
						}
					}
					
					setCenterZoom( minLat, maxLat, minLong, maxLong );
				}
			);
		}
	);
};
	
var createMarker	= function ( i, isStored )
{
	var geocoder = new GClientGeocoder();
	
	if ( isStored )
	{	
		geoCallback( storedData[ i ], i, isStored );
		
		return storedData[ i ];
	}
	else
	{
		geocoder.getLatLng( rawData[ i ][ 'address' ], function ( point )
			{			
				if( point )
				{
					geoCallback( point, i, false );
					storePoint( point, rawData[ i ][ 'id' ] );
				}
			}, i
		);
	}
};

var geoCallback = function ( point, i, isStored )
{
	var data = rawData;
	
	if( isStored )
	{
		data = storedData;
		var tmp = data[i]['LatLong'].split(",",2);		
		point = new GLatLng(tmp[0],tmp[1]);
	}
	
	var customIcon = new GIcon();
	customIcon.image = "/images/marker.png";
	customIcon.iconSize = new GSize(35, 28);
	customIcon.iconAnchor = new GPoint(5, 34);
	customIcon.infoWindowAnchor = new GPoint(5, 2)
	
	var markerOptions = {icon:customIcon};
	var marker = new GMarker(point, markerOptions);
	map.addOverlay(marker);
	GEvent.addListener(marker, "click", function() 
		{
			marker.openInfoWindowHtml( data[ i ][ 'info' ] );
		}
	);	
};

var storePoint = function ( point, id )
{
	$.post( '/locations/updateGeoLocs', { id: id, lat: point.x, lng: point.y } );
};

var setCenterZoom = function ( minLat, maxLat, minLong, maxLong )
{
	if ( !newLocation )
	{
		var centerLat = minLat + (maxLat - minLat) / 2;  
		var centerLong = minLong + (maxLong - minLong) / 2;
		
		var miles = 0;
		
		if ( minLat != maxLat )
		{
			miles = ( 3958.75 * Math.acos( Math.sin( minLat / 57.2958 ) * Math.sin( maxLat / 57.2958 ) + Math.cos( minLat / 57.2958 ) * Math.cos( maxLat / 57.2958 ) * Math.cos( maxLong / 57.2958 - minLong / 57.2958 ) ) ); 
		}
		
		 if ( miles < 0.2 )
		   	map.setCenter( new GLatLng( centerLat, centerLong ), 16 );
		else if ( miles < 0.5 )
			map.setCenter( new GLatLng( centerLat, centerLong ), 15 );
		else if ( miles < 1 )
			map.setCenter( new GLatLng( centerLat, centerLong ), 14 );
		else if ( miles < 2 )
			map.setCenter( new GLatLng( centerLat, centerLong ), 13 );
		else if ( miles < 3 )
			map.setCenter( new GLatLng( centerLat, centerLong ), 12 );
		else if ( miles < 7 )
			map.setCenter( new GLatLng( centerLat, centerLong ), 11 );
		else if ( miles < 15 )
			map.setCenter( new GLatLng( centerLat, centerLong ), 10 );
		else if ( miles < 37 )
			map.setCenter( new GLatLng( centerLat, centerLong ), 9 );
		else if ( miles < 86 )
			map.setCenter( new GLatLng( centerLat, centerLong ), 8 );
		else if ( miles < 209 )
			map.setCenter( new GLatLng( centerLat, centerLong ), 7 );
		else if ( miles < 504 )
			map.setCenter( new GLatLng( centerLat, centerLong ), 6 );
		else if ( miles < 1217 )
			map.setCenter( new GLatLng( centerLat, centerLong ), 5 );
		else if ( miles < 2938 )
			map.setCenter( new GLatLng( centerLat, centerLong ), 4 );
		else if ( miles < 5000 )
			map.setCenter( new GLatLng( centerLat, centerLong ), 3 );
		else if ( miles < 10000 )
			map.setCenter( new GLatLng( centerLat, centerLong ), 2 );
		else
			map.setCenter( new GLatLng( centerLat, centerLong ), 1 );
	}
};

$( 'document' ).ready(
	function ()
	{
		setMap();	
	}
);

