﻿var shader = null;
var detailsDiv = null;

function showDetails(type, id) {
	map.HideInfoBox();
	
	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;
	}

	request.open('GET', 'http://zfs.informatik.rwth-aachen.de/dbworldmap/dbworldmap.asmx/GetMessageDetails?msgType=' + type + '&id=' + id, false);
	request.send(null);
	var xml = request.responseXML;
	
	showShader();
	var body = document.getElementsByTagName("body")[0];
	
	detailsDiv = document.createElement("div");
	detailsDiv.className = "details";
	
	var close = document.createElement("a");
	close.setAttribute("href", "javascript:hideDetails()");
	close.setAttribute("title", "Close");
	close.className = "closeButton";
	close.appendChild(document.createTextNode(" "));
	detailsDiv.appendChild(close);
	
	var node = xml.documentElement.selectSingleNode("Subject");
	var headline = document.createElement("h1");
	headline.appendChild(document.createTextNode(node.firstChild.nodeValue));
	detailsDiv.appendChild(headline);
	
	var tableContainer = document.createElement("div");
	tableContainer.className = "tableContainer";
	
	var table = document.createElement("table");
	var tbody = document.createElement("tbody");
	table.appendChild(tbody);
	
	node = xml.documentElement.selectSingleNode("Posted");
	var posted = node.firstChild.nodeValue;
	tbody.appendChild(addDetail("Posted:", posted.substr(0, 10)
		+ " " + posted.substr(11, 5)));
	
	var author = xml.documentElement.selectSingleNode("Author").firstChild.nodeValue;
	node = xml.documentElement.selectSingleNode("AuthorMail");
	var link = document.createElement("a");
	link.setAttribute("href", "mailto:" + node.firstChild.nodeValue.replace("@", " at "));
	link.appendChild(document.createTextNode(author));
	tbody.appendChild(addDetail("Author:", link));
	
	node = xml.documentElement.selectSingleNode("StartDate");
	if (node != null && node.firstChild.nodeValue != "0001-01-01T00:00:00") {
		tbody.appendChild(addDetail("Start Date:", node.firstChild.nodeValue.substr(0, 10)));
	}

	node = xml.documentElement.selectSingleNode("Deadline");
	if (node != null && node.firstChild.nodeValue != "0001-01-01T00:00:00") {
		tbody.appendChild(addDetail("Deadline:", node.firstChild.nodeValue.substr(0, 10)));
	}

	switch (type) {
		case "Book":
			showBookDetails(tbody, xml);
			break;
		case "Conference":
			showConferenceDetails(tbody, xml);
			break;
		case "Grant":
			showGrantDetails(tbody, xml);
			break;
		case "Job":
			showJobDetails(tbody, xml);
			break;
		case "Journal":
			showJournalDetails(tbody, xml);
			break;
		case "News":
			showNewsDetails(tbody, xml);
			break;
		case "Software":
			showSoftwareDetails(tbody, xml);
			break;
	}
	
	tableContainer.appendChild(table);
	detailsDiv.appendChild(tableContainer);
	
	var iframe = document.createElement("iframe");
	detailsDiv.appendChild(iframe);
	body.appendChild(detailsDiv);
	iframe.src = 'http://zfs.informatik.rwth-aachen.de/dbworldmap/message.aspx?type=' + type + '&id=' + id;
}

function showConferenceDetails(tbody, xml) {
	var shortName = null;
	var city = null;
	var country = null;
	var webpage = null;
	
	var details = xml.documentElement.selectNodes("Details/Detail");
	for (var i = 0; i < details.length; i++) {
		var detailName = details[i].selectSingleNode("Name").firstChild.nodeValue;
		
		switch (detailName) {
			case "ShortName":
				shortName = details[i].selectSingleNode("Value").firstChild.nodeValue;
				break;
			case "City":
				city = details[i].selectSingleNode("Value").firstChild.nodeValue;
				break;
			case "Country":
				country = details[i].selectSingleNode("Value").firstChild.nodeValue;
				break;
			case "Webpage":
				webpage = details[i].selectSingleNode("Value").firstChild.nodeValue;
				break;
		}
	}
	
	if (shortName != null) {
		tbody.appendChild(addDetail("Short Name:", shortName));
	}
	if (city != null && country != null) {
		tbody.appendChild(addDetail("Location:", city + ", " + country));
	}
	if (webpage != null) {
		if (webpage.substr(0, 7) != "http://") {
			webpage = "http://" + webpage;
		}
		link = document.createElement("a");
		link.setAttribute("href", webpage);
		link.setAttribute("target", "_blank");
		link.appendChild(document.createTextNode(webpage));
		tbody.appendChild(addDetail("Webpage:", link));
	}
}

