/* ----- General ----- */

var pospopup_markerinfo = [];
var icons = {};

//Init icons
function init_icons() {

	//Unit icons
	icons["unit"] = new Array();

	for (i in uniticonpaths) {

		icons["unit"][i] = {
			"iconsize": [32, 32],
			"iconanchor": [16, 16],
			//"infowindowanchor": [16, 1],
			"image": uniticonpaths[i]
		};

	}

	//Journey inbetween arrow
	if (line_color_id != undefined) {

		icons["arrow"] = new Array();
		icons["arrow"][line_color_id] = new Array();
		
		for (var i=0; i<360; i+=45) {

			icons["arrow"][line_color_id][i] = {
				"iconsize": [16, 16],
				"iconanchor": [8, 8],
				//"infowindowanchor": [8, 1],
				"image": "resources/images/map/arrow/" + line_color_id + "/" + i + ".png"
			};

		}

	}

	//Start journey icon
	icons["start"] = {
		"iconsize": [20, 34],
		"iconanchor": [10, 34],
		//"infowindowanchor": [, ],
		"image": "http://www.google.com/mapfiles/dd-start.png"
	};

	//End journey icon
	icons["end"] = {
		"iconsize": [20, 34],
		"iconanchor": [10, 34],
		//"infowindowanchor": [10, ],
		"image": "http://www.google.com/mapfiles/dd-end.png"
	};

}

//Init general
function initgeneral() {

	//Multimap marker click handling
	if (mapstraction.api == "multimap") {

		/*
		basemap.addEventHandler("click", function(type, target) {
			if (target instanceof MMMarkerOverlay) {
				alert(target.mid);
			}
		});
		*/

		basemap.addEventHandler("click", multimapmarkerclicked);

	}

}

//Load marker html info
function googlemarkerclicked() {

	var currmarker = this;

	var markerid = this.mid;

	//Handle marker click
	markerclicked(markerid, currmarker);

}

//Multimap marker clicked
function multimapmarkerclicked(type, target) {

	if (target instanceof MMMarkerOverlay) {

		//Handle marker click
		markerclicked(target.mid, target);

	}

}

//Load marker html info
function markerclicked(markerid, markerele) {

	if (pospopup_markerinfo[markerid] == undefined) {

		try {

			nocache_urladdon = "&nocache=" + new Date().getTime();
			geturl = baseurl + "&t=positionpopup&marker=" + markerid + nocache_urladdon;
			var loader = new net.ContentLoader(geturl, function() {
				eval("var jsondata = " + this.req.responseText);

				pospopup_markerinfo[markerid] = jsondata;

				//Open marker info window
				openmarkerinfowindow(markerele, markerid);

			});

		} catch (e) {
			alert(e.message + "\nWas trying to add marker info for specified marker " + markerid + ", try clicking refresh");
		}


	} else {

		//Open marker info window
		openmarkerinfowindow(markerele, markerid);

	}

}

//Open marker info window
function openmarkerinfowindow(markerele, markerid) {

	//Google marker click handling
	if (mapstraction.api == "google") {

		markerele.openInfoWindowHtml(genmarkerinfohtml(pospopup_markerinfo[markerid]));

	} else if (mapstraction.api == "multimap") {

		markerele.openInfoBox(genmarkerinfohtml(pospopup_markerinfo[markerid]));

	}

}

//Generate marker info html
function genmarkerinfohtml(markerdata) {

	var baloonhtml = "";
	baloonhtml += "<div class=\"baloonhtml\">\n";
	baloonhtml += "<div class=\"baloontitle\">Date:</div>" + markerdata["datetime"] + "<br />\n";
	baloonhtml += "<div class=\"baloontitle\">Speed:</div>" + markerdata["speed_km"] + " kph (" + markerdata["speed_mph"]  + " mph)<br />\n";
	baloonhtml += "<div class=\"baloontitle\">Heading:</div>" + markerdata["deg"] + " (deg)<br />\n";
	baloonhtml += "<div class=\"baloontitle\">Altitude:</div>" + markerdata["alt"] + " m<br />\n";
	baloonhtml += "<div class=\"baloontitle\">Lat:</div>" + markerdata["lat"] + "<br />\n";
	baloonhtml += "<div class=\"baloontitle\">Lon:</div>" + markerdata["lon"] + "<br />\n";
	baloonhtml += "<div class=\"baloontitle\">View on:</div>" + "<a target=\"_blank\" href=\"http://maps.google.co.uk/maps?f=q&hl=en&q=" + markerdata["lat"] + "," + markerdata["lon"] + "\">Google</a> | <a target=\"_blank\" href=\"http://www.multimap.com/maps/#t=l&map=" + markerdata["lat"] + "," + markerdata["lon"] + "|14|4&loc=GB:" + markerdata["lat"] + ":" + markerdata["lon"] + ":14|" + markerdata["lat"] + "," + markerdata["lon"] + "|Lat:%20" + markerdata["lat"] + ",%20Lon:%20" + markerdata["lon"] + "\">Multimap</a><br />\n";
	baloonhtml += "</div>";

	return baloonhtml;

}


