common={};
common.bbs={};
common.bbs.post = function(objParam){
	this.url = objParam.url;
	this.postDiv = objParam.div;
	this.contentElementName = "PostContent";
	this.editElementName = "PostEdit";
	this.actionElementName = "PostAction";
	if (objParam.contentElementName != null){
		this.contentElementName = objParam.contentElementName;
	}
	if (objParam.editElementName != null){
		this.editElementName = objParam.editElementName;
	}
	if (objParam.actionElementName != null){
		this.actionElementName = objParam.actionElementName;
	}
	this.postId = this.postDiv.getAttribute("postId");

	for (var i=0;i<this.postDiv.childNodes.length;i++){
		var childNode = this.postDiv.childNodes[i];
		if (childNode.nodeType == 1 && childNode.getAttribute("name") == this.contentElementName){
			this.postContentDiv = childNode;
		}
		if (childNode.nodeType == 1 && childNode.getAttribute("name") == this.editElementName){
			this.editDiv = childNode;
		}
	}
	var actionDiv = document.getElementById(this.actionElementName + "_" + this.postId);
	if (actionDiv.nodeType == 1 && actionDiv.getAttribute("name") == this.actionElementName){
		this.actionDiv = actionDiv;
	}
}

common.bbs.post.prototype=new Object;
common.bbs.post.prototype.startEdit = function(){
	this.postContentDiv.style.display="none";
	if (this.editDiv != null) {
		this.editDiv.style.display="";
		this.textArea = document.getElementById(this.editElementName + "TextArea_"+this.postId);
	} else {
		this.editDiv = document.createElement("div");
		this.editDiv.id = this.editElementName + "_" + this.postId;
		this.editDiv.setAttribute("name",this.editElementName);
		var textArea = document.createElement("textarea");
		this.textArea = textArea;
		textArea.id = this.editElementName + "TextArea_" + this.postId;
		textArea.setAttribute("style","width: 500px; height: 100px;");
		textArea.setAttribute("rows","6");
		textArea.setAttribute("cols","50");
		this.resetEdit();
		this.editDiv.appendChild(textArea);
		var oThis = this;
		//append OK button
		var okButton = this.createButton("È·¶¨");
		okButton.onclick = function() { oThis.applyEdit(); };
		this.editDiv.appendChild(okButton);
		//append Cancel button
		var cancelButton = this.createButton("È¡Ïû");
		cancelButton.onclick = function() { oThis.cancelEdit(); };
		this.editDiv.appendChild(cancelButton);
		this.postDiv.insertBefore(this.editDiv,this.postContentDiv);
	}
	this.actionDiv.style.display="none";
	return true;
}
common.bbs.post.prototype.createButton = function(title) {
	var button = document.createElement("input");
	button.setAttribute("type","button");
	button.setAttribute("value",title);
	return button;
}
common.bbs.post.prototype.cancelEdit = function(){
	if (this.editDiv != null) {
		this.editDiv.style.display="none";
	}
	this.postContentDiv.style.display="";
	this.resetEdit();
	this.actionDiv.style.display="";
}
common.bbs.post.prototype.applyEdit = function(){
	//submit to server
	var myAjax = new Ajax.Request(
   		this.url, 
    	{   method: 'post', 
    	parameters: "postId=" + this.postId + "&content=" + encodeURIComponent(this.textArea.value), 
    	onComplete: common.bbs.post.refreshContentCallback(this)});
	if (this.editDiv != null) {
		this.editDiv.style.display="none";
	}
	this.postContentDiv.style.display="";
	this.busy();
}

common.bbs.post.refreshContentCallback = function(bbsPostObj) {
	return function(originalRequest){
		if (originalRequest.readyState==4 && originalRequest.status==200){
			bbsPostObj.postContentDiv.innerHTML = originalRequest.responseText;
			bbsPostObj.actionDiv.style.display="";
		}
	}
}

//resetting the value of the textarea.
common.bbs.post.prototype.resetEdit = function() {
	this.textArea.value = "";
	var children = this.postContentDiv.childNodes;
	for (var i=0;i<children.length;i++) {
		if (children[i].nodeType == 3) { // text node
			this.textArea.value += children[i].nodeValue;
			if (i != children.length-1 ) {
				this.textArea.value += "\n";
			}
		}
	}
}

common.bbs.post.prototype.busy = function () {
	this.postContentDiv.innerHTML = "<red>Processing...</red>";
}