﻿var map;
var pinCount = 0;

function createVirtualEarth() {
	map = new VEMap("virtualEarthHost");
	map.LoadMap(new VELatLong(0, 0), 2, VEMapStyle.Road, false, VEMapMode.Mode2D, false);
	map.SetScaleBarDistanceUnit(VEDistanceUnit.Kilometers);
}

function clearPins() {
	map.Clear();
	pinCount = 0;
}

function addPushPin(msg) {
	if (pinCount >= 100 || msg.LatLong == null) {
		msg.PinNum = 0;
		return;
	}

	pinCount++;
	msg.PinNum = pinCount;
	
	var pin = new VEShape(VEShapeType.Pushpin, msg.LatLong);
	if (msg.Type == "MessageCluster") {
		pin.SetTitle(msg.ShortName);
		pin.SetDescription(messageClusterDescription(msg));
	}
	else if (msg.Type == "MapCluster") {
		pin.SetTitle("Messages for this location");
		pin.SetDescription(mapClusterDescription(msg));
		for (var i = 0; i < msg.Items.length; i++) {
			msg.Items[i].PinNum = msg.PinNum;
		}
	}
	else {
		pin.SetTitle(msg.ShortName);
		pin.SetDescription(messageDescription(msg));
	}
	
	var className = "pushpin";
	if (msg.Type == "Job") {
		className = "pushpin_job";
	}
	else if (msg.Type == "MapCluster") {
		className = "pushpin_cluster";
	}
	
	pin.SetCustomIcon("<span class=\"" + className + "\">" + pinCount + "</span>");
	map.AddShape(pin);
	
}

function createLocateButton(msg) {
	if (msg.LatLong == null || msg.PinNum == 0) {
		return null;
	}
	
	var locateButton = document.createElement("a");
	if (msg.Type == "Job") {
		locateButton.className = "locateJobButton";
	}
	else {
		locateButton.className = "locateButton";
	}
	locateButton.setAttribute("href", "javascript:locatePin(" + msg.LatLong.Latitude + "," + msg.LatLong.Longitude + ")");
	locateButton.setAttribute("title", "Locate on the map.");
	locateButton.appendChild(document.createTextNode(msg.PinNum));
	
	return locateButton;	
}

function messageDescription(msg) {
	var html =
		msg.Posted.substring(0, 10) + " |  " + msg.Type + "<br />" +
		"<a href=\"javascript:showDetails('" +
		msg.Type + "', " + msg.Id + ")\">" +
		msg.Subject + "</a>";
	if (msg.StartDate != null || msg.Deadline != null) {
		html += "<br /><br />";
	}
	if (msg.StartDate != null) {
		html += "<b>Start Date:</b> " + msg.StartDate + "<br />";
	}
	if (msg.Deadline != null) {
		html += "<b>Deadline:</b> " + msg.Deadline + "<br />";
	}
	
	return html;
}

function messageClusterDescription(cluster) {
	var html = "";
	if (cluster.StartDate != null) {
		html += "<b>Start Date:</b>  " + cluster.StartDate + "<br />";
	}
	if (cluster.Deadline != null) {
		html += "<b>Deadline:</b> " + cluster.Deadline + "<br />";
	}
	if (html != "") {
		html += "<br />";
	}
	
	for (var i = 0; i < cluster.Messages.length; i++) {
		html += cluster.Messages[i].Posted.substring(0, 10) + " | " + cluster.Messages[i].Type + "<br />" +
			"<a href=\"javascript:showDetails('" + cluster.Messages[i].Type + "', " +
			cluster.Messages[i].Id + ")\">" +
			cluster.Messages[i].Subject + "</a><br /><br />";		
	}
	
	return html;
}

function mapClusterDescription(cluster) {
	var html = "";
	var msg;
	
	for (var i = 0; i < cluster.Items.length; i++) {
		msg = cluster.Items[i];
		if (msg.Type == "MessageCluster") {
			html += "<div class=\"infobox_subtitle\">" + msg.ShortName + "</div>\r\n";
			for (var j = 0; j < msg.Messages.length; j++) {
				html += msg.Messages[j].Posted.substr(0, 10) + " | " + msg.Messages[j].Type + "<br />";
				html += "<a href=\"javascript:showDetails('" + msg.Messages[j].Type + "', " +
						msg.Messages[j].Id + ")\">" + msg.Messages[j].Subject + "</a><br /><br />";
			}
		}
		else {
			html += "<div class=\"infobox_subtitle\">" + msg.ShortName + "</div>\r\n";
			html += msg.Posted.substr(0, 10) + " | " + msg.Type + "<br />";
			html += "<a href=\"javascript:showDetails('" + msg.Type + "', " +
					msg.Id + ")\">" + msg.Subject + "</a><br /><br />";
		}
	}
	
	return html;
}

function locatePin(lat, long) {
	map.SetCenterAndZoom(new VELatLong(lat, long), 6);
}