/*
Plugin Name: Mapping
Plugin URI: http://greenerfishers.org/
Description: Associate geographic information with posts and include maps in posts and listings
Version: 0.4
Author: Christopher Hansen
Author URI: http://emilyandchristopher.com/

	Copyright 2008, Christopher Hansen  

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

TODO:
	- refactor javascript into admin and public includes
	- add default properties to database
	- properties admin page
*/

google.load("maps", "2");
google.load("prototype", "1.6");
//google.load("scriptaculous", "1.8.1");

var debugAlertCount = 5;



// Display a debug message on the console if the browser supports it.
// If the browser does not support console debug messages, display an alert.
// If we're displaying alerts, don't display too many.
function debug(what) {
	if (window.console) {
		window.console.log(what);
	} else {
		if (debugAlertCount-- > 0) {
			alert(what);
		}
	}
}

// Are there any points in the global points array?
// The points array is defined in the Mapping head template
function anyPoints() {
	var pointFound = false;
	if (typeof points) {
		$A(points).each(function(point) {
			if (parseFloat(point.latitude) && parseFloat(point.longitude)) {
				pointFound = true;
			}
		});
	} else {
		// points not defined
	}
	return pointFound;
}

// Build the map according to the global points array
function buildMap() {
	if (anyPoints()) {
		$('map_container').show();
		var map = new GMap2($('map_container'));
		var keyboardHandler = new GKeyboardHandler(map);
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
//		var mt = map.getMapTypes();
		map.addMapType(G_PHYSICAL_MAP);
		map.setMapType(eval(mapType));
		map.enableContinuousZoom();
		map.enableDoubleClickZoom();
	
		bounds = new GLatLngBounds;
		points.each(function(point) {
			if (parseFloat(point.latitude) && parseFloat(point.longitude)) {
				var latLng = new GLatLng(parseFloat(point.latitude), parseFloat(point.longitude));
				point.latLng = latLng;
				bounds.extend(latLng);
			}
		});

		if (points.length == 1) {
			map.setCenter(bounds.getCenter(), defaultZoom);
		} else {
			map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
		}

		points.each(function(point) {
			if (point.latLng) {
				// need a latLng before we can set the center
				var marker = new GMarker(point.latLng);
					// need to set the center before we can add overlays
				map.addOverlay(marker);
				GEvent.addListener(marker, "click", function() {
					marker.openInfoWindowHtml(point.html);

				})
			}
		});
	} else {
//		debug('AnyPoints returned false.');
	}
}