var justdoit = "coverimage";
var justdoitid = 0;
var url = 'ajax.php';

function doitfromadmin(id, action) {
	url = '../ajax.php';
	doit(id, action);
}

// Really do it
function doit(id, action) {
	justdoit = action;
	justdoitid = id;

	var pars = 'action=' + action + '&i=' + id;
	
	if (action == "deleteimage")
	{
		if (!window.confirm(deleteconfirm))
		{
			return false;
		}
	}
	else if (action == "postcomment")
	{
		comment = $F("comment_text");
		pars = pars + '&comment=' + encodeURIComponent(comment);
	}
	else if (action == "voteimage")
	{
		pars = pars + '&rating=5';
	}

	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");

	// error
	if (tag == 1)
	{
		alert(linkhtml);
	}
	else
	{
		if (justdoit == "deleteimage")
		{
			// É¾³ýÍ¼Æ¬Ê±£¬coidÒ²ÓÐÓÃ¡£
			if (coid == -2)
			{
				window.location.href = "c.php?c=" + caid;
			}
			else if (coid == -1)
			{
				// do nothing
			}
			else
			{
				window.location.href = "image.php?i="+imid+"&oi="+coid;
			}
		}
		else if (justdoit == "postcomment")
		{
			$FS("comment_text", "");

			new Insertion.Top('image_comment', linkhtml); 
		}
		else if (justdoit == "voteimage")
		{
			alert(linkhtml);
		}
		else if (justdoit == "recommend")
		{
			alert(linkhtml);
			$("image" + imid).style.color = "#f00";
		}
		else if (justdoit == "unrecommend")
		{
			alert(linkhtml);
			$("image" + imid).style.color = "#000";
		}
	}
}

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

// Hide all category
function init_expand(sid) {
	var oContainer = $(sid);
	var aDt = oContainer.getElementsByTagName("dt");
	for (var i = 0 ; i < aDt.length ; i++ )
	{
		aDt[i].onclick = function()
		{
			var aDd = this.parentNode.getElementsByTagName("dd");
			if(this.className =="s")
			{
				for(var j = 0 ; j < aDd.length ; j++)
				{
					aDd[j].style.display = "none";
				}
				this.className="";
			}
			else
			{
				for(var j = 0 ; j < aDd.length ; j++)
				{
					aDd[j].style.display = "block";
				}
				this.className="s";
			}
		}
	}
}

// show category by categoryid
function showcategory(categoryid)
{
	var dt = $(categoryid);
	var dd = dt.parentNode.getElementsByTagName("dd");
	for (var i = 0 ; i < dd.length ; i++ ){
		dd[i].style.display = "block";
	}
	dt.className = "s";
}