var centerLatitude = 44.29240108529005;
var centerLongitude = -85.7373046875;
var description = "Michigan!";
var startZoom = 5;
var createMapManager = false;

var map = null;
var mapManager = null;

var iconHash = new Array();

var pointHash = new Array();

function addMarker( latitude, longitude, description ) {
	addMarker2( latitude, longitude, description, null );
}

function addMarker2( latitude, longitude, description, icon_IN )
{
	/*
    var marker = null;
    
    if ( ( icon_IN != null ) && ( icon_IN != "" ) )
	{
		// look up the icon in the iconHash
		var icon = iconHash[ icon_IN ]
		//alert ( "icon_IN = " + icon_IN + ", icon URL = " + icon.image );
	   	marker = new GMarker( new GLatLng( latitude, longitude ), icon );
    }
	else
	{
    	marker = new GMarker(new GLatLng(latitude, longitude));
    }

    GEvent.addListener(marker, 'click',
        function() {
            marker.openInfoWindowHtml(description);
        }
    );
 
    map.addOverlay();
     */
    map.addOverlay( createMarker( latitude, longitude, description, icon_IN ) );
    
    //if ( marker.isHidden() == true )
    //{
    	//alert ("marker is hidden");
    	//marker.show();
    	//GEvent.trigger(marker,'click');
    //}
}	

function createMarker( latitude_IN, longitude_IN, description_IN, icon_IN )
{
    // return reference
    var marker_OUT = null;
    
    if ( ( icon_IN != null ) && ( icon_IN != "" ) )
	{
		// look up the icon in the iconHash
		var icon = iconHash[ icon_IN ]
		//alert ( "icon_IN = " + icon_IN + ", icon URL = " + icon.image );
	   	marker_OUT = new GMarker( new GLatLng( latitude_IN, longitude_IN ), icon );
    }
	else
	{
    	marker_OUT = new GMarker(new GLatLng(latitude_IN, longitude_IN));
    }

    GEvent.addListener(marker_OUT, 'click',
        function() {
            marker_OUT.openInfoWindowHtml(description_IN);
        }
    );

	if ( mapManager != null )
	{
		mapManager.addMarker( marker_OUT, 1 );
	}
    
    return marker_OUT;
} //-- end function createMarker() --//


function focusOnMarker( marker_IN, doClick_IN )
{
	// pan to this marker
	map.panTo( marker_IN.getPoint() );
	
	// do a click event?
	if ( doClick_IN == true )
	{
		// open the marker's infoWindow
		GEvent.trigger( marker_IN, 'click' );
	}
} //-- end function moveMap() --//


function googleMapsInit() {
    if (GBrowserIsCompatible()) {	
        map = new GMap2(document.getElementById("map"));
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);

		if ( createMapManager == true )
		{
			mapManager = new GMarkerManager( map );
			loadMapManager();
			
		    GEvent.addListener( map, 'moveend',
		        function() {
		            mapManager.refresh();
		        }
		    );
		    
		    GEvent.addListener( map, 'zoomend',
		        function( oldLevel_IN, newLevel_IN ) {
		            mapManager.refresh();
		        }
		    );
		}

        if ( ( description != null ) && ( description != "" ) )
        {
        	addMarker(centerLatitude, centerLongitude, description);
       	}
    }
}

function loadMapManager() {}

function reDrawMap( lat_IN, long_IN, desc_IN, zoom_IN )
{
	centerLatitude = lat_IN;
	centerLongitude = long_IN;
	description = desc_IN;
	startZoom = zoom_IN;
	GUnload();
	init();
}

function reCenterMap( lat_IN, long_IN, desc_IN, zoom_IN, icon_IN )
{
	var location = null;

	location = pointHash[ lat_IN + long_IN ];

	if ( location == null )
	{
		//alert ("new location!");
		var location = new GLatLng(lat_IN, long_IN);
		pointHash[ lat_IN + long_IN ] = location;

		//-- !!! Don't call setCenter once you have initialized map!!! --//
		//map.setCenter( location, zoom_IN );
		addMarker2( lat_IN, long_IN, desc_IN, icon_IN  );
		//alert ( "After adding marker" );
	}
	else
	{
		//alert ("existing location!");
	}

	if ( map != null )
	{
		map.panTo( location );
	}
	else
	{
		// map is not available...
	}
}

function rePositionMap( lat_IN, long_IN, zoom_IN )
{
	//alert ("new location!");
	//var location = new GLatLng(lat_IN, long_IN);

	//-- !!! Don't call setCenter once you have initialized map!!! --//
	if ( map != null )
	{
		map.panTo( new GLatLng( lat_IN, long_IN ) );
		if ( ( zoom_IN != null ) && ( zoom_IN != "" ) )
		{
			map.setZoom( zoom_IN );
		}
	}
	else
	{
		// map is not available...
	}
	
	if ( mapManager != null )
	{
		mapManager.refresh();
	}
}

window.onload = googleMapsInit;
window.onunload = GUnload;