$(document).ready(function() {
	$.tubaOneTopic.loadPosts(1);
	$.tubaOneTopic.loadAllTopics(1);
	writeCookie("EDiaryEditor_RSave", "false", 1);
});

(function($){
	$.topic = {
		id:null,
		name:null,
		uid:null,
		tubaid:null
	};
	
	$.tubaOneTopic = {
		postReply:function() {
			if(document.getElementById('editArea') != null) return;
			EDiaryEditor.save();
			if($.trim($('#editor_body_textarea').val()) == '') {
				$.errortips.showErrMsgAfter('警告:不能发表空回复', '#btnPostCancel');
				return;
			}
			$('#btnPost').attr('disabled', true);
			$.mytuyaloading.show('正在发表回复');
			$.post('/newPost.htm', {tubaid:$.topic.tubaid, topicid:$.topic.id, postContent:$('#editor_body_textarea').val()}, function(data){$.tubaOneTopic.postReplyCallback(data)});
		},
		postReplyCallback:function(data) {
			var result = eval('(' + data + ')');
			if(result.status == 'success') {
				$.mytuyaloading.setMessage('发表成功~');
				$.tubaOneTopic.loadPosts(result.page);
				//$('#postContent').val('');
				EDiaryEditor.clear();
			} else {
				$.mytuyaloading.setMessage('发表回复失败...');
			}
			$.mytuyaloading.hide();
			$('#btnPost').attr('disabled', false);
		},
		loadPosts:function(page) {
			$.post('/viewOneTopicPosts.htm', {topicid:$.topic.id, currentPage:page}, function(data) {$('.mytuya_review').html(data);});
		}, 
		focusToPostContent:function(level) {
			//$('#postContent').focus().val('[回复 ' + level + '楼]\n');
			initTextEditor('[回复 ' + level + '楼]<br/>');
		},
		loadAllTopics:function(page) {
			$.post('/viewTubaTopics.htm', {tubaid:$.topic.tubaid, currentPage:page, isSide:1}, function(data) {$('.mytuya_left_narrow').html(data);});
		}
	};
})(jQuery);
function article_preview(){
	if (check_editor()){
		var editor_win = window.open('', "_blank", '');
			editor_win.document.open('text/html', 'replace');
			editor_win.opener = null 
			editor_win.document.writeln($E('editor_body_textarea').value);
			editor_win.document.close();
		}
}
function check_editor(){
	var err_msg = '请先输入文章内容';
	if ($E('editor_body_textarea').value.toLowerCase() =="" ||$E('editor_body_textarea').value ==""){ 
		alert(err_msg);
		return false;
	}
	return true;
}
function initTextEditor(content) {
	if(document.getElementById('editArea') != null)
		$('#editArea').remove();
	if(document.getElementById('editor_body_area') == null) {
		EDiaryEditor.initialize("EDiaryEditor", "postContent", true, '');
	}
	if(content != '') {
		EDiaryEditor.setValueToEditor(content);
	}
	//$E("editor_body_textarea").value = content;
	//_this.iframe.contentWindow.document.body.innerHTML = content;
	$('#editor_body_area').focus();
}