﻿var dateType = 2;

function init() {
	createSilverlight();
	createVirtualEarth();
	createCalendar();
	
	var today = new Date();
	loadData(today, today);
}

function changeDateType() {
	var posted = document.getElementById('dateTypePosted');
	var start = document.getElementById('dateTypeStart');
	var deadline = document.getElementById('dateTypeDeadline');
	
	if (posted.checked) {
		dateType = 1;
	}
	else if (start.checked) {
		dateType = 2;
	}
	else if (deadline.checked) {
		dateType = 3;
	}
	else {
		posted.checked = true;
		start.checked = false;
		deadline.checked = false;
		dateType = 1;
	}
	
	loadData(calendar_dateStart, calendar_dateEnd);
}

function loadData(date1, date2) {
	var request = null;
	if (window.XMLHttpRequest) {
	    request = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
	    try {
	        request = new ActiveXObject("Msxml2.XMLHTTP");
	    }
	    catch (e) {
	        try {
	            request = new ActiveXObject("Microsoft.XMLHTTP");
	        }
	        catch (e) {}
	    }
	}
	
	if (request == null) {
	    alert("It seems, that your browser does not support the Javascript XMLHttpRequest object, that is necessary to use this application.");
	    return;
	}
	
	switch (dateType) {
		case 2:
			request.open('GET', 'http://zfs.informatik.rwth-aachen.de/dbworldmap/dbworldmap.asmx/GetMessagesByStartDate?start=' + dateToString(date1) + '&end=' + dateToString(date2) + '&zoom=1', false);
			break;
		case 3:
			request.open('GET', 'http://zfs.informatik.rwth-aachen.de/dbworldmap/dbworldmap.asmx/GetMessagesByDeadline?start=' + dateToString(date1) + '&end=' + dateToString(date2) + '&zoom=1', false);
			break;
		default:
			request.open('GET', 'http://zfs.informatik.rwth-aachen.de/dbworldmap/dbworldmap.asmx/GetMessagesByPostingDate?start=' + dateToString(date1) + '&end=' + dateToString(date2) + '&zoom=1', false);
			break;
	}
	request.send(null);
	
	var xml = request.responseXML;
	createMessageList(xml);
}

function findData() {
    var city = document.getElementById("city");
    
    if (city.value != null && city.value != "") {
        map.Find(null, city.value, null, null, 0, 1, false, false, false, false, findDataCallback);
    }
    else {
        sendQuery(0, 0);
    }
}

function findDataCallback(layer, results, places, moreResults, errorMsg) {
    if (places != null && places.length > 0) {
        sendQuery(places[0].LatLong.Latitude, places[0].LatLong.Longitude);
        map.SetCenterAndZoom(new VELatLong(places[0].LatLong.Latitude, places[0].LatLong.Longitude), 6);
    }
    else {
        alert("Sorry, but we cannot find the city you entered.");
    }
}

function sendQuery(latitude, longitude) {
	var inQuery = document.getElementById("search");
	var inStart = document.getElementById("beginDate");
	var inEnd = document.getElementById("endDate");
	var inType = document.getElementById("msgType");

	if (inQuery.value == "" && inStart.value == "" && inEnd.value == "" && latitude == 0 && longitude == 0) {
		alert("Please enter at least a search term, a start or end date or a city.");
		return;
	}

	var query = inQuery.value;
	
	var start = new Date(1, 0, 1);
	if (inStart.value != "") {
		var tmp = new Date(inStart.value);
		if (tmp != null) {
			start = tmp;
		}
	}

	var end = new Date(9999, 11, 31);
	if (inEnd.value != "") {
		var tmp = new Date(inEnd.value);
		if (tmp != null) {
			end = tmp;
		}
	}
	
	var dateType = "posting";
	switch (document.getElementById("searchDateType").selectedIndex) {
		case 1:
			dateType = "start";
			break;
		case 2:
			dateType = "deadline";
			break;
		default:
			dateType = "posting";
			break;
	}
	
	var msgType = inType.value;
	
	var request = null;
	if (window.XMLHttpRequest) {
	    request = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
	    try {
	        request = new ActiveXObject("Msxml2.XMLHTTP");
	    }
	    catch (e) {
	        try {
	            request = new ActiveXObject("Microsoft.XMLHTTP");
	        }
	        catch (e) {}
	    }
	}
	
	if (request == null) {
	    alert("It seems, that your browser does not support the Javascript XMLHttpRequest object, that is necessary to use this application.");
	    return;
	}

	var url = "http://zfs.informatik.rwth-aachen.de/dbworldmap/dbworldmap.asmx/FindMessages2?";
	url += "search=" + query + "&start=" + dateToString(start)
		+ "&end=" + dateToString(end) + "&dateType=" + dateType
		+ "&latitude=" + latitude + "&longitude=" + longitude
		+ "&msgType=" + msgType;
	
	request.open("GET", url, false);
	request.send(null);
	
	var xml = request.responseXML;
	createMessageList(xml);
}

