
function deletePost(node, id)
{
	var a = new ajaxCall();
	a.send("/jig_comment_delete.php", { 'id' : id }, _on_deletePostComplete, {'node':node});
}


function _on_deletePostComplete(result, data, userdata)
{
	var code = "ERROR";
	if (result)
	{
		if (data.indexOf("OK") == 0) code = "OK";
	}

	if (code != "OK") return;
	
	var node = findParentByClass(userdata.node, "post");
	if (!node) return;
	node.parentNode.removeChild(node);
}

function deletePuzzle(node, id, title)
{
	var msg = "You are about to delete the puzzle: '"+title+"'\nThis action is irreversible.\n\nDo you want to delete the puzzle?";
	if (!confirm(msg)) return;
		
	var a = new ajaxCall();
	a.send("/jig_delete.php", { 'id' : id }, _on_deletePuzzleComplete, {'node':node});
	
	node.innerHTML = "Deleting ...";
	node.className = "js_disabled";
}


function _on_deletePuzzleComplete(result, data, userdata)
{
	var code = "ERROR";
	if (result)
	{
		if (data.indexOf("OK") == 0) code = "OK";
	}

	if (code != "OK") return;
	
	var node = findParentByClass(userdata.node, "puzzle_item");
	if (!node) return;
	node.parentNode.removeChild(node);
}

function findParentByClass(node, name)
{
	while (node.className != name)
	{
		node = node.parentNode;
		if (!node) return null
	}
	return node;
}

function findChildNodeByClass(node, name)
{
	if (node.className == name) return node;
	for (var k in node.childNodes)
	{
		var n = node.childNodes[k];
		var r = findChildNodeByClass(n, name);
		if (r) return r;
	}
	return null;
}

function removeBookmarkFromList(node, id)
{
	var a = new ajaxCall();
	a.send("/jig_bookmark_remove.php", { 'id' : id }, _on_removeBookmarkFromListComplete, {'node':node});
	
	node.innerHTML = "Removing ...";
	node.className = "js_disabled";
}

function _on_removeBookmarkFromListComplete(result, data, userdata)
{
	var code = "ERROR";
	if (result)
	{
		if (data.indexOf("OK") == 0) code = "OK";
	}
	if (code != "OK") return;
	
	var node = findParentByClass(userdata.node, "puzzle_item");
	if (!node) return;
	node.parentNode.removeChild(node);
}

function bookmarkFromList(node, id)
{
	var a = new ajaxCall();
	a.send("/jig_bookmark_add.php", { 'id' : id }, _on_bookmarkFromListComplete, {'node':node});
	
	node.innerHTML = "Bookmarking ...";
	node.className = "js_disabled";
}

function bookmarkFromPuzzle(node, id)
{
	bookmarkFromList(node, id);
}

function _on_bookmarkFromListComplete(result, data, userdata)
{
	var code = "ERROR";
	if (result)
	{
		if (data.indexOf("OK") == 0) code = "OK";
		if (data.indexOf("ALREADY_BOOKMARKED") == 0) code = "ALREADY_BOOKMARKED";
	}
	var node = userdata.node;
	switch (code)
	{
	case "OK":
	case "ALREADY_BOOKMARKED":
		node.innerHTML = "Bookmarked";
		node.className = "js_disabled";
		break;
	case "ERROR":
		break;
	}
}


// help

var g_help = null;
var g_help_visible = false;

function showTip(e, id)
{
	hideTip();
	if (g_help == null)
	{
		g_help = document.createElement('div');
		g_help.className = 'help_box';
	}

	var pos = getElementPosition(e);
	g_help.style.left = (pos.x+pos.w+5) + 'px';
	g_help.style.top = (pos.y) + 'px';
		
	switch (id)
	{
	case "profile_filter":
		g_help.innerHTML = "Enable this if you do not want to see any puzzles that may be offensive.<br>Minors should enable this option.";
		break;
	case "comment_delete":
		g_help.innerHTML = "Delete this comment";
		g_help.style.width = 'auto';
		g_help.style.paddingTop = '4px';
		g_help.style.paddingBottom = '5px';
		break;
	}

	g_help_visible = true;
	document.body.appendChild(g_help);
}

function hideTip()
{
	if (!g_help_visible) return;
	document.body.removeChild(g_help);	
	g_help_visible = false;
}
