// Minify to tools.min.js!

$(function() {
	var $siteContent = $('#siteContent'),
	    corners = $.map(['ne', 'se', 'sw', 'nw'], function(item) {
	        return '<span class="qnav-corner qnav-corner-' + item + '"></span>';
	    });
	
	// Add corners to .qnav
	$('.qnav', $siteContent).append(corners.join(''));
	
	// Show versions:
	//  * Insert icon on mouseover on <ins> elements
	//  * Bind click handler to icon:
	//     - Remove icon
	//     - Add '.active' to all <ins> and <del> elements
	//     - Add text (removal date) is attribute 'datetime' is set on <del>
	$('ins', $siteContent).one('mouseover', function() {
		$('<img />', {
			src: '/gfx/showversions.png',
			title: 'Denne side indeholder opdaterede oplysninger. Klik her for at vise gamle (slettede) oplysninger.',
			'class': 'showversions-icon',
			click: function() {
				var $ins = $('ins', $siteContent),
					$del = $('del', $siteContent);
					
				$('.showversions-icon', $ins).remove();
				$ins.add($del).addClass('active');
				$del.each(function() {
					var $elm = $(this),
						ts = $elm.attr('datetime');
					
					if (ts) {
						$elm.append(document.createTextNode(' (Slettet ' + ts.substring(0, 10) + ' kl. ' + ts.substring(11, 19) + ')'));
					}
				});
			}
		}).appendTo(this);
	});
	
	// Append permalink to all headers with ID
	$('h1[id], h2[id], h3[id], h4[id], h5[id], h6[id]', $siteContent).each(function() {
		$('<a></a>', {
			href: '#' + this.id,
			'class': 'permalink-header',
			title: 'Permalink til dette afsnit (#' + this.id + ')'
		}).text('\u00B6').appendTo(this);
	});
});

function skift(elm, c) {
	elm.style.backgroundColor = c;
}

// Funktion til at vise billeder i et tilpasset vindue
function vis_billede(pic) { 
	var picWin = window.open('', 'picWin', 'width=50,height=50,scrollbars=no,resizable=yes,status=no,titlebar=no');
	picWin.document.open();
	picWin.document.write("<html>\n<head>\n<title>Vis billede [" + pic + "]</title>\n");
	picWin.document.write("<scr" + "ipt language=\"javasc" + "ript\" type=\"text/javascr" + "ipt\">\nself.focus();\n\nfunction tilpas_win()\n{\n    $billW = document.getElementById('billede').width + 40;\n    $billH = document.getElementById('billede').height + 50;\n    if ($billW > screen.availWidth) {\n        $billW = screen.availWidth;\n    }\n    if ($billH > screen.availHeight) {\n        $billH = screen.availHeight;\n    }\n    window.resizeTo($billW, $billH);\n    return true;\n}\n</scr" + "ipt>\n");
	picWin.document.write("</head>\n<body bgcolor=\"#ffffff\" text=\"#000000\" style=\"margin: 0\" onload=\"tilpas_win();\">\n");
	picWin.document.write("<table style=\"width: 100%; height: 100%; text-align: center; cursor: pointer;\" border=\"0\" onclick=\"window.close();\">\n<tr>\n    <td><img src=\"http://www.jensgram.dk/" + pic + "\" id=\"billede\" style=\"border: 1px solid #000000;\"></td>\n</tr>\n</table>\n</body>\n</html>"); 
	picWin.document.close();
}

function JgMenuCoreShow(index, items) {
	for (var i = 0; i < items.length; i++) {
		document.getElementById(items[i]).style.display = 'none';
	}
	document.getElementById(items[index]).style.display = 'block';
}

/**
 * Alias for document.getElementById.
 */
function r(id) {
	return document.getElementById(id);
}

/**
 * Toggles the style.display property of elm.
 * Returns true if the element is now displayed, false otherwise.
 */
function toggleDisp(elm, dVal) {
	var d;
	if (elm) {
		dVal = (dVal === null ? 'block' : dVal);
		d = (elm.style.display != 'none');
		elm.style.display = (d ? 'none' : dVal);
		return !d;
	}
	return false;
}

/**
 * Switches the text (s1 and s2) of elm.
 */
function swapText(elm, s1, s2) {
	elm.firstChild.nodeValue = (elm.firstChild.nodeValue == s1 ? s2 : s1);
}
