var errImg = '<img src="/static/images/icon-general-error-med.gif" align="absmiddle"> ';
var have_admin = false;
var have_moderator = false;

function toggleReply(thread_id, parent_id, forum_id) {
	var repElId = 'rep_' + parent_id;
	var repEl = $(repElId);
			
	if(repEl.innerHTML.length) {
		var mooffect = new Fx.Tween(repElId, {duration: 500, transition: Fx.Transitions.Elastic.easeOut});
		
		mooffect.start('height', haveUser ? 145 : 295, 0).chain(function() {
			repEl.set('html', '');
		});
	} else {
		var newFormHtml = '<form method="post" action="/postForumThreadReply.php">' + "\n";
		newFormHtml += '<input type="hidden" name="forum_id" value="' + forum_id + '" />' + "\n";
		newFormHtml += '<input type="hidden" name="thread_id" value="' + thread_id + '" />' + "\n";
		newFormHtml += '<input type="hidden" name="parent_id" value="' + parent_id + '" />' + "\n";
		
		order_form_value = (typeof order_form != 'undefined' ? order_form : 'default,oldest');
		
		newFormHtml += '<input type="hidden" name="order" value="' + order_form_value + '" />' + "\n";
						
		newFormHtml += '<div id="msg_rep_txt_' + parent_id + '"><span class="text_med">Your reply:</span></div>' + "\n";
		newFormHtml += '<textarea id="rep_txt_' + parent_id + '" name="comment_body" class="comment_box comment_box_innerreply"></textarea>' + "\n";
		newFormHtml += '<br />' + "\n";
		
		if(have_admin || have_moderator) {
			newFormHtml += '<div><input type="checkbox" name="has_html" value="1"> This post contains HTML code.</div>';
		}
		
		newFormHtml += '<div align="right">' + "\n";
		newFormHtml += '<input type="submit" class="btn_search btn_innerreply" value="Reply" onclick="return validateReply(' + parent_id + ');">' + "\n";
		newFormHtml += '</div>' + "\n";
		newFormHtml += '</form>' + "\n";
		
		repEl.setStyle('height', '0px');
		repEl.set('html', newFormHtml);
					
		var mooffect = new Fx.Tween(repElId, {duration: 1000, transition: Fx.Transitions.Elastic.easeOut});
		mooffect.start('height', 0, (have_admin || have_moderator) ? 165 : 145);
	}
}

function toggleReport(post_id) {
	var reportId = 'report_' + post_id;
	var reportEl = $(reportId);
	
	if(reportEl.innerHTML.length) {
		var mooffect = new Fx.Tween(reportId, {duration: 500, transition: Fx.Transitions.linear});
		
		mooffect.start('height', reportEl.getSize().size.y, 0).chain(function() {
			reportEl.set('html', '');
		});
	} else {
		var newFormHtml = '<form id="report_form_' + post_id + '" method="post" action="/postReport.php">' + "\n";
		newFormHtml += '<input type="hidden" name="post_id" value="' + post_id + '" />' + "\n";
		newFormHtml += '<div id="msg_report_class_' + post_id + '"><span class="text_med">Reason for reporting:</span></div>' + "\n";
		newFormHtml += '<select id="report_sel_' + post_id + '" name="report_class">' + "\n";
		newFormHtml += '<option value="spam">This post is spam</option>' + "\n";
		newFormHtml += '<option value="offensive">This post is too offensive</option>' + "\n";
		newFormHtml += '<option value="racist">This post is racist</option>' + "\n";
		newFormHtml += '</select>';
		newFormHtml += '<div id="msg_report_txt_' + post_id + '"><span class="text_med">Additional details:</span></div>' + "\n";
		newFormHtml += '<textarea id="report_txt_' + post_id + '" name="report_body" class="comment_box comment_box_innerreply"></textarea>' + "\n";
		newFormHtml += '<br />' + "\n";
		newFormHtml += '<div align="right">' + "\n";
		newFormHtml += '<input type="button" class="btn_search btn_innerreply" value="Report" onclick="return validateReport(' + post_id + ');">' + "\n";
		newFormHtml += '</div>' + "\n";
		newFormHtml += '</form>' + "\n";
		
		reportEl.setStyle('height', '0px');
		reportEl.set('html', newFormHtml);
	
		var mooffect = new Fx.Tween(reportId, {duration: 500, transition: Fx.Transitions.linear});
		mooffect.start('height', 0, 200);
	}
}

function validateReply(parent_id) {
	var errCount = 0;

	var repText_id = 'rep_txt_' + parent_id;
	if(!$(repText_id).value.trim().length) {
		$('msg_' + repText_id).set('html', errImg + '<span class="text_med color_pink">You do need to type a message before you can post!</span>');
		errCount += 1;
	}
		
	if(errCount) {
		return false;
	}
	
	return true;
}

function validateReport(post_id) {
	var reportId = 'report_' + post_id;
	var reportEl = $(reportId);

	$('report_form_' + post_id).send({'onComplete': function(ajresponse) {
		var jsonresponse = Json.evaluate(ajresponse);
		
		if(jsonresponse) {
			if(jsonresponse.status == 'ok') {
				reportEl.set('html', '<div align="center">This conversation has been flagged and will be reviewed by our staff of brainwashed monkeys as soon as they finish their lunch break. Thank you.</div>');
				
				var mooffect = new Fx.Tween(reportId, {duration: 500, transition: Fx.Transitions.linear});
				mooffect.start('height', 200, 50);
			} else {
				jsonresponse.errors.each(function(errEl, errIdx) {
					alert('crap - ' + errEl);
				});
			}
		}
	}});
}