var maps = new Object();
var markers = new Object();
var paths = new Object();
var aId = new Object();
var tId = new Object();
var lapMark;

function mark(mapNumber, lapid) {
	row = document.getElementById("lap"+lapid);
	row.style.background = '#0000FF';
	lapMark = lapid;
	refreshLaps(mapNumber, lapid);
}

function unmark(mapNumber, lapid) {
	row = document.getElementById("lap"+lapid);
	row.style.background = '';
	if (lapMark==lapid)
		lapMark = false;
	refreshLaps(mapNumber, lapid);
}

function load(mapNumber) {
  if (GBrowserIsCompatible()) {
    maps[mapNumber] = new GMap2(document.getElementById("map"+mapNumber));
    maps[mapNumber].setCenter(new GLatLng(0,0), 0);
    maps[mapNumber].setMapType(G_SATELLITE_MAP);
    maps[mapNumber].addControl(new GLargeMapControl());
    maps[mapNumber].addControl(new GMapTypeControl());
  }
}

function refreshLaps(mapNumber, lapid) {
	if (lapid == lapMark || document.getElementById("lc"+lapid).checked) {
		addLapPath(mapNumber, lapid);
	} else {
		removeLapPath(mapNumber, lapid);
	}
}

function evalResult(data, response) {
	if (response==200) {
	 	//alert(data);
		eval(data);
	}
}

function handleZoom(data, response) {
 	if (response!=200 || !data) {
		alert("ERROR: New zoom level not saved to database\n"+data);
	}
}

function addActivityPath(mapNumber, aIdLocal, tIdLocal, path) {
 	if (!path)
 		path = "";
 	aId[mapNumber] = aIdLocal;
 	tId[mapNumber] = tIdLocal;
	GDownloadUrl(path+"wp-content/plugins/GPSdata/path.php?aid="+aIdLocal+"&map="+mapNumber, evalResult);
}

function addLapPath(mapNumber, lId) {
 	if (paths["l"+lId])
 		maps[mapNumber].addOverlay(paths["l"+lId]);
 	else
		GDownloadUrl("wp-content/plugins/GPSdata/path.php?lid="+lId+"&map="+mapNumber, evalResult);
}

function removeLapPath(mapNumber, lId) {
	maps[mapNumber].removeOverlay(paths["l"+lId]);
}

function refreshGraph(mapNumber) {
 	x = document.getElementById("x"+mapNumber);
 	xvalue = x.options[x.selectedIndex].value;
 	y = document.getElementById("y"+mapNumber);
 	yvalue = y.options[y.selectedIndex].value;
 	y2 = document.getElementById("y2"+mapNumber);
 	y2value = y2.options[y2.selectedIndex].value;
 	y2text = "";
 	if (y2value!="NONE")
 		y2text = "&y2="+y2value;
 	img = document.getElementById("graph"+mapNumber);
	img.src = "wp-content/plugins/GPSdata/graph.php?aid="+aId[mapNumber]+"&x="+xvalue+"&y="+yvalue+y2text+"&width=450";
}

