var justdoit = "coverimage";
var justdoitid = 0;

// Really do it
function doit(id, action) {
	if (action == "replycomment")
	{
		document.commentform.comment_parentid.value = id;
		return true;
	}

	justdoit = action;
	justdoitid = id;

	var url = 'ajax.php';
	var pars = 'action=' + action + '&i=' + id;
	
	// 移动到其他相册
	if (action == "moveimage")
	{
		if (document.mi.from_id.value == document.mi.catid.options[document.mi.catid.selectedIndex].value)
		{
			alert(lang_cannot_move_to_samecat);
			return false;
		}
		pars = pars + '&from_id=' + document.mi.from_id.value + '&c=' + document.mi.catid.options[document.mi.catid.selectedIndex].value;
	}
	// 删除图片
	else if (action == "deleteimage")
	{
		if (!window.confirm(deleteconfirm))
		{
			return false;
		}
	}
	// 删除评论
	else if (action == "deletecomment")
	{
		if (window.confirm(deletecommentconfirm))
		{
			pars = 'action=' + action + '&commentid=' + id;
		}
		else
		{
			return false;
		}
	}
	// 张贴评论
	else if (action == "postcomment")
	{
		comment = $F("comment_text");
		parentid = $F("comment_parentid");
		pars = pars + '&comment=' + encodeURIComponent(comment) + '&parentid=' + parentid;
	}
	// 对照片评分
	else if (action == "rateimage")
	{
		//
	}

	var myAjax = new Ajax.Request(
						url,
						{method: 'get', parameters: pars, onComplete: showEditImageResponse}
						);

}

function showEditImageResponse(originalRequest)
{
	var linkhtml = getXmlData(originalRequest, "linkhtml");
	var tag = getXmlData(originalRequest, "tag");
	var imid = getXmlData(originalRequest, "imageid");
	var caid = getXmlData(originalRequest, "categoryid");
	var coid = getXmlData(originalRequest, "commentid");
	var pid = getXmlData(originalRequest, "parentid");

	// 除张贴、删除评论成功外，其他操作均显示提示信息
	if (tag == 1 || (!(justdoit == "postcomment" || justdoit == "deletecomment") && tag == 0))
	{
		alert(linkhtml);
	}

	// No error
	if (tag == 0)
	{
		if (justdoit == "moveimage")
		{
			document.mi.from_id.value = document.mi.catid.options[document.mi.catid.selectedIndex].value;
		}
		else if (justdoit == "deletecomment")
		{
			$("comment" + coid).style.display = "none";
		}
		else if (justdoit == "deleteimage")
		{
			window.location.href = "c.php?c=" + caid;
		}
		else if (justdoit == "postcomment")
		{
			$FS("comment_parentid", 0);
			$FS("comment_text", "");

			if (pid == 0)
			{
				new Insertion.Bottom('commentbit_table', linkhtml); 
			}
			else
			{
				new Insertion.Bottom('replycomment' + pid, linkhtml); 
			}
		}
		else if (justdoit == "rateimage")
		{
			if (imid == 1)
			{
				$("iloveit").innerHTML = parseInt($("iloveit").innerHTML) + 1;
			}
		}
	}
}

function getXmlData(originalRequest, tagName)
{
	return fetch_xmldata(fetch_tags(originalRequest.responseXML, tagName)[0]);
}
