var ua = navigator.userAgent.toLowerCase();
var fixSel = (ua.indexOf('win') != -1 && ua.indexOf('msie') != -1) || (ua.indexOf('gecko') != -1 && ua.indexOf('safari') == -1);

function getOffsetY(event) {
	return event.offsetY == null ? event.layerY + 18 : event.offsetY;
}

function selMouseDown(event, sel) {
	if (fixSel && sel.second && getOffsetY(event) < sel.offsetHeight - 1) sel.second = false;
}

function selMouseUp(event, sel, url) {
	if (fixSel && event) {
		if (sel.second) {
			sel.second = false;
		} else if (getOffsetY(event) < sel.offsetHeight - 1) {
			sel.second = true;
			url = null;
		}
	}
	if (url) {
		var n = sel.options[sel.selectedIndex].value;
		if (n != null) {
			if (n) url += escape(n).replace(/\+/g, "%2B").replace(/%20/g, "+") + '/';
			document.location.href = url;
		}
	}
}

function toggle(e) {
	var e = document.getElementById(e).style;
	e.display = e.display == 'none' || e.display == '' ?  'block' : 'none';
}

function writeTo(u, d) {
	var l="\x6D\x61\x69";
	l+="\x6C\x74\x6F\x3A"+u;
	window.location=l+"\x40"+d;
}

function openWin(url, title, width, height, status, resizeable, scrollbars) {
	var win = window.open(url, title, "width=" + width + ",height=" + height + ",left=" + Math.round((screen.width - width) / 2) + ",top=" + Math.round((screen.height - height) / 2 - 40) + ",toolbar=0,menubar=0,location=0,status=" + (status ? 1 : 0) + ",resizable=" + (resizeable ? 1 : 0) + ",scrollbars=" + (scrollbars ? 1 : 0));
	win.focus();
	return win;
}

function editComment(id) {
	if (lastCommentId && lastCommentId != id) {
		var prevId = lastCommentId;
		lastCommentId = null;
		editComment(prevId);
	}
	var v = document.getElementById("comment-view-" + id);
	var e = document.getElementById("comment-edit-" + id);
	if (v.style.display == 'none') {
		v.style.display = 'block';
		e.style.display = 'none';
		lastCommentId = null;
	} else {
		v.style.display = 'none';
		e.style.display = 'block';
		lastCommentId = id;
		setTimeout("scrollToComment('" + id + "')", 1);
	}
	return false;
}

function scrollToComment(id) {
	var c = document.getElementById("comment-" + id);
	if (c.scrollIntoView) {
		c.scrollIntoView();
		window.scrollBy(0, -8);
	} else {
		document.location.hash = "comment-" + id;
	}
}

function deleteComment(url) {
	if (confirm("Do you really want to delete your comment?")) {
		document.location = url;
	}
	return false;
}

function postComment(id, key) {
	var form = document["comment_" + id];
	form.key.value = key;
	form.submit();
	return false;
}

window.onload = function()  {
	var h = document.location.hash;
	if (h.indexOf("#comment-") == 0) {
		scrollToComment(h.substring(9));
	}
}