function showJobDetails(tbody, xml) {
	var organization = null;
	var city = null;
	var country = null;
	var webpage = null;
	
	var details = xml.documentElement.selectNodes("Details/Detail");
	for (var i = 0; i < details.length; i++) {
		var detailName = details[i].selectSingleNode("Name").firstChild.nodeValue;
		
		switch (detailName) {
			case "Organization":
				organization = details[i].selectSingleNode("Value").firstChild.nodeValue;
				break;
			case "City":
				city = details[i].selectSingleNode("Value").firstChild.nodeValue;
				break;
			case "Country":
				country = details[i].selectSingleNode("Value").firstChild.nodeValue;
				break;
			case "Webpage":
				webpage = details[i].selectSingleNode("Value").firstChild.nodeValue;
				break;
		}
	}
	
	if (organization != null) {
		tbody.appendChild(addDetail("Organization:", organization));
	}
	if (city != null && country != null) {
		tbody.appendChild(addDetail("Location:", city + ", " + country));
	}
	if (webpage != null) {
		if (webpage.substr(0, 7) != "http://") {
			webpage = "http://" + webpage;
		}
		link = document.createElement("a");
		link.setAttribute("href", webpage);
		link.setAttribute("target", "_blank");
		link.appendChild(document.createTextNode(webpage));
		tbody.appendChild(addDetail("Webpage:", link));
	}
}

function showBookDetails(tbody, xml) {
	var title = null;
	var topic = null;
	var webpage = null;
	
	var details = xml.documentElement.selectNodes("Details/Detail");
	for (var i = 0; i < details.length; i++) {
		var detailName = details[i].selectSingleNode("Name").firstChild.nodeValue;
		
		switch (detailName) {
			case "Title":
				title = details[i].selectSingleNode("Value").firstChild.nodeValue;
				break;
			case "Topic":
				topic = details[i].selectSingleNode("Value").firstChild.nodeValue;
				break;
			case "Webpage":
				webpage = details[i].selectSingleNode("Value").firstChild.nodeValue;
				break;
		}
	}
	
	if (title != null) {
		tbody.appendChild(addDetail("Title:", title));
	}
	if (topic != null) {
		tbody.appendChild(addDetail("Topic:", topic));
	}
	if (webpage != null) {
		if (webpage.substr(0, 7) != "http://") {
			webpage = "http://" + webpage;
		}
		link = document.createElement("a");
		link.setAttribute("href", webpage);
		link.setAttribute("target", "_blank");
		link.appendChild(document.createTextNode(webpage));
		tbody.appendChild(addDetail("Webpage:", link));
	}
}

function showGrantDetails(tbody, xml) {
	var topic = null;
	var country = null;
	var webpage = null;
	
	var details = xml.documentElement.selectNodes("Details/Detail");
	for (var i = 0; i < details.length; i++) {
		var detailName = details[i].selectSingleNode("Name").firstChild.nodeValue;
		
		switch (detailName) {
			case "Topic":
				topic = details[i].selectSingleNode("Value").firstChild.nodeValue;
				break;
			case "Country":
				country = details[i].selectSingleNode("Value").firstChild.nodeValue;
				break;
			case "Webpage":
				webpage = details[i].selectSingleNode("Value").firstChild.nodeValue;
				break;
		}
	}
	
	if (topic != null) {
		tbody.appendChild(addDetail("Topic:", topic));
	}
	if (country != null) {
		tbody.appendChild(addDetail("Country:", country));
	}
	if (webpage != null) {
		if (webpage.substr(0, 7) != "http://") {
			webpage = "http://" + webpage;
		}
		link = document.createElement("a");
		link.setAttribute("href", webpage);
		link.setAttribute("target", "_blank");
		link.appendChild(document.createTextNode(webpage));
		tbody.appendChild(addDetail("Webpage:", link));
	}
}

