/*
 * Hotwire a.courselink to display a course info box.  The links are expected to be direct links to "kurs- och
 * programkatalogen" (e.g. <a href="http://www.kth.se/student/kurser/kurs/DD2354" class="courselink">...</a>).
 */

function isSwedish(tag) {
	var lang = tag.parents("[lang]").attr("lang");
	return lang && lang.match(/^sv/);
}

$(document).ready(function(){
	$("a.courselink").click(function(){
		$("body").css("cursor", "progress");
		var theLink = $(this);
		theLink.css("cursor", "progress");
		$("div.box").replaceWith('');

		var href = theLink.attr('href');
		$.ajax({
			url:href,
			dataType: 'html',
			success: function (xml, status) {
				var x = $(xml).find('#article');
				var head = x.find('h1').text();
				var content = "<h2 style='width: auto; margin: 0;padding:8px 16px 8px 4px; color: black; background: #a7a7a7 url(http://www.kth.se/img/h-grad-gray.png) repeat-x; font-size: 1.3em; font-weight: bold; position:relative; top: -5px; left: 1px;'>" + head + "</h2>";
				var recruitmentText = $(x).find('div#recruitmentText');
				if (recruitmentText.html()) {
					content += "<div style='padding: 0 4px;'>" + recruitmentText.html(); + "</div>";
				}
				var applicationInfo = $(x).find('div#applicationInfo');
				if (applicationInfo.html()) {
					content += "<div>" + applicationInfo.html() + "</div>";
				}
				var courseHomePage = $(x).find("#courseHomePage");
				if (courseHomePage.html()) {
					content += courseHomePage.html();
				}
				var linkText;
				if (isSwedish(theLink)) {
					linkText = "Utf\u00F6rligare information i kurs- och programkatalogen";
				} else {
					linkText = "More information in the course and programme directory";
				}
				content += "<a href='" + href + "' style='width:auto;'>" + linkText + "</a>";
				var closeButton = "<div style='position:absolute;top:18px;right:18px;cursor:pointer;z-index:1;' class='closeButton'><img src='http://www.kth.se/img/close.png' alt='St\u00E4ng'></div>";
				theLink.css("display", "block");
				theLink.after("<div style='display:block;width:500px;margin-top:12px;z-index:17;' class='box'><div class='boxOuter'><div class='boxInner'><div class='boxInner2'><div style='margin-right:15px;'>"+content+closeButton+"</div></div></div></div></div>");
				
				var parent = theLink.parent();
				var theBox = parent.find('.box');
				$("a", theBox).css("width", "auto");
				parent.find('.closeButton').click(function(){
					theBox.replaceWith('');
				});
				var scrolltop = (document.documentElement.scrollTop ?
		                document.documentElement.scrollTop :
		                document.body.scrollTop);
				var viewbottom = scrolltop + $(window).height();
				var bottomshadow = 20;
				var boxbottom = theBox.offset().top + theBox.height() - bottomshadow;
				if (boxbottom > viewbottom) {
					var moveup = boxbottom - viewbottom;
					theBox.animate({marginTop: (-moveup) + "px"}, 500);
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				location.href = href;
			},
			complete: function (XMLHttpRequest, textStatus) {
				$("body").css("cursor", "auto");
				theLink.css("cursor", "pointer");
			}
		});
		return false;
	});
});