function createMessageList(xml) {
	var container = document.getElementById("listContainer");
	clear(container);
	clearPins();
	
	var nodes = xml.documentElement.selectNodes("/ArrayOfItem/Item");
	var mapItems = new Array(nodes.length);
	var items = new Array();
	
	for (var i = 0; i < nodes.length; i++) {
		var node = nodes[i];
		if (node.getAttribute("xsi:type") == "MessageCluster") {
			mapItems[i] = new DBMessageCluster(node);
		}
		else if (node.getAttribute("xsi:type") == "Message") {
			mapItems[i] = new DBMessage(node);
		}
		else if (node.getAttribute("xsi:type") == "MapCluster") {
			mapItems[i] = new DBMapCluster(node);
		}
	}
	
	switch (dateType) {
		case 3:
			mapItems.sort(sortByDeadline);
			break;
		case 2:
			mapItems.sort(sortByStartDate);
			break;
		default:
			mapItems.sort(sortByPostingDate);
			break;
	}
	
	for (var i = 0; i < mapItems.length; i++) {
		if (mapItems[i] != null) {
			addPushPin(mapItems[i]);
			if (mapItems[i].Type == "MapCluster") {
				for (var j = 0; j < mapItems[i].Items.length; j++) {
					items.push(mapItems[i].Items[j]);
				}
			}
			else {
				items.push(mapItems[i]);
			}
		}
	}
	
	switch (dateType) {
		case 3:
			items.sort(sortByDeadline);
			break;
		case 2:
			items.sort(sortByStartDate);
			break;
		default:
			items.sort(sortByPostingDate);
			break;
	}
	
	for (var i = 0; i < items.length; i++) {
		if (items[i] !=  undefined && items[i] != null) {
			printMessageInfo(items[i], container);
		}
	}
}


