var psCommentsEditor = {
	initStart: false,
	textArea: false,
	colorArea: false,
	colorDiv: false,
	listDiv: false,
	listArea: false,
	listMark: false,
	psPrefix: false,
	init: function(){
		if($('blog_doc_list_id'))
			this.psPrefix = 'blog_';
		else
			this.psPrefix = 'forum_';
	
		this.listArea = $(this.psPrefix + 'doc_list_id');
		this.textArea = $(this.psPrefix + 'doc_editor_id');
		this.colorDiv = $(this.psPrefix + 'inputColor');
		this.listDiv = $(this.psPrefix + 'inputList');
		var x = $(this.psPrefix + 'tableColors').getElementsByTagName('TD');
		for (var i=0;i<x.length;i++) {
			x[i].obj = this;
			x[i].onmouseover = function(){
				this.style.border = '1px dotted white';
			}
			x[i].onmouseout = function(){
				this.style.border = '1px solid gray';
			}
			x[i].onclick = function(){
				this.obj.bbAdd('color', this.id);
			}
		}
	},
	
	bbAdd: function(tag, arg){
		if(!this.initStart) this.init();
		this.bbClose();
		
		this.textArea.focus();
		if(arg && tag != 'size' && tag != 'color')	{
			this.textArea.value += arg;
		}
		else	{
			if(tag == 'size' || tag == 'color')	{
				str1 = '['+tag+'='+arg+']';
			}
			else	{
				str1 = '['+tag+']';
			}
			str2 = '[/'+tag+']';
			
			this.wraptag(str1, str2);
		}
		this.textArea.focus();
		return;
	},
	bbShow: function(name){
		if(!this.initStart) this.init();		
		this.bbClose(name);
		
		if(name == 'color')	{
			obj = this.colorDiv;
		}
		else	{
			obj = this.listDiv;
		}
		
		if(obj.style.display == 'block')	{
			if(this.listMark == name)
				obj.style.display = 'none';
		}
		else obj.style.display = 'block';
		
		this.listMark = name;		
		return;
	},
	wraptag: function(str1, str2){
		if(document.selection)	{
			var s = document.selection.createRange();
			if(s.text)	{
				s.text = str1 + s.text + str2;
				s.select();
			}
			else	{
				this.textArea.value = this.textArea.value + str1 + str2;
			}
			return true;
		}
		else if (typeof(this.textArea.selectionStart) == "number")	{
			if (this.textArea.selectionStart != this.textArea.selectionEnd)	{
				var start = this.textArea.selectionStart;
				var end = this.textArea.selectionEnd;
				s = this.textArea.value.substr(start,end-start);
				this.textArea.value = this.textArea.value.substr(0, start) + str1 + s + str2 + this.textArea.value.substr(end);
				
				var new_pos = end + str1.length + str2.length;
				this.textArea.setSelectionRange(new_pos, new_pos);
			}
			else	{
				this.textArea.value = this.textArea.value + str1 + str2;
			}
			this.textArea.focus();
			return true;
		}
		return false;
	},
	bbClose: function(arg){
		if(!this.initStart) this.init();
		if(arg)	{
			if(arg == 'color')	{
				obj = this.listDiv;
			}
			else	{
				obj = this.colorDiv;
				this.listMark = false;
			}
			obj.style.display = 'none';
		}
		else	{
			this.listDiv.style.display = 'none';
			this.colorDiv.style.display = 'none';
			this.listMark = false;
		}
		return;
	},
	bbList: function(){
		if(!this.initStart) this.init();
		
		if(this.listMark == 'ordered'){
			str1 = '[ollist]';
			str2 = '[/ollist]';
		}
		else	{
			str1 = '[ullist]';
			str2 = '[/ullist]';
		}

		var listData = explode("\n", this.listArea.value);
		var list = '';
		for(var i=0; i<listData.length; i++){
			listData[i] = (listData[i].length > 0) ? listData[i] : '&nbsp;';
			list += '[li]'+listData[i]+'[/li]';
		}
		
		this.textArea.focus();
		this.textArea.value += (str1 + list + str2);
		this.textArea.focus();
		
		this.listMark = '';
		this.listArea.value = '';
		this.listDiv.style.display = 'none';
		return;
	}
}