function showJournalDetails(tbody, xml) {
	var shortName = null;
	var title = null;
	var topic = null;
	var webpage = null;
	
	var details = xml.documentElement.selectNodes("Details/Detail");
	for (var i = 0; i < details.length; i++) {
		var detailName = details[i].selectSingleNode("Name").firstChild.nodeValue;
		
		switch (detailName) {
			case "Title":
				title = details[i].selectSingleNode("Value").firstChild.nodeValue;
				break;
			case "ShortName":
				shortName = details[i].selectSingleNode("Value").firstChild.nodeValue;
				break;
			case "Topic":
				topic = details[i].selectSingleNode("Value").firstChild.nodeValue;
				break;
			case "Webpage":
				webpage = details[i].selectSingleNode("Value").firstChild.nodeValue;
				break;
		}
	}
	
	if (title != null) {
		tbody.appendChild(addDetail("Short Name:", shortName));
	}
	if (topic != null) {
		tbody.appendChild(addDetail("Topic:", topic));
	}
	if (webpage != null) {
		if (webpage.substr(0, 7) != "http://") {
			webpage = "http://" + webpage;
		}
		link = document.createElement("a");
		link.setAttribute("href", webpage);
		link.setAttribute("target", "_blank");
		link.appendChild(document.createTextNode(webpage));
		tbody.appendChild(addDetail("Webpage:", link));
	}
}

function showNewsDetails(tbody, xml) {
	var country = null;
	var webpage = null;
	
	var details = xml.documentElement.selectNodes("Details/Detail");
	for (var i = 0; i < details.length; i++) {
		var detailName = details[i].selectSingleNode("Name").firstChild.nodeValue;
		
		switch (detailName) {
			case "Country":
				country = details[i].selectSingleNode("Value").firstChild.nodeValue;
				break;
			case "Webpage":
				webpage = details[i].selectSingleNode("Value").firstChild.nodeValue;
				break;
		}
	}
	
	if (country != null) {
		tbody.appendChild(addDetail("Country:", title));
	}
	if (webpage != null) {
		if (webpage.substr(0, 7) != "http://") {
			webpage = "http://" + webpage;
		}
		link = document.createElement("a");
		link.setAttribute("href", webpage);
		link.setAttribute("target", "_blank");
		link.appendChild(document.createTextNode(webpage));
		tbody.appendChild(addDetail("Webpage:", link));
	}
}

function showSoftwareDetails(tbody, xml) {
	var title = null;
	var organization = null;
	var topic = null;
	var webpage = null;
	
	var details = xml.documentElement.selectNodes("Details/Detail");
	for (var i = 0; i < details.length; i++) {
		var detailName = details[i].selectSingleNode("Name").firstChild.nodeValue;
		
		switch (detailName) {
			case "Title":
				title = details[i].selectSingleNode("Value").firstChild.nodeValue;
				break;
			case "Organization":
				organization = details[i].selectSingleNode("Value").firstChild.nodeValue;
				break;
			case "Topic":
				topic = details[i].selectSingleNode("Value").firstChild.nodeValue;
				break;
			case "Webpage":
				webpage = details[i].selectSingleNode("Value").firstChild.nodeValue;
				break;
		}
	}
	
	if (title != null) {
		tbody.appendChild(addDetail("Title:", title));
	}
	if (organization != null) {
		tbody.appendChild(addDetail("Organization:", organization));
	}
	if (topic != null) {
		tbody.appendChild(addDetail("Topic:", topic));
	}
	if (webpage != null) {
		if (webpage.substr(0, 7) != "http://") {
			webpage = "http://" + webpage;
		}
		link = document.createElement("a");
		link.setAttribute("href", webpage);
		link.setAttribute("target", "_blank");
		link.appendChild(document.createTextNode(webpage));
		tbody.appendChild(addDetail("Webpage:", link));
	}
}

function addDetail(name, value) {
	var row = document.createElement("tr");
	var cell;
	
	cell = document.createElement("th");
	cell.appendChild(document.createTextNode(name));
	row.appendChild(cell);
	
	cell = document.createElement("td");
	if (typeof value == "string") {
		cell.appendChild(document.createTextNode(value));
	}
	else {
		cell.appendChild(value);
	}
	row.appendChild(cell);
	
	return row;
}

function hideDetails() {
	detailsDiv.parentNode.removeChild(detailsDiv);
	detialsDiv = null;
	hideShader();
}

function showShader() {
	if (shader == null) {
		shader = document.createElement("div");
		var body = document.getElementsByTagName("body")[0];
		
		shader.className = "shader";
		body.appendChild(shader);
		setOpacity(shader, 0.6);		
	}
}

function hideShader() {
	shader.parentNode.removeChild(shader);
	shader = null;
}

function setOpacity( element, alpha ) {
	var style = element.style;
	if( style.MozOpacity != undefined ) { //Moz and older
		style.MozOpacity = alpha;
	}
	else if( style.filter != undefined ) { //IE
		style.filter = "alpha(opacity=0)";
		element.filters.alpha.opacity = ( alpha * 100 );
	}
	else if( style.opacity != undefined ) { //Opera
		style.opacity = alpha;
	}
}