function printMessageInfo(msg, container) {
	var divMsgContainer = document.createElement("div");
	divMsgContainer.className = "messageContainer";
	container.appendChild(divMsgContainer);

	var btn = createLocateButton(msg);
	if (btn != null) {
		divMsgContainer.appendChild(btn);
	}
	
	var divMsg = document.createElement("div");
	divMsg.className = "messageInfo";
	divMsgContainer.appendChild(divMsg);
	
	if (msg.Type != "MessageCluster") {
		var divType = document.createElement("div");
		divType.className = "messageType";
		divType.appendChild(document.createTextNode(msg.Posted.substring(0, 10) + " | " + msg.Type));
		divMsg.appendChild(divType);

		var showLink = document.createElement("a");
		showLink.setAttribute("href", "javascript:showDetails(\"" + msg.Type + "\"," + msg.Id + ")");
		showLink.appendChild(document.createTextNode(msg.Subject));
		divMsg.appendChild(showLink);
		
		if (msg.StartDate != null || msg.Deadline != null) {
			divDetails = document.createElement("div");
			divDetails.className = "messageDetails";
			if (msg.StartDate != null) {
				divDetails.appendChild(document.createTextNode("Start date: " + msg.StartDate));
				divDetails.appendChild(document.createElement("br"));
			}
			if (msg.Deadline != null) {
				divDetails.appendChild(document.createTextNode("Deadline: " + msg.Deadline));
				divDetails.appendChild(document.createElement("br"));
			}
			
			var externalLink = document.createElement("a");
			externalLink.setAttribute("href", "../dbworldmap/ShowDetails.aspx?type=" + msg.Type + "&id=" + msg.Id);
			externalLink.setAttribute("target", "_blank");
			externalLink.className = "linkToMessage";
			externalLink.appendChild(document.createTextNode("Link to this message"));
			divDetails.appendChild(externalLink);
			
			divMsg.appendChild(divDetails);
		}
	}
	else {
		var bold = document.createElement("b");
		bold.appendChild(document.createTextNode(msg.ShortName));
		divMsg.appendChild(bold);
		
		if (msg.StartDate != null || msg.Deadline != null) {
			divDetails = document.createElement("div");
			divDetails.className = "messageDetails";
			if (msg.StartDate != null) {
				divDetails.appendChild(document.createTextNode("Start date: " + msg.StartDate));
				if (msg.Deadline != null) {
					divDetails.appendChild(document.createElement("br"));
				}
			}
			if (msg.Deadline != null) {
				divDetails.appendChild(document.createTextNode("Deadline: " + msg.Deadline));
			}
			divMsg.appendChild(divDetails);
		}
		
		divCluster = document.createElement("div");
		divCluster.className = "clusteredMessages";
		for (var i = 0; i < msg.Messages.length; i++) {
			var divType = document.createElement("div");
			divType.className = "messageType";
			divType.appendChild(document.createTextNode(msg.Messages[i].Posted.substring(0, 10) + " | " + msg.Messages[i].Type));
			divCluster.appendChild(divType);
			
			var showLink = document.createElement("a");
			showLink.setAttribute("href", "javascript:showDetails(\"" + msg.Messages[i].Type + "\", " + msg.Messages[i].Id + ")");
			showLink.appendChild(document.createTextNode(msg.Messages[i].Subject));
			divCluster.appendChild(showLink);
			
			divCluster.appendChild(document.createElement("br"));
			
			var externalLink = document.createElement("a");
			externalLink.setAttribute("href", "../dbworldmap/ShowDetails.aspx?type=" + msg.Messages[i].Type + "&id=" + msg.Messages[i].Id);
			externalLink.setAttribute("target", "_blank");
			externalLink.className = "linkToMessage";
			externalLink.appendChild(document.createTextNode("Link to this message"));
			divCluster.appendChild(externalLink);
		}
		divMsg.appendChild(divCluster);
	}
}

function clear(elem) {
	for (var i = elem.childNodes.length - 1; i >= 0; i--) {
		elem.removeChild(elem.childNodes[i]);
	}
}

function sortByPostingDate(a, b) {
	if (a.Posted < b.Posted) return 1;
	if (a.Posted > b.Posted) return -1;
	return 0;
}

function sortByStartDate(a, b) {
	if (a.StartDate == null && b.StartDate == null) return 0;
	
	if (b.StartDate == null || a.StartDate < b.StartDate) return -1;
	if (a.StartDate == null || a.StartDate > b.StartDate) return 1;
	
	if (a.Posted < b.Posted) return -1;
	if (a.Posted > b.Posted) return 1;
	return 0;
}

function sortByDeadline(a, b) {
	if (a.Deadline == null && b.Deadline == null) return 0;
	
	if (b.Deadline == null || a.Deadline < b.Deadline) return -1;
	if (a.Deadline == null || a.Deadline > b.Deadline) return 1;

	if (a.Posted < b.Posted) return -1;
	if (a.Posted > b.Posted) return 1;
	return 0;
}

function DBMessage(node) {
	this.Id = parseInt(node.getAttribute("ID"));
	this.Type = node.getAttribute("Type");
	this.PinNum = 0;
	this.Posted = node.selectSingleNode("Posted").firstChild.nodeValue;
	this.Subject = node.selectSingleNode("Subject").firstChild.nodeValue;
	
	var n = node.selectSingleNode("ShortName");
	if (n != null) {
		this.ShortName = n.firstChild.nodeValue;
	}
	n = node.selectSingleNode("StartDate");
	if (n != null && n.firstChild.nodeValue != "0001-01-01T00:00:00") {
		this.StartDate = n.firstChild.nodeValue.substring(0, 10);
	}
	n = node.selectSingleNode("Deadline");
	if (n != null && n.firstChild.nodeValue != "0001-01-01T00:00:00") {
		this.Deadline = n.firstChild.nodeValue.substring(0, 10);
	}
	n = node.selectSingleNode("Location");
	if (n != null) {
		this.LatLong = new VELatLong(parseFloat(n.selectSingleNode("Latitude").firstChild.nodeValue),
									  parseFloat(n.selectSingleNode("Longitude").firstChild.nodeValue));
	}
}