/* ----- Live Map ----- */

//Vars
var updatetimeout_live;
var trackerunitchk;
//var trackerunitmarker = {};
//var trackerunitactive = [];
var trackerunitlive = [];
//var lastid;

//Check / uncheck all unit checkboxes
function unit_chkboxchange(status) {
	for (i in trackerunitchk) {
		$(trackerunitchk[i]).checked = status;
	}
}

//Change checkbox
function checkboxchange(chckboxid) {
	if ($(chckboxid).checked == true) {
		$(chckboxid).checked = false;
	} else {
		$(chckboxid).checked = true;
	}
}

//Change autoupdate status
function autoupdatechanged(autoupdate) {

	//Clear timer
	if (updatetimeout_live) {
		window.clearTimeout(updatetimeout_live);
	}

	//If checked
	if (radiogetselected("autoupdate") == "1") {

		//Update display / restart timer
		updatedisplay();

	}

}

//Update display
function updatedisplay() {

	try {

		nocache_urladdon = "&nocache=" + new Date().getTime();
		geturl = baseurl + "&t=liveupdate" + nocache_urladdon;
		var loader = new net.ContentLoader(geturl, function() {
			eval("var jsondata = " + this.req.responseText);

			//Update current position data
			currpositions = jsondata;

			//Update markers
			placeliveicons();

		});

		//If checked
		if (radiogetselected("autoupdate") == "1") {
			//Set display to autoupdate
			updatetimeout_live = window.setTimeout("updatedisplay();", dispupd_interval);
		}

	} catch (e) {
		alert(e.message + "\nWas trying to retrieve new data, try clicking refresh");
	}

}

//Init live icons
function init_liveicons() {

	//Go through units
	for (unit in unitinfo) {

		//Add on extra fields
		unitinfo[unit]["lat"] = undefined;
		unitinfo[unit]["lon"] = undefined;
		unitinfo[unit]["marker"] = undefined;

	}

}

//Place live icons
function placeliveicons() {

	var recenter_required = false;

	//Go through units
	for (unit in unitinfo) {

		//If unit should be shown
		if ($("trackerunit_" + unit).checked) {

			//If have data for the unit
			if (currpositions["livepos"][unit] != undefined) {

				var point = new LatLonPoint(currpositions["livepos"][unit]["lat"], currpositions["livepos"][unit]["lon"]);
				var unitnotmoved = point.equals(new LatLonPoint(unitinfo[unit]["lat"], unitinfo[unit]["lon"]));

				//If unit marker is currently hidden or is shown but has has moved
				if ( (unitinfo[unit]["marker"] == undefined) || ((unitinfo[unit]["marker"] != undefined) && (unitnotmoved == false)) ) {

					//If marker currently shown, remove it
					if (unitinfo[unit]["marker"] != undefined) {
						mapstraction.removeMarker(unitinfo[unit]["marker"]);
						unitinfo[unit]["marker"] = undefined;
					}

					//Create point
					var point = new LatLonPoint(currpositions["livepos"][unit]["lat"], currpositions["livepos"][unit]["lon"]);

					//Icon id
					var iconid = unitinfo[unit]["icon"];

					//Create marker
					var marker = new Marker(point);

					//Set marker icon
					marker.setIcon(icons["unit"][iconid]["image"], icons["unit"][iconid]["iconsize"], icons["unit"][iconid]["iconanchor"]);
					marker.setShadowIcon(icons["unit"][iconid]["image"], icons["unit"][iconid]["iconsize"]);

					//Add marker to map
					mapstraction.addMarker(marker);

					if (mapstraction.api == "google") {

						marker.proprietary_marker.mid = currpositions["livepos"][unit]["id"];

						//GEvent.addListener(marker.proprietary_marker, "mousedown", function() {alert(99)});
						GEvent.addListener(marker.proprietary_marker, "mousedown", googlemarkerclicked);

					} else if (mapstraction.api == "multimap") {
						marker.proprietary_marker.mid = currpositions["livepos"][unit]["id"];
					}

					//Save marker back to unit
					unitinfo[unit]["marker"] = marker;

					//Save new lat/lon back to unit
					unitinfo[unit]["lat"] = currpositions["livepos"][unit]["lat"];
					unitinfo[unit]["lon"] = currpositions["livepos"][unit]["lon"];

					//Recenter required
					recenter_required = true;

				}

			}

		} else {
			//Unit should not be shown

			//If unit marker is currently shown
			if (unitinfo[unit]["marker"] != undefined) {

				//Remove marker
				mapstraction.removeMarker(unitinfo[unit]["marker"]);
				unitinfo[unit]["marker"] = undefined;

				//Recenter required
				recenter_required = true;

			}

		}

	}

	//If icons were moved / newly shown
	if (recenter_required == true) {

		//Recenter map on icons
		centerlivemap();

	}

}

//Center live map on shown icons
function centerlivemap() {

	var havemarkers = false;

	//Go through units
	for (unit in unitinfo) {

		//If unit marker is currently hidden or is shown but has has moved
		if (unitinfo[unit]["marker"] != undefined) {
			havemarkers = true;
		}

	}

	//If there are markers
	if (havemarkers) {
		//Auto center and zoom on markers
		mapstraction.autoCenterAndZoom();
	}

}

