var footprints = {}, customFootprints = {};

var footprintPrecision = 60;

function calcFootprint(id, points) {
	var satInfo = PLib.QuickFind(id);
	var lat = satInfo.latitude, lon = satInfo.longitude, alt = satInfo.altitude;
	var sat = new GLatLng(lat, lon);
	if (points < 3) {
		return [ sat ];
	}
	var perimeter = new Array();
	var dg = 2.0*3.1415/points;
	var re = 6375.0;
	var visibilityCos = re/(re + alt);
	var visibilitySin = Math.sqrt(1 - visibilityCos*visibilityCos);
	for (var i = 0; i < points; i++) {
		perimeter.push(cart2sph(rotate([visibilitySin*Math.cos(i*dg), visibilitySin*Math.sin(i*dg), visibilityCos], satInfo)));
	}
	if (sat.distanceFrom(new GLatLng(90, 0)) < sat.distanceFrom(perimeter[0])) {
		perimeter.push(new GLatLng(perimeter[0].lat(), perimeter[0].lng()));
		for (var i = points - 1; i >= 0; i--) {
			perimeter.push(new GLatLng(85, perimeter[i].lng()));
		}
		perimeter.push(new GLatLng(85, perimeter[points - 1].lng()));
		perimeter.push(new GLatLng(perimeter[0].lat(), perimeter[0].lng()));
	}
	else if (sat.distanceFrom(new GLatLng(-90, 0)) < sat.distanceFrom(perimeter[0])) {
		perimeter.push(new GLatLng(perimeter[0].lat(), perimeter[0].lng()));
		for (var i = points - 1; i >= 0; i--) {
			perimeter.push(new GLatLng(-85, perimeter[i].lng()));
		}
		perimeter.push(new GLatLng(-85, perimeter[points - 1].lng()));
		perimeter.push(new GLatLng(perimeter[0].lat(), perimeter[0].lng()));
	}
	else {
		perimeter.push(new GLatLng(perimeter[0].lat(), perimeter[0].lng()));
	}
	return perimeter;
}

function recalcFootprint(id) {
	destroyFootprint(id);
	createFootprint(id, false);
}

function createFootprint(id, flash) {
	if (flash) {
		footprints[id] = new GPolygon(calcFootprint(id, footprintPrecision),"#EA0000", 1, 0.8, "#E14000", 0.6);
		map.addOverlay(footprints[id]);
		setTimeout("dimFootprint('"+ id + "')", 3000);
	}
	else {
		footprints[id] = new GPolygon(calcFootprint(id, footprintPrecision),"#00EA00", 1, 0.4, "#E1E100", 0.2);
		map.addOverlay(footprints[id]);
	}
}

function destroyFootprint(id) {
	map.removeOverlay(footprints[id]);
	delete footprints[id];
}

function highlightFootprint(id) {
	if (!footprints[id]) {
		return;
	}
	if (customFootprints[id]) {
		footprints[id].setStrokeStyle({ color: customFootprints[id][0], opacity: 0.8 });
		footprints[id].setFillStyle({ color: customFootprints[id][1], opacity: 0.6 });
	}
	else {
		footprints[id].setStrokeStyle({ color: "#EA0000", opacity: 0.8 });
		footprints[id].setFillStyle({ color: "#E14000", opacity: 0.6 });
	}
}

function dimFootprint(id) {
	if (!footprints[id]) {
		return;
	}
	if (customFootprints[id]) {
		footprints[id].setStrokeStyle({ color: customFootprints[id][0], opacity: 0.4 });
		footprints[id].setFillStyle({ color: customFootprints[id][1], opacity: 0.2 });
	}
	else {
		footprints[id].setStrokeStyle({ color: "#00EA00", opacity: 0.4 });
		footprints[id].setFillStyle({ color: "#E1E100", opacity: 0.2 });
	}
}

function setFootprintPrecision() {
	if (typeof map.addOverlay != "function") {
		return;
	}
	if (!isNumeric($("input#set_footprintprecision")[0].value)) {
		$("input#set_footprintprecision")[0].value = "20";
	};
	footprintPrecision = parseInt($("input#set_footprintprecision")[0].value);
	for (var id in footprints) {
		destroyFootprint(id);
		createFootprint(id, false);
	}
}

function toggleFootprints() {
	if (typeof map.addOverlay != "function") {
		return;
	}
	if ($("input#set_showfootprints")[0].checked) {
		for (var id in markers) {
			createFootprint(id, true);
		}
	}
	else {
		for (var id in footprints) {
			destroyFootprint(id);
		}
	}
}
