function action_forum_threads_get(owner_id, owner_type){
	var params = new Object();
	params.owner_type = owner_type;
	params.owner_id = owner_id;	
	params.limit = 10;
	params.page = 1;
	action('forum_threads_get',params);
}

function modules_forum_actions_threads_get_complete(response){

}

function forum_threads_get_error(response){	
}

function modules_forum_actions_thread_add(owner_id, owner_type, user_id){
	var params = new Object();
	params.owner_type = owner_type;
	params.owner_id = owner_id;
	params.user_id = user_id;
	params.title = $('#forum_thread_new').val();
	action('modules_forum_actions_thread_add', params);
}

function modules_forum_actions_thread_add_complete(response){	
	location.href = response.data.uri;
}

function modules_forum_actions_thread_add_error(response){
}

function modules_forum_actions_thread_mod(thread_id, status){
	var params = new Object();
	params.status = status;
	params.thread_id = thread_id;
	action('forum_thread_mod',params);
}

function modules_forum_actions_mod_complete(response){
}

function modules_forum_actions_mod_error(response){
}

function modules_forum_actions_post_add(owner_id, thread_id, user_id, last_sequence,new_thread, owner_type){	
	var params = new Object();
	params.owner_id = owner_id;
	params.owner_type = owner_type;
	params.thread_id = thread_id;
	params.user_id = user_id;
	if (new_thread){
		params.title = $('#forum_thread_new').val();
	}
	params.body = $('#forum_post_new').val();
	if (last_sequence)
		params.last_sequence = last_sequence;		
	
	$('#post_ajax').show();	
	$('#button_div_add_post').hide();
	$('#button_preview_post').hide();
	$('#action_add_post_errors').html('');
	action('modules_forum_actions_post_add', params);
}

function modules_forum_actions_post_add_complete(response){
	if (response.data.count > 0){
		$('#post_info').html('Pojawiły się nowe posty, gdy pisałeś(-aś) posta.');
	}

	$('#post_ajax').hide();
	if (response.data['uri']){		
		location.href = response.data['uri'];
		return true;
	}
	$('#post_preview').show();
	$('#post_preview_content').html(response.data['body']);
	$('#forum_post_new').hide();
}

function modules_forum_actions_post_add_error(response){

	$('#button_div_add_post').show();
	$('#button_preview_post').show();
	$('#post_ajax').hide();
	add_post_errors(response);
}

function modules_forum_actions_post_preview(owner_id, thread_id, user_id, last_sequence, owner_type){		
	var params = new Object();
	params.owner_id = owner_id;
	params.owner_type = owner_type;
	params.thread_id = thread_id;
	params.user_id = user_id;	
	params.body = $('#forum_post_new').val();
	if (last_sequence)
		params.last_sequence = last_sequence;
	//$('#button_preview_post').hide();
	$('#action_add_post_errors').html('');
	action('modules_forum_actions_post_preview', params);
}

function modules_forum_actions_post_preview_complete(response){
//	$('#post_content_new').hide();
	$('#post_preview_content').html(response.data['body']);
	$('#post_preview').show();
	$('#post_ajax').hide();
}

function modules_forum_actions_post_preview_error(response){
	$('#button_preview_post').show();
	$('#post_ajax').hide();
	add_post_errors(response);
}

function add_post(owner_id, thread_id, user_id, last_sequence){
	
}

function add_post_errors(response){
	var html = '';
	for (var i in response.data){
		html += '<div>';
		var no = i*1;
		switch (no){
			case 2:
				html += 'Użytkownik niezalogowany';
				break;		
			case 3:
				html += 'Brak treści posta';
				break;
			case 101:
				for (var j in response.data[101]){
					var no = j*1;						
					switch (no){
						case 1:
							html += 'Musisz podać atrybut "' + response.data[101][no]['attribute'] + '" dla elementu [' + response.data[101][no]['element'] + ']';
							break;
						case 3:
							html += 'Znacznik [' + response.data[101][no]['element'] + '] jest niedozwolony w tym miejscu';
							break;
						case 5:
							if (response.data[101][no]['minLength']){
								html += 'Długość atrybutu "' + response.data[101][no]['attribute'] + '" w znaczniku [' + response.data[101][no]['element'] + '] musi być większa niż ' + response.data[101][no]['allowed_length'] + ' znaki';
							} else if (response.data[101][no]['maxLength']){
								html += 'Długość atrybutu "' + response.data[101][no]['attribute'] + '" musi być większa niż ' + response.data[101][no]['allowed_length'] + ' znaków';
							}
							break;
						case 6:
							html += 'Nieprawidłowo zamknięte znaczniki: ';
							var tmp = new Array();
							var n=0;
							for (var k in (response.data[101][no])){								
								if (response.data[101][no][k][0] != 'body'){
									tmp[n] = response.data[101][no][k][0];
								}
								++n;
								if ( response.data[101][no][k][1] != 'body'){
									tmp[n] = response.data[101][no][k][1];
								}
								++n;
							}
							html += tmp.join(', ');
							break;
						default:
							html += 'Wystąpił błąd podczas dodawania posta';
							break;		
					}
				}
				break;					
			case 102:
				html += 'Treść posta musi być dłuższa niż 3 znaki. Znaczniki nie wliczają się do treści posta.';
				break;
			case 104:
				html += 'Usuń wulgaryzmy ' + response.data[i];
				break;
			case 105:
				html += 'Usuń puste znaczniki!';
				break;				
			default:
				if (!isNaN(no)){
					html += 'Wystąpił błąd nr ' + no + ' podczas dodawania posta';
				} else {
					html += 'Wystąpił błąd podczas dodawania posta';
				}
		}
		
		html += '</div>';
	}	

	$('#action_add_post_errors').html(html);
}


function modules_forum_actions_post_mod(thread_id, sequence){
	var params = new Object();
	params.thread_id = thread_id;	
	params.sequence = sequence;
	action('forum_post_mod', params);
}

function modules_forum_actions_post_mod_complete(response){
	//alert(var_dump(response));
}

function modules_forum_actions_post_mod_error(response){
	//alert(var_dump(response));
}

function make_citation(textarea_id){
	$('#'+textarea_id).val($('#'+textarea_id).val() + '[quote login="' + login + '"]' + selectedText + '[/quote]\n');
	$('#'+textarea_id).focus();
	return true;
}

function show_help(text, element){
	$('#'+element).html(text);
}

function add_tag(textarea, tag, attributes){
	var element = $('#' + textarea);
	var val = element.val();
	
	val += '[' + tag;
	if (attributes){
		for (var i in attributes){
			val += ' ' + attributes[i] + '=""';
		}
	}
	val += ']';
	val += '[/' + tag + ']';
	$('#' + textarea).val(val);
}
	