//Show / hide live icon
function showhideliveicon(unit, statushow) {

	//ignore args

	//Go through all icons hide/show as needed depending on checkbox status
	placeliveicons();

}

//Show / hide all live icon
function showallliveicon(statushow) {

	//ignore args

	//Go through all icons hide/show as needed depending on checkbox status
	placeliveicons();

}


/* ----- History Map ----- */

var trackhist_avail = {};
var markermng_zoomlevel = new Array();

//Select history tracker
function selecthisttracker(unit_id) {
	document.location.href = currpageunit_starturl + "&unit=" + unit_id;
}

//Add a polyline, then center and zoom to it
function addpolylinecenterzoomall(polyline_points, color) {

	//Go through lat/lon cords
	var points = [];
	for (var line in polyline_points) {

		//Place point on map
		var point = new LatLonPoint(polyline_points[line][0], polyline_points[line][1]);
		points.push(point);

	}

	//If there are points
	if (points.length > 0) {

		//Init polyline
		var polyline = new Polyline(points);

		//Set properties
		polyline.setColor(color);
		polyline.setOpacity(0.5);

		//Add polyline
		mapstraction.addPolyline(polyline);

		//Center and zoom on points (polyline)
		//done here, because it only needs doing once, and the line is only drawn once
		mapstraction.centerAndZoomOnPoints(points);

		if (mapstraction.api == "google") {
			basemap.savePosition();
		}

	}


}

//Zoom end event fired
function evtzoomend(oldzoom, newzoom) {

	//Handle zoom change
	handlezoomchange(newzoom);

}

//Zoom end event fired
function multimapzoomchange(type, target, oldzoom, newzoom) {

	//Handle zoom change
	handlezoomchange(newzoom);

}

//Handle zoom change
function handlezoomchange(zoomlevel) {

	//If marker manager for specified zoom level is not already created, create it
	if (inarray(markermng_zoomlevel, zoomlevel) == false) {

		//Add marker manager for specified zoom level
		addmmangerforzoom(zoomlevel);

	} else {

		//Show markers only for specifed zoom level
		markerszoomlevel(zoomlevel);

	}

}

//Show markers only for specifed zoom level
function markerszoomlevel(zoomlevel) {

	//Show markers only for specified filter level
	mapstraction.removeAllFilters();
	mapstraction.addFilter("zoomlevel", "le", zoomlevel);
	mapstraction.addFilter("zoomlevel", "ge", zoomlevel);
	mapstraction.doFilter();

}

//Add marker manager for specified zoom level
function addmmangerforzoom(zoomlevel) {

	try {

		nocache_urladdon = "&nocache=" + new Date().getTime();
		geturl = baseurl + "&t=markerdata&unit=" +  currtrackerunit + "&date=" + currdate + "&zoomlevel=" + zoomlevel + nocache_urladdon;
		var loader = new net.ContentLoader(geturl, function() {
			eval("var jsondata = " + this.req.responseText);

			//Go through lat/lon cords
			var gmarkers = [];
			var i = 0;
			var totalicon = jsondata["data"].length;
			for (var markerarr in jsondata["data"]) {

				//Choose icon
				var icon = chooseicon("archive", i, jsondata["data"][markerarr][3], totalicon);

				//Create point
				var point = new LatLonPoint(jsondata["data"][markerarr][1], jsondata["data"][markerarr][2]);

				//Create marker
				var marker = new Marker(point);

				//Set zoom level to show this marker
				marker.setAttribute("zoomlevel", zoomlevel);

				//Set marker icon
				marker.setIcon(icon["image"], icon["iconsize"], icon["iconanchor"]);
				marker.setShadowIcon(icon["image"], icon["iconsize"]);

				//Add marker to map
				mapstraction.addMarker(marker);

				if (mapstraction.api == "google") {
					marker.proprietary_marker.mid = jsondata["data"][markerarr][0];
					GEvent.addListener(marker.proprietary_marker, "mousedown", googlemarkerclicked);
				} else if (mapstraction.api == "multimap") {
					marker.proprietary_marker.mid = jsondata["data"][markerarr][0];
				}

				i++;

			}

			//Add to list of marker managers allready created
			markermng_zoomlevel.push(zoomlevel);

			//Show markers only for specifed zoom level
			markerszoomlevel(zoomlevel);

		});

	} catch (e) {
		alert(e.message + "\nWas trying to add marker manager for specified zoom level " + zoomlevel + ", try clicking refresh");
	}

}

//Choose icon
function chooseicon(type, count, dir, totalicon) {

	//Choose icon
	var icon;

	if (count == 0) {
		icon = icons["start"];
	} else if (count+1 == totalicon) {
		icon = icons["end"];
	} else {

		//Round direction to be out of 10
		dir = Math.round(dir/45) * 45;
		dir = (dir == 360) ? 0 : dir;

		icon = icons["arrow"][line_color_id][dir];

	}

	return icon;

}

