//<!-- 

var g_NurrMapTimeout = -1; // controls the map reload schedule.

function nurr_update_data(mapEl, more) {
	var urlBase = 'mapdata?'; // '/cgi-bin/map-async.py?'; 

	var bounds = mapEl.myMap.getBounds();
	var ne = bounds.getNorthEast();
	var sw = bounds.getSouthWest();
	var eldims = get_element_dimensions(mapEl);
	var qs = 'width='+eldims[2]+'&height='+eldims[3]+'&';
	if(typeof(more) != "undefined") {
		qs = more + '&';
	}
	qs += 'swlat=' + sw.lat() + '&swlng=' + sw.lng() + '&nelat='+ ne.lat() + '&nelng=' + ne.lng();
	var req = new async_request(urlBase + qs, 'GET');
}

function nurr_maps_loaded(mapElementName) {
	var mapEl = document.getElementById(mapElementName);
	mapEl.myMap = new google.maps.Map2(mapEl);
	mapEl.myMap.setCenter(new google.maps.LatLng(0, 0), 2);
	mapEl.myMap.setUIToDefault();
	mapEl.myMap.setMapType(G_HYBRID_MAP);
	mapEl.myMap.myItemTable = new Array();
	mapEl.myMap.myOverlays = new Array();
	
	google.maps.Event.addListener(mapEl.myMap, "moveend", function() {
		if (g_NurrMapTimeout != -1) {
			clearTimeout(g_NurrMapTimeout);
			g_NurrMapTimeout = -1;
		}
		g_NurrMapTimeout = setTimeout(function() { nurr_update_data(mapEl); }, 100);
	});
	nurr_update_data(mapEl);
}

function nurr_map_init() {
	google.load('maps', '2', { "callback" :
		 function() { 
			nurr_maps_loaded("map"); 
		 } 
	});
}

function create_nurr_info_window_element(nurrRecord)
{
	var el = document.createElement("div");
	el.id = "nurr" + nurrRecord.id;
	el.className = "nurrMapTabContainer";
	el.innerHTML = '<div class="loading">Loading...</div>';
	return el;
}

function map_item_handler(req) {
	var mapEl = document.getElementById("map");
	mapEl.myMap.updateCurrentTab(function(tab) {
		mapEl.myMap.myItemTable[req.response.nurrid].myElement.innerHTML = req.response.content;
	});
}

function create_nurr_overlay(mapEl, nurrRecord)
{
	var ico = new google.maps.Icon(G_DEFAULT_ICON);
	ico.image = nurrRecord.icon;
	ico.iconSize = new google.maps.Size(24, 24);
//	ico.shadow = '/static/images/mapicon-shadow.png';
	ico.shadow = null;
	ico.anchor = null;
//	Debug(ico.image + ", location " + nurrRecord.lat + ", " + nurrRecord.lng);
	var overlay = new google.maps.Marker(new google.maps.LatLng(nurrRecord.lat, nurrRecord.lng), { 
		icon: ico,
		clickable: true
	});
	overlay.myRecord = nurrRecord;
	// 1. create the element to stick in the info window
	var el = create_nurr_info_window_element(nurrRecord);
	// 2. stick it in there already (that's what she said BANANG!)
	var infoWindowTabs = [ new google.maps.InfoWindowTab("Hello!", el) ];
	overlay.bindInfoWindowTabs(infoWindowTabs);
	overlay.myContentLoaded = false;
	overlay.myElement = el;
	mapEl.myMap.myItemTable[nurrRecord.id] = overlay;

	google.maps.Event.addListener(overlay, 'click', function() { 
		if (!overlay.myContentLoaded) { // shit, load it!
			new async_request('mapitem?id=' + nurrRecord.id, 'GET');
			overlay.myContentLoaded = true; // it'll get here eventually, probably
		}
	}); 
	return overlay;
}

function map_handler(o) {
	// everything server-side is now in o.response yay
	var mapEl = document.getElementById("map");
//	mapEl.myMap.clearOverlays();
//	if (mapEl.myMap.myLoadingOverlay != null) {
//		mapEl.myMap.addOverlay(mapEl.myMap.myLoadingOverlay);
//	}
	var index = new Array();
	for(var k = 0; k < mapEl.myMap.myOverlays.length; k++) {
		var ol = mapEl.myMap.myOverlays[k];
		index[ol.myRecord.id] = ol;
		ol.markedForDeletion = true;
	}
	for(var k = 0; k < o.response.results.length; k++) {
		if (o.response.results[k].id in index) {
			index[o.response.results[k].id].markedForDeletion = false;
		} else {
			var overlay = create_nurr_overlay(mapEl, o.response.results[k]);
			mapEl.myMap.myOverlays.push(overlay); // argh, fine, i'll keep my own copy.
			mapEl.myMap.addOverlay(overlay);
			var jobEl = document.getElementById("showjob_" + o.response.results[k].job_id);
			if (!jobEl.checked) {
				overlay.hide();
			}
		}
	}

	while (k < mapEl.myMap.myOverlays.length) {
		if (mapEl.myMap.myOverlays[k].markedForDeletion) {
//			Debug("removing unused element " + k);
			mapEl.myMap.removeOverlay(mapEl.myMap.myOverlays[k]);
			mapEl.myMap.myOverlays.splice(k, 1);
		} else {
			k++;
//			Debug("retaining in-use element " + k);
		}
	}
}

function show_job(checker, jobid) 
{
	var mapEl = document.getElementById("map");
	for (var k = 0; k < mapEl.myMap.myOverlays.length; k++) {
		var marker = mapEl.myMap.myOverlays[k];
		if (marker.myRecord.job_id == jobid) {
			if(!checker.checked) { 
				marker.hide();
			} else {
				marker.show();
			}
		}
	}
}

// -->
