var gm = GMap2.prototype;
var gi = GIcon.prototype;
var wm = null;

// the document initilization function
$(document).ready(function () {
	wm = new GMap2(document.getElementById("map"));
	wm._init();
	add_points();
	wm._add_markers(wm.markers);

});


function map_goto() {
	var v  = {'loc': $("#weatherAddress").val(), 'type': 'locate' };
	$.post("/mapinfo.php", v, function(data) {
		// Unserialize JSON data
		eval("data = "+data);
		if (data['error']) {
			alert(data['error']);
		} else {
			var z = 9;
			wm.setCenter(new GLatLng(data['lat'], data['lng']), z);	
		}
	});
}

function reset_map() {
	wm._deleteCircle();
	wm.mgr.clearMarkers();
	wm._add_markers(wm.markers);
	wm._recenter();
	$('#weatherAddress').val("");
}


function ap(n, lat, lng, zoom, type, title) {
	wm.points[n] = new GLatLng(lat, lng);
	// create an array of markers for each zoom level
	if (!wm.markers[zoom]) {
		wm.markers[zoom] = new Array();
	}
	var i = wm.markers[zoom].length;
	var icon;
	switch(type) {
		case 1: icon = wm.iconG; break;
		case 2: icon = wm.iconR; break;
		default: icon = wm.iconR;
	}
	wm.markers[zoom][i] = new GMarker(wm.points[n], {title: title, icon: icon});
	// icon needs to be saved to the point so it can be retrieved
	// in _getSearchMarkers
	wm.points[n].icon = icon;
	wm.markers[zoom][i].id = n;
	GEvent.addListener(wm.markers[zoom][i], "click", markerEvent);
}

var markerEvent = function() {
	if (this.html) { 
		this.openInfoWindowHtml(this.html); 
	} else {
		var m = this;
		var v  = {'type': 'info', 'id': m.id };
		$.post("/mapinfo.php", v, function(data) {
			m.html = data;
			m.openInfoWindowHtml(m.html);
		});
	}
}

// takes a point and a radius and finds the zoom level
// that contains a circle defined by them
gm._getZoomLevel = function(lt, ln, r)
{
	var deg = Number(r)/69.172;
	var sw = new GLatLng(lt-deg, ln-deg);
	var ne = new GLatLng(lt+deg, ln+deg);
	var bnds = new GLatLngBounds(sw, ne);

	return this.getBoundsZoomLevel(bnds);
}

gm._getSearchMarkers = function(center, d)
{
	var r = d*1.609344*1000;
	if (this.sMarkers[3]) {
		while (this.sMarkers[3].pop()) { ; }
	} else {
		this.sMarkers[3] = new Array();
	}
	var j = 0;
	for (i in this.points) {
		if (center.distanceFrom(this.points[i]) < r) {
			j = this.sMarkers[3].length;
			this.sMarkers[3][j] = new GMarker(this.points[i], this.points[i].icon);
			this.sMarkers[3][j].id = i;
			GEvent.addListener(this.sMarkers[3][j], "click", markerEvent);
		}
	}
	this.mgr.clearMarkers();
	this._add_markers(this.sMarkers);
}

// adds markers to the map
// m is an array of arrays
// the index of the top level array is the zoomlevel in which to display
// that array of markers
gm._add_markers = function(m) {
	for (i in m) {
		this.mgr.addMarkers(m[i], i);
	}
	this.mgr.refresh();
}

// deletes a radius circle and frees the memory
gm._deleteCircle = function() {
	if (this.mapCircle) {
		this.removeOverlay(this.mapCircle);
		delete this.mapCircle;
	}
}

gm._drawCircle = function(lat, lng, rad) {
	var r = rad*1.609344;
	// this._deleteCircle();
	this.mapCircle = new BDCCCircle(new GLatLng(lat, lng), r, "#ef5f10", 3, 0.5, false);
	this.addOverlay(this.mapCircle);
}

gm._init = function() {
	var m = this;
	m._recenter();
	m.addControl(new GLargeMapControl());
	m.addControl(new GMapTypeControl());
	m.addMapType(G_PHYSICAL_MAP);
	m.mgr = new MarkerManager(wm);
	m.points = {};
	m.markers = {};
	m.sMarkers = {};
	m.iconR = new GIcon();
	m.iconR._init('mm_20_yellow.png');
	m.iconG = new GIcon();
	m.iconG._init('mm_20_green.png');
	m.iconB = new GIcon();
	m.iconB._init('mm_20_blue.png');
}

// recenters the map above the continental US
gm._recenter = function() {
	this.setCenter(new GLatLng(39.50, -96.35), 3);
}

gi._init = function(image) {
	var i = this;
	i.image = 'http://labs.google.com/ridefinder/images/'+image;
	i.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
	i.iconSize = new GSize(12, 20);
	i.shadowSize = new GSize(22, 20);
	i.iconAnchor = new GPoint(6, 20);
	i.infoWindowAnchor = new GPoint(5, 1);
}

