postedit_mode = false;
ajax_qei = null;

function ajax_error(resp) {
	if(resp.substr(0,1) == '-') {
		alert(resp.substr(1));
		return true;
	}
	return false;
};

function ajax_query(callback, args) {
	
	$.post('ajax.php', $.extend(args, {sid : sid}), callback);
}

function ajax_get_data(c, el) {
	e = $(el).parents('tr[@name*=p_]');
	return [e.attr('name').substr(2), e]
}

function ajax_handle_post(i, el) {
	el = $(el);
	el.find('div.ajax_post').dblclick(ajax_handle_postedit);

	if(ajax_qei && !(el.find('.ajax_qei').length)) $(ajax_qei).addClass('ajax_qei').appendTo( el.find('div.ajax_post').parent() ).css({float: 'right', cursor: 'pointer'}).click(ajax_handle_postedit);	
}

function ajax_handle_postedit(el) {
	if(postedit_mode) return;
	postedit_mode = true;
	pid = ajax_get_data('p', el.target)[0];
	msg = $('tr[@name=p_'+pid+'] div.ajax_post');
	ajax_query(function(t) { ajax_show_postedit_form(t, msg, pid) }, {mode: 'p_editform', p: pid});
}

function ajax_show_postedit_form(cont, msg, pid) {
	post_text = msg.html();

	msg[0].innerHTML = '';
	msg.html(cont);

	$('.ajax_qei').hide();
	msg.find('#send').click( function() {
		ajax_query(function(cont) {
			postedit_mode = false;
			if(ajax_error(cont)) return;
			msg.html(cont.substr(1));
			$('.ajax_qei').show();
		},  {mode: 'p_send', p: pid, text: msg.find('#message').val()});
	});

	msg.find('#preview').click( function() {
		ajax_query(function(cont) {
			if(ajax_error(cont)) return;
			msg.find('#preview_inline_box').html(cont);
		},  { mode: 'p_preview', p: pid, text: msg.find('#message').val() });
	});

	msg.find('#cancel').click( function() {
		postedit_mode = false;
		msg.html(post_text);
		$('.ajax_qei').show();
	});
}

function ajax_handle_post_forms(i, e) {
	$('input[@name=preview]').click(function(el) {
		var p = $('#preview_box');

		if(!p.length) return true;
		
		var s = $('input[@name=subject]').val();
		if(s == null) s = '';
		ajax_query(function(cont) {
			if(ajax_error(cont)) return;
			p.html(cont);
		},  {mode: 'p_preview', text: $('textarea[@name=message]').val(), subj:  s});
		return false;
	});

	$('input[@name=post]').click(function(el) {
		var a = $('#ajax_reply_anchor'), b;

		if(!a.length) return true;
		var s = $('input[@name=subject]').val();
		if(s == null) s = '';

		var fu = $('input[@name=fileupload]').val();
		if(!(fu == null || $.trim(fu) == '')) return true;

		ajax_query(function(cont) {
			if(ajax_error(cont)) return;

			if(cont.substr(0,1) == 'm') {
				b = a.prev();
				while(b.length) {
					if(b.attr('name') && b.attr('name').substr(0,2) == 'p_') {
						$('tr[@name=' + b.attr('name')+']').remove();
						break;
					}
					b = b.prev();
				}
			}
			a.before(cont.substr(1));
			$('tr[@name*=p_]').each(ajax_handle_post);
			$('textarea[@name=message]').val('');
			$('#preview_box').html('');
		},  {mode: 'p_quickreply', t: topic_id, message: $('textarea[@name=message]').val(), subject: s});

		return false;
	});
}

function ajax_handle_topics() {
	$('a[@href*=modcp.php]').each(function(i,e) {
		if(!$(e).attr('ajax')) {
			$(e).attr('ajax', '1').click(function(e) { return ajax_topic_action(e, this) });
		}
	})
}

function ajax_handle_topic_poll() {
	f = $('form[@action*=posting.php?mode=vote]');
	if(!f.length) return;
	tid = /t=([0-9]*)/.exec(f.attr('action'))[1];
	f.find('a[@href*=vote=viewresult]').click(function() {
		ajax_query(function(cont) {
			if(ajax_error(cont)) return;
			$(cont.substr(1)).insertAfter($('#vote_ballot').hide());
			
		},  {mode: 't_viewvote', t: tid});
		return false;
	})
}

$(document).ready(function(){
	$.ajaxSetup({timeout: 10000});
	$('tr[@name*=p_]').each(ajax_handle_post);
	ajax_handle_post_forms();
	ajax_handle_topics();
	ajax_handle_topic_poll();

	$("#ajax_load").ajaxStart(function(){ $(this).show() }).ajaxStop(function(){ $(this).hide() });
});

