old_theme = '';

//============= ADMIN FUNCTIONS ================
function checkAll() {
	if ($("#chkboxall").attr('checked')) {
		$("input.chkboxes").attr('checked','checked');
	}
	else {
		$("input.chkboxes").removeAttr('checked');
	}
}
function setForumStatus(obj, id) {
	showWaitWindow();
	$.post('/forum/set_status.html',{id:id,status:obj.value},function(){reloadCurPg();});
}
function setStatusAll(value) {
	var ids = '';
	$("input.chkboxes:checked").each(function() {
		ids += ids?","+$(this).attr('unreadid'):$(this).attr('unreadid');
	});
	if (ids)
		$.post('/forum/checked_set_status.html',{ids:ids,status:value},function(){reloadCurPg();});
}
//============= END ADMIN FUNCTIONS ================

function replaceForumItem(obj, id) {
	$.post('/forum/replace_item.html',{id:id,parent_id:obj.value},function(data){location.href=data;});
}
function globalReplaceForumItem(obj, id) {
	$.post('/forum/global_replace_item.html',{id:id,parent_id:obj.value},function(data){location.href=data;});
}
function sendForumText() {
	var pid = $("#parent_id_place").val();
	var edit_id = $("#edit_id").val();
	var txt = $("#forum_text").val();
	var quote = $("#divquote div").html();
	if (quote == undefined)
		quote = '';
	else
		quote = quote.replace(/(<blockquote>)|(<\/blockquote>)|(<span class=['"]+title['"]+>)|(<\/span>)|(<[\/]{0,}\w+>)/g, repFunction);
	var theme = $("#forum_theme").val();
	var tp = $("#forum_type").val();
	var uri = $("#cur_uri").val();
	showWaitWindow();
	if ((pid != undefined) && (txt != undefined && txt != '') && (theme != undefined) && (tp != undefined)) {
		$.post('/forum/write.html',{parent_id:pid,name:theme,text:quote+txt,type:tp,id:edit_id},
			function(data) {
				closeWaitWindow();
//				alert(data);
				if (data != '')
					location.href = '/forum/_goto/'+data+'.html';
//				else
//					location.href = uri;
			});
	}
}
function editForumText(id) {
	showForumText();
	var outstr = $("#quote_text"+id).html();
	outstr = outstr.replace(/(<blockquote>)|(<\/blockquote>)|(<span class=['"]+title['"]+>)|(<\/span>)|(<[\/]{0,}\w+>)/g, repFunction);
	old_theme = $('#forum_theme').val();
	$('#forum_theme').attr('value',$('#forum_link'+id).text());
	$('#answer_place textarea').text(outstr);
	$('#edit_id').attr('value', id);
}
function writeForumText(parent_id, quote_id) {
	showForumText();
	var q_title = '';
	if (old_theme && !quote_id) {
		q_title = old_theme;
		$('#forum_theme').attr('value', old_theme);
	}
	else {
		old_theme = $('#forum_theme').val();
		if (quote_id) {
			q_title = $("#forum_link"+quote_id).text();
		}
		else {
			q_title = $("#forum_link"+parent_id).text();
		}
		$('#forum_theme').attr('value', "Re: "+q_title);
	}
	if (quote_id) {
		var outstr = $("#quote_text"+quote_id).html();
		outstr = outstr.replace(/(<blockquote>)|(<\/blockquote>)|(<span class=['"]+title['"]+>)|(<\/span>)|(<[\/]{0,}\w+>)/g, repFunction);
		$('#answer_place #divquote div').html("<blockquote>\n<span class='title'>"+q_title+"</span>\n"+outstr+"\n</blockquote>\n");
		$('#answer_place #divquote').show();
	}
	else {
		$('#answer_place #divquote div').text('');
		$('#answer_place #divquote').hide();
	}
	$('#answer_place textarea').attr('value','');
	$("#parent_id_place").attr('value',parent_id);
	$('#edit_id').attr('value', 0);
}
function hideForumText() {
	$('#answer_place').animate({height:'hide'},500).find("textarea").attr('value','');
	$('#answer_place #divquote div').text('');
	$('#answer_place #divquote').hide();
}
function showForumText() {
	$('#answer_place').animate({height:'show'},500);
}
function repFunction(data) {
	switch (data) {
		case '<blockquote>':
			return '[quote]';
		case '</blockquote>':
			return '[/quote]';
		case '<span class="title">':
		case '<span class=\'title\'>':
			return '[title]';
		case '</span>':
			return '[/title]';
		default:
			return '';
	}
}

