var CmtEditor = function() {

	var registerDiv = new Array();
	
	var _private = {
	    
	    showForm : function(Type, DivId, TimeId, CyId, PostId, Seq, Width, Height, Data, OrigData) {
	    	
	    	var content = "";
	    	var ukey = "ukey" + CyId+PostId+Seq;
	    	var func_save = Type + "_save";
	    	
	    	content  = "<span id='cmtDivId"+ukey+"' style='display:block;width:"+Width+"px;'>";
	    	content += "<textarea name='cmtContent' id='comment"+ukey+"' style='display:block;float:left;width:"+Width+"px;height:"+Height+"px;'>"+Data+"</textarea>";
	    	content += "<input type='hidden' id='type"+ukey+"' value='"+Type+"' />";
	    	content += "<input type='hidden' id='divid"+ukey+"' value='"+DivId+"' />";
	    	content += "<input type='hidden' id='timeid"+ukey+"' value='"+TimeId+"' />";
	    	content += "<input type='hidden' id='cyid"+ukey+"' value='"+CyId+"' />";
	    	content += "<input type='hidden' id='postid"+ukey+"' value='"+PostId+"' />";
	    	content += "<input type='hidden' id='seq"+ukey+"' value='"+Seq+"' />";
			content += "<span style='display:block; float:right;' id='btn"+ukey+"'><a href='javascript:void(0)' onclick=\"CmtEditor.cancel('"+ukey+"')\" alt='Cancel'>Cancel</a> | <a href='javascript:void(0)' onclick=\"CmtEditor.save('"+ukey+"')\" alt='Save'>Save</a></span>";
	    	content += "</span>";
	    	content += "<span id='cmtDivOrigData"+ukey+"' style='display:none;'>"+OrigData+"</span>";

	    	document.getElementById(DivId).innerHTML = content;
	    },
	    
	    showContent : function(DivId,TimeId,data) {
	    	
	    	var Index = data.indexOf('|');
	    	var DateStamp = data.substr(0,Index);
	    	var Content = data.substr(Index+1);
	    	
	    	document.getElementById(DivId).innerHTML = Content;
	    	document.getElementById(TimeId).innerHTML = DateStamp;
	    	
	    	_private.unregister(DivId);
	    	
	    },
	    
	    
	    isRegister : function(Divid) {	
	    	for(i=0; i < registerDiv.length; i++) {
	    		if ( registerDiv[i] == Divid ) {
	    			return true;
	    		}
 	    	}
	    	return false;
	    },
	    
	    register : function(DivId) {
	    	if ( this.isRegister(DivId) ) {
	    		return false;
	    	} else {
				registerDiv[registerDiv.length] = DivId;
	    		return true;
	    	}
	    },
	    
	    unregister : function(DivId) {
	    	if ( this.isRegister(DivId) ) {
	    		var tmpRegister = new Array();
	    		for(i=0; i < registerDiv.length; i++) {
	    			if ( registerDiv[i] != DivId ) {
	    				tmpRegister[tmpRegister.length] = registerDiv[i];
	    			}
	    		}
	    		registerDiv = tmpRegister;
	    	}
	    }
	    
	};

	var _public = {
		
		// for guestbook only
		guestbook : function(DivId,TimeId,CyId,PostId) {

			if ( _private.register(DivId) == false ) {
				return;
			}
			
			var OrigData = document.getElementById(DivId).innerHTML;			
			$jq.ajax({
				type: "POST",
				url: "/main/comment_data.php",
				cache: false,
				data: "type=guestbook&cyid=" + CyId + "&postid=" + PostId,
				success: function(data){
					_private.showForm('guestbook',DivId,TimeId,CyId,PostId,'',460,50,data, OrigData);
				},
				error: function() {}
			});
		
		},
		
		// for guestbook in index page only
		guestbook_index : function(DivId,TimeId,CyId,PostId) {

			if ( _private.register(DivId) == false ) {
				return;
			}
			
			var OrigData = document.getElementById(DivId).innerHTML;			
			$jq.ajax({
				type: "POST",
				url: "/main/comment_data.php",
				cache: false,
				data: "type=guestbook&cyid=" + CyId + "&postid=" + PostId,
				success: function(data){
					_private.showForm('guestbook',DivId,TimeId,CyId,PostId,'',300,50,data, OrigData);
				},
				error: function() {}
			});
		
		},
		
		// for all profile's comments
		profile : function(DivId,TimeId,CyId,PostId,Seq) {

			if ( _private.register(DivId) == false ) {
				return;
			}

			var OrigData = document.getElementById(DivId).innerHTML;
			$jq.ajax({
				type: "POST",
				url: "/main/comment_data.php",
				cache: false,
				data: "type=profile&cyid=" + CyId + "&postid=" + PostId + "&seq=" + Seq,
				success: function(data){
					_private.showForm('profile',DivId,TimeId,CyId,PostId,Seq,460,50,data, OrigData);
				},
				error: function() {}
			});
			
		},
		
		// for all profile's comments
		profile_blog : function(DivId,TimeId,CyId,PostId,Seq) {

			if ( _private.register(DivId) == false ) {
				return;
			}

			var OrigData = document.getElementById(DivId).innerHTML;
			$jq.ajax({
				type: "POST",
				url: "/main/comment_data.php",
				cache: false,
				data: "type=profile&cyid=" + CyId + "&postid=" + PostId + "&seq=" + Seq,
				success: function(data){
					_private.showForm('profile',DivId,TimeId,CyId,PostId,Seq,400,50,data, OrigData);
				},
				error: function() {}
			});
			
		},
		
		save : function(ukey) {

			var DivId 	= document.getElementById('divid'+ukey).value;
			var TimeId 	= document.getElementById('timeid'+ukey).value;
			
			var Type 	= document.getElementById('type'+ukey).value;
			var CyId 	= document.getElementById('cyid'+ukey).value;
			var PostId 	= document.getElementById('postid'+ukey).value;
			var Seq 	= document.getElementById('seq'+ukey).value;
			var Comment	= document.getElementById('comment'+ukey).value;

			// validate
			if(FilterPattern('HAN',Comment)){
				return;
			}
			if(wordFilter(Comment, 'T')){
				return;
			}
			if(wordFilter(Comment, 'B')){
				return;
			}	
	
			$jq("#btn"+ukey).fadeOut("fast");
	
			$jq.ajax({
				type: "POST",
				url: "/main/comment_data_update_proc.php",
				cache: false,
				data: "type=" + Type + "&cyid=" + CyId + "&postid=" + PostId + "&seq=" + Seq + "&comment=" + Comment,
				success: function(data){
					_private.showContent(DivId,TimeId,data);
				},
				error: function() {}
			});
		
		},
		
		// cancel the update
	    cancel : function(ukey) {
	    	
	    	var OrigData = document.getElementById('cmtDivOrigData'+ukey).innerHTML;
	    	var DivId = document.getElementById('divid'+ukey).value;
	    	document.getElementById(DivId).innerHTML = OrigData;
	    	_private.unregister(DivId);
	    }		
	};
	
	return _public;
	
}();