DBMessage.prototype.Id;
DBMessage.prototype.Type;
DBMessage.prototype.Posted;
DBMessage.prototype.Subject;
DBMessage.prototype.ShortName;
DBMessage.prototype.StartDate;
DBMessage.prototype.Deadline;
DBMessage.prototype.LatLong;
DBMessage.prototype.PinNum;

function DBMessageCluster(node) {
	this.Id = parseInt(node.getAttribute("ID"));
	this.Type = "MessageCluster";
	this.PinNum = 0;
	this.Posted = node.selectSingleNode("Posted").firstChild.nodeValue;
	this.Subject = node.selectSingleNode("Subject").firstChild.nodeValue;
	
	var n = node.selectSingleNode("ShortName");
	if (n != null) {
		this.ShortName = n.firstChild.nodeValue;
	}
	n = node.selectSingleNode("StartDate");
	if (n != null && n.firstChild.nodeValue != "0001-01-01T00:00:00") {
		this.StartDate = n.firstChild.nodeValue.substring(0, 10);
	}
	n = node.selectSingleNode("Deadline");
	if (n != null && n.firstChild.nodeValue != "0001-01-01T00:00:00") {
		this.Deadline = n.firstChild.nodeValue.substring(0, 10);
	}
	n = node.selectSingleNode("Location");
	if (n != null) {
		this.LatLong = new VELatLong(parseFloat(n.selectSingleNode("Latitude").firstChild.nodeValue),
									  parseFloat(n.selectSingleNode("Longitude").firstChild.nodeValue));
	}
	
	var msgs = node.selectNodes("Messages/Message");
	this.Messages = new Array(msgs.length);
	for (var i = 0; i < msgs.length; i++) {
		this.Messages[i] = new DBMessage(msgs[i]);
	}
	this.Messages.sort(sortByPostingDate);
}

DBMessageCluster.prototype.Id;
DBMessageCluster.prototype.Type;
DBMessageCluster.prototype.Posted;
DBMessageCluster.prototype.Subject;
DBMessageCluster.prototype.ShortName;
DBMessageCluster.prototype.StartDate;
DBMessageCluster.prototype.Deadline;
DBMessageCluster.prototype.Messages;
DBMessageCluster.prototype.LatLong;
DBMessageCluster.prototype.PinNum;


function DBMapCluster(node) {
	this.Type = "MapCluster";
	this.PinNum = 0;
	var n = node.selectSingleNode("Location");
	if (n != null) {
		this.LatLong = new VELatLong(parseFloat(n.selectSingleNode("Latitude").firstChild.nodeValue),
									 parseFloat(n.selectSingleNode("Longitude").firstChild.nodeValue));
	}
	
	n = node.selectSingleNode("Posted");
	if (n != null && n.firstChild.nodeValue != "0001-01-01T00:00:00") {
		this.Posted = n.firstChild.nodeValue;
	}
	n = node.selectSingleNode("StartDate");
	if (n != null && n.firstChild.nodeValue != "0001-01-01T00:00:00") {
		this.StartDate = n.firstChild.nodeValue.substring(0, 10);
	}
	n = node.selectSingleNode("Deadline");
	if (n != null && n.firstChild.nodeValue != "0001-01-01T00:00:00") {
		this.Deadline = n.firstChild.nodeValue.substring(0, 10);
	}
	
	var msgs = node.selectNodes("Items/Item");
	this.Items = new Array(msgs.length);
	for (var i = 0; i < msgs.length; i++) {
		if (msgs[i].getAttribute("xsi:type") == "MessageCluster") {
			this.Items[i] = new DBMessageCluster(msgs[i]);
		}
		else if (msgs[i].getAttribute("xsi:type") == "Message") {
			this.Items[i] = new DBMessage(msgs[i]);
		}
	}
	
	this.Items.sort(sortByPostingDate);
}

DBMapCluster.prototype.Type;
DBMapCluster.prototype.Posted;
DBMapCluster.prototype.StartDate;
DBMapCluster.prototype.Deadline;
DBMapCluster.prototype.LatLong;
DBMapCluster.prototype.Items;
DBMapCluster.prototype.PinNum;