/* 
 * maps.js
 * 
 * 
 */

var map = null;
var maplist = new Object(); 
 
/*
** Global coordinate saves
**
** savepoint = one distinct site point object 
** points    = saved array of coord objects returned by the server
*/
var savepoint  = new Object();
var pointsObj  = new Object();


function savePoint(lat, lng, html, icon) {
	savepoint.lat  = lat;
	savepoint.lng  = lng;
	savepoint.html = html;
	savepoint.icon = icon;
} // end savePoint()



function initMap( idname ) {
	var lat  = 27.999820112304942;
	var lng  = -82.44707107543945;
	var zoom = 15;
	
 
	if( !idname ) {
		idname = "map";
	}
	if (GBrowserIsCompatible()) {
		var map = new GMap2($(idname),{size:new GSize(450,250)});
		map.addControl(new GSmallMapControl());
		//map.addControl(new GMapTypeControl());
		map.enableScrollWheelZoom();
        map.setCenter(new GLatLng(lat, lng), zoom); 
		maplist[idname] = map;
		polyHampTerr(idname);
 
		
	} else {
		alert("Sorry, Map is not compatible with your browser.");
	}
} // end initMap()

function createMarker(map_id, lat, lng, html, icon ) {
	// recommended to keep markers below 50 for best performance
	// below 100 is reasonable.
	// "icon" is a string that matches an iconlist object name
    var latlong = new GLatLng(lat, lng);
	if (icon == undefined) {			
		var marker  = new GMarker(latlong, iconlist['g_general']);	
	} else {
		var marker  = new GMarker(latlong, iconlist[icon]);
	}
	GEvent.addListener(marker, 'click', function(overlay, latlng) {
			marker.openInfoWindowHtml(html);
	});
	maplist[map_id].addOverlay(marker);

    return marker;
} // end createMarker()

function createMapListener(map) {
	// provided the map object, create a listener that will respond
	// to clicks on the map itself.
	GEvent.addListener(map, 'click', function(overlay, latlng) {
		if (latlng) {
			var locHtml = "Lat/Long: " + latlng.lat() + "/" + latlng.lng();
			map.openInfoWindow(latlng, document.createTextNode(locHtml));
		}
	});
} // end createMapListener()

function displayInfoWindow(map_id, lat, lng, html) {
	// no marker.  Just open an info window overlay with the html
	// inserted at the lat/long provided.  May be with an existing marker,
	// may not.
    var latlng = new GLatLng(lat, lng);
	maplist[map_id].openInfoWindowHtml(latlng, html);
} // end displayInfoWindow()

function clearMaps(map_id) {
	maplist[map_id].clearOverlays();
} // end clearMaps()


function centerMap(map_id, lat, lng, zoom, type, save) {
	var map = maplist[map_id];
	var latlong = new GLatLng(lat, lng);
	if (!zoom || isNaN(zoom) || zoom > 16) { zoom = 3;}
	if (!type || (type != G_NORMAL_MAP && type != G_SATELLITE_MAP && type != G_HYBRID_MAP)) {
		type = G_NORMAL_MAP;
	}
	map.setCenter(latlong, zoom, type);
	if( save ) {
		map.savePosition( );
	}
} // end centerMap()


function getBoundary(map_id) {
	// returns the current boundary points from the viewport in
	// a useable string to pass on to another function/query 
	// -- good for querying all points in visible map from database
	var map    = maplist[map_id];
	var bounds = map.getBounds();
	var SW     = bounds.getSouthWest();
	var NE     = bounds.getNorthEast();
	var out    = 'ne=' + NE.toUrlValue() + '&sw=' + SW.toUrlValue(); 
	return out;
} // end getBoundary()



function resizeMap( mapid, xsize, ysize ) {
	var theDiv = document.getElementById( mapid );

	theDiv.style.height = ysize + 'px';
	theDiv.style.width = xsize + 'px';
}





function polyHampTerr(map_id) {
	var polygon = new GPolygon([	                            		  
	    new GLatLng(28.003230344023283, -82.45119094848633),
	    new GLatLng(27.99610662613497, -82.45097637176514),
	    new GLatLng(27.99610662613497, -82.44282245635986),
	    new GLatLng(28.003381907372326, -82.44282245635986),
	    new GLatLng(28.003344016555072, -82.4509334564209)
	    ], "#0033EE", 1, 0.5, "#00ffef", 0.2);
	maplist[map_id].addOverlay(polygon); 
}
function getPoints(service_id)  {
    var bounds = getBoundary('map');
	//alert ("bounds:" + bounds); 
	var url = 'pub.php?srv=getpoints&service_id=' + service_id;
	new Ajax.Request(url, {
		  method: 'get',
		  onSuccess: function(transport) {displayPoints(transport.responseText);}
		});
    return;
} // end getPoints()
function displayPoints(returntext) {
	var plist = document.getElementById('plist');
	var points;
	var gMarker;
	eval(returntext);
	var pointCount = 0;
 
	for (i in points) {pointCount++;}

	plist.innerHTML = "<p class='info'>Found " + pointCount + " Points</p>";	
	clearMaps('map');
	maplist['map'].setZoom(14);
	polyHampTerr('map');
	for (i in points) {
		gMarker = createMarker('map', points[i].lat, points[i].lng, decodeURI(points[i].blurb), points[i].icon );
		points[i].marker = gMarker;
		plist.innerHTML += decodeURI(points[i].list); 
	} // end for
	pointsObj = points;
} // end displayPoints()
function showPointInfo(rowid) {
	//alert("showPointInfo:: rowid:" + rowid);
	if (rowid > 0) {
		for (i in pointsObj) {
			if (pointsObj[i].key == rowid ) {
				var gMarker = pointsObj[i].marker;
				gMarker.openInfoWindowHtml(decodeURI(pointsObj[i].blurb));
			} // end if
		} // end for
	} // end if rowid > 0
} // end showPointInfo()
function mapRefresh(map_id) {
	var ndx  = $('mapservice').selectedIndex;	
	var sid  = $('mapservice')[ndx].value;
	getPoints(sid);
} // end mapRefresh()


function chooseMapIcon(objId, iconname) {
	// objId is destination form field.
	//alert("chooseMapIcon:: objId:" + objId + " iconname:" + iconname);
	$(objId).value = iconname;
	//var iconObject = objId + "_icon";
	//$(iconObject).src = iconlist[iconname].image;
	return;
} // end chooseMapIcon()


