var rpPel = null;

function $s(){
	if(arguments.length == 1)
		return get$(arguments[0]);
	
	var elements = [];
	$c(arguments).each(function(el){elements.push(get$(el));});

	return elements;
}

function get$(el){
	if(typeof el == 'string')
		el = document.getElementById(el);
	return el;
}

function $c(array){
	var nArray = [];
	for (i=0;el=array[i];i++) nArray.push(el);
	return nArray;
}

// 文件框原本宽度
var cfm = $s(commentformid);
cfmtextarea = cfm.getElementsByTagName('textarea');
cfmtextareawidth = cfmtextarea[0].offsetWidth;

function movecfm(event,Id,dp){
	var cfm = $s(commentformid);

	if(cfm == null){
	  	alert("ERROR:\nCan't find the '"+commentformid+"' div.");
		return false;
	}

	// 修改移动后的样式
	cfmtextarea = cfm.getElementsByTagName('textarea');
	if (event != null) {
		cfmtextarea[0].style.width = $s("comment-" + Id).offsetWidth * 0.8 + "px";
	} else {
		cfmtextarea[0].style.width = "";
	}
	
	var reRootElement = $s("reroot");

	if(reRootElement == null){
		alert("Error:\nNo anchor tag called 'reroot'.");
		return false;
	}
	
	var replyId = $s("comment_reply_ID");
	
	if(replyId == null){
		alert("Error:\nNo form field called 'comment_reply_ID'.");
		return false;
	}

	var dpId = $s("comment_reply_dp");

	if(parseInt(Id)){

		if(event == null)
              event = window.event;

		rpPel = event.srcElement? event.srcElement : event.target;
		rpPel = rpPel.parentNode.parentNode;

		var OId = $s("comment-"+Id);
		if(OId == null){
			OId = rpPel;
		}

		replyId.value = Id;
		if(dpId)
			dpId.value = dp;
		reRootElement.style.display = "";

		if($s("cfmguid") == null){
			var c = document.createElement("div");
			c.id = "cfmguid";
			c.style.display = "none";
			cfm.parentNode.insertBefore(c,cfm);
		}
		cfm.parentNode.removeChild(cfm);
		OId.appendChild(cfm);
 
		if($s("comment"))
			$s("comment").focus();

		cfm.style.display = "block";
	}else{
		replyId.value = "0";
		if(dpId)
			dpId.value = "0";
		reRootElement.style.display = "none";
			
		var c = $s("cfmguid");
		if(c){
			cfm.parentNode.removeChild(cfm);
			c.parentNode.insertBefore(cfm,c);
		}

		if(parseInt(dp) && $s("comment"))
			$s("comment").focus();
	}
	return true;
}
