var actions = {
	pseudo_action: 'foobar/blablubb'
}

function action_url(name) {
	return "/kbumm/ajax/action/" + (name in actions ? actions[name] : name);
}

/* 
 * Builds an url for an ajax request.
 * 
 * @param page 		has to contain preceding slash, for example '/profil'
 * @param subPage	subPage without preceding slash, for example 'edit'
 * @param params	dict {parameter1: value1, ...}
 * 
 * @return the server root relative url, for example /kbumm/ajax/profile/edit?nick=foo
 */
function ajax_url(page, subPage, params) {
	var url = "/kbumm/ajax/" + page;
	url = url.replace(/\/\//g, "/"); //replace doubleslashes

	if (subPage) {
		url += "/" + subPage;
	}
	
	if (params) {
		var first = true;
		$.each(params, function(key, value) {
			if (value && key) {
				if (first) {
					url += "?";
					first = false;
				} else {
					url += "&";
				}
				url += encodeURIComponent(key) + "=" + encodeURIComponent(value);
			}			
		});
	}
	
	return url;
}

function getStatus() {
	var data = "";
	if(window.chatIDs !== undefined) {
		for(var i = 0; i < chatIDs.length; i++) {
			var chat_id = chatIDs[i];
			if(chat_id > 0) {
				if(data.length == 0) {
					data = "mid_"+chat_id+"="+$("#mid_"+chat_id).val();
				}
				else {
					data = data + "&mid_"+chat_id+"="+$("#mid_"+chat_id).val();
				}
			}
		}
	}
	
	if(typeof sendChatMessageActive != "undefined" && sendChatMessageActive) {
		//Currently we process a chat message
		return;
	}
	
	$.ajax({
		type: "POST",
		url: "/kbumm/ajax/content/status.php",
		data: data,
		success: function(json) {
			if (json && 'success' in json && json.success) {
				var playchatsound = false;
				// success
				//Add chats begin
				if('chats_begin' in json) {
					$("#communitychat").append(json.chats_begin);
					 $("div.chatmessages").each(function(index) {
						 $(this).scrollTop($(this)[0].scrollHeight);
					 });
					 playchatsound = true;
				}
				
				if('chats_active' in json) {
					for(var chat_id in json.chats_active) {
						appendChatMessages(chat_id, json.chats_active[chat_id], true);
						playchatsound = true;
					}
				}
				
				if('chats_status' in json) {
					for(var chat_id in json.chats_status) {
						if(json.chats_status[chat_id] != $("#onlinestatus"+chat_id).attr("class")) {
							$("#onlinestatus"+chat_id).attr("class", json.chats_status[chat_id]);
						}
					}
				}
				
				if('chats_message_ids' in json) {
					window.chatIDs = new Array();
					for(var chat_id in json.chats_message_ids) {
						chatIDs.push(chat_id);
						$("#mid_"+chat_id).val(json.chats_message_ids[chat_id]);
					}
				}				
				
				if(playchatsound) {
					soundManager.play('chat');
				}
				
				if(json.profile_count != profile_count) {
					if(json.profile_count > 0) {
						$("#profile_count").html(" ("+json.profile_count+")");
						if(json.profile_count > profile_count) {
							$("#profile_link").fadeOut().fadeIn().fadeOut().fadeIn().fadeOut().fadeIn();
							soundManager.play('friend');
						}
					}
					else {
						$("#profile_count").html("");
					}
					
					if(json.unread_doings > 0) {
						$("#unread_doings").html(" ("+json.unread_doings+")");
					}
					
					profile_count = json.profile_count;
				}
				if(json.messages_count != messages_count) {
					if(json.messages_count > 0) {
						$("#messages_count").html(" ("+json.messages_count+")");
						if(json.messages_count > messages_count) {
							$("#messages_link").fadeOut().fadeIn().fadeOut().fadeIn().fadeOut().fadeIn();
							soundManager.play('notify');
						}
					}
					else {
						$("#messages_count").html("");
					}
					messages_count = json.messages_count;
				}
				if(json.friends_count != friends_count) {
					if(json.friends_count > 0) {
						$("#friends_count").html(" ("+json.friends_count+")");
						if(json.friends_count > friends_count) {
							$("#friends_link").fadeOut().fadeIn().fadeOut().fadeIn().fadeOut().fadeIn();
						}
					}
					else {
						$("#friends_count").html("");
					}
					friends_count = json.friends_count;
				}					
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			//alert("Internal error while trying to receive status");
		}
	});
}

 /**************** CHAT FUNCTIONS BEGIN *********************/
 function beginChat(user_id) {
	 if(user_id > 0) {
		if($("#chat"+user_id).length > 0 && $("#chat"+user_id).val() > 0) {
			//Chat already exists
			$.fancybox.close();
			toggleChat($("#chat"+user_id).val(), true);
		}
		else {
			beforeAjax();
			$.ajax({
				type: "POST",
				url: "/kbumm/ajax/action/chat_create",
				data: "user_id="+user_id,
				success: function(json) {
					if (json && 'success' in json && json.success && 'html' in json) {
						// success
						$.fancybox.close();
						$("#communitychat").append(json.html);
						toggleChat(json.chat_id, true);
					}
					else if (json && 'messages' in json) {
						showMessages(json.messages);
					}				
					else {
						ajaxError(json);
					}				
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					ajaxError(errorThrown);
				}
			});
		}
	 }
 }
 
 function chatMessage(the_chat_id) {
	 if(the_chat_id > 0) {
		 var text = $("#chatmessage"+the_chat_id).val();
		 if(text.length > 0) {
			if(typeof sendChatMessageActive != "undefined" && sendChatMessageActive) {
				//Currently we process a chat message
				return;
			}
			 
			 beforeAjax();
			 sendChatMessageActive = true;
			 $.ajax({
				type: "POST",
				url: "/kbumm/ajax/action/chat_message",
				data: "chat_id="+the_chat_id+"&text="+encodeURIComponent(text),
				success: function(json) {
					if (json && 'success' in json && json.success && 'html' in json) {
						$("#chatmessage"+the_chat_id).val('');
						$("#mid_"+the_chat_id).val(json.mid);
						appendChatMessages(the_chat_id, json.html);
					}
					else if (json && 'messages' in json) {
						showMessages(json.messages);
					}				
					else {
						ajaxError(json);
					}			
					sendChatMessageActive = false;
				},
				error: function(XMLHttpRequest, textStatus, errorThrown){
					ajaxError(errorThrown);
					sendChatMessageActive = false;
				}
			});			 
		 }
	 }
 }
 
 function appendChatMessages(chat_id, html, forceShow) {
	 $("#chatmessages"+chat_id).append(html);
	 if(forceShow != undefined && forceShow) {
		toggleChat(chat_id, true); 
	 }
	 else {
		 $("#chatcontainer"+chat_id).show();
		 $("#chatmessages"+chat_id).scrollTop($("#chatmessages"+chat_id)[0].scrollHeight);
		 //$("#chatmessage"+chat_id).focus();
	 }
 }
 
 function closeChat(chat_id) {
	 if(chat_id > 0) {
		 
		beforeAjax();
		$.ajax({
			type: "POST",
			url: "/kbumm/ajax/action/chat_close",
			data: "chat_id="+chat_id,
			success: function(json) {
				if (json && 'success' in json && json.success) {
					$("#chat"+chat_id).hide();
				}
				else if (json && 'messages' in json) {
					showMessages(json.messages);
				}				
				else {
					ajaxError(json);
				}				
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				ajaxError(errorThrown);
			}
		});
	}
 }
 
 function toggleChat(chat_id, forceShow) {
	 if(chat_id > 0) {
		if((forceShow != undefined && forceShow) || $("#chatcontainer"+chat_id).css("display") == "none") {
			$("#chatcontainer"+chat_id).show();
			$("#chatmessages"+chat_id).scrollTop($("#chatmessages"+chat_id)[0].scrollHeight);
			$("#chatmessage"+chat_id).focus();
			var minimized = "FALSE";
		}
		else {
			$("#chatcontainer"+chat_id).hide();
			var minimized = "TRUE";
		}
		
		beforeAjax();
		$.ajax({
			type: "POST",
			url: "/kbumm/ajax/action/chat_toggle",
			data: "chat_id="+chat_id+"&minimized="+minimized,
			success: function(json) {
				if (json && 'success' in json && json.success) {
					//doNothing
				}
				else if (json && 'messages' in json) {
					showMessages(json.messages);
				}				
				else {
					ajaxError(json);
				}				
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				ajaxError(errorThrown);
			}
		});
	 }
 }
 
 /**************** CHAT FUNCTIONS END *********************/
 
 function getAjaxContent(url, content_target, display_loading) {
	beforeAjax();
	if(display_loading != undefined && display_loading) {
		$("#"+content_target).html('<div class="loading_large"></div>');
	}
	$.ajax({
		type: "GET",
		url: "/kbumm/ajax/"+url,
		success: function(json) {
			if (json && 'success' in json && json.success && 'html' in json) {
				// success
				$("#"+content_target).html(json.html);
				kbummInit();
			}
			else if (json && 'messages' in json) {
				showMessages(json.messages);
			}
			else {
				ajaxError(json);
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			ajaxError(errorThrown);
		}
	});
}

function profileContent(url, id, start) {
	$("#profile_navigation").children().addClass("greybutton");
	$("#button_"+url).removeClass("greybutton");
	if(url == "user_doings") {
		$("#unread_doings").html('');
	}
	if(id == undefined) {
		getAjaxContent(url, "profile_content", true);	
	}
	else if(id != undefined) {
		if(start != undefined) {
			getAjaxContent(url+"?id="+id+"&start="+start, "profile_content", true);
		}
		else {
			getAjaxContent(url+"?id="+id, "profile_content", true);
		}
	}
}

function showTab(tabstrip_id, tab_id) {
	//Do we really need those params here???
	/*
	var params = {
	 	'nick': $(document).getUrlParam('nick'),
	 	'id': $(document).getUrlParam('id'),
	 	'tid': $(document).getUrlParam('tid')
	};
	*/
	var params = {};
	var url = ajax_url(
		$('#a_'+tabstrip_id+'_'+tab_id).attr('href'),
		null,
		params
	);
	//if ($('#'+tabstrip_id+'_'+tab_id+'_content').html().length < 1) {
		$('#span_'+tabstrip_id+'_'+tab_id).addClass("loading");
		beforeAjax();
		$.ajax({
			type: "GET",
			url: url,
			success: function(result) {
				var json = result;
				if (json && 'html' in json) {
					// success
					$("#"+tabstrip_id+"_"+tab_id+'_content').html(json.html);
					$('#span_'+tabstrip_id+'_'+tab_id).removeClass("loading");
					kbummInit();
					//initFancybox();
					//initTablesorter();
					
					// page specific extensions
					/*
					if (tabstrip_id == 'profile' && tab_id == 'edit') {
						$("select[onclick]").each(function () {
							saveSelected(this);
						});
					}
					*/
					displayTab(tabstrip_id, tab_id);
				} else if (json && 'messages' in json) {
					$('#span_'+tabstrip_id+'_'+tab_id).removeClass("loading");
					showMessages(json.messages);
				} else {
					$('#span_'+tabstrip_id+'_'+tab_id).removeClass("loading");
					ajaxError(json);
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				$('#span_'+tabstrip_id+'_'+tab_id).removeClass("loading");
				ajaxError(errorThrown);
			}
		});
	/*
	} else {
		displayTab(tabstrip_id, tab_id);
	}
	*/
		
	//Return false, because browsers with activated javascript won't follow the href link
	return false;
}


function displayTab(tabstrip_id, tab_id) {
	if (tabstrip_id == 'messages' && tab_id == 'new') {
		// new PM tab selected, do nothing		
		return;
	}
	
	if($("#li_"+tabstrip_id+"_"+tab_id).length < 1) {
		//Tab does not exist
		return;
	}
	
	var tabs = $("#"+tabstrip_id).children();
	tabs.removeClass("current");
	$("#li_"+tabstrip_id+"_"+tab_id).addClass("current");
	
	var divtabs = $("#div_"+tabstrip_id).children();
	divtabs.hide();
	
	$("#"+tabstrip_id+"_"+tab_id).show();
	
	/*
	if(tabstrip_id == "mediaTab" && tab_id == "upload") {
		setSWFUploadVars();		
	}
	*/
}

function initFancybox() {	
	$('.fb').fancybox({
		'hideOnContentClick': false
	});
}

function ajaxError(error) {
	if(typeof error  == 'object') {
		if(!error || !'messages' in error) {
			showMessages('<p class="msg_error">AJAX-Fehler: Bitte versuche es später noch einmal.</div>');	
		}
	}
	else {
		showMessages('<p class="msg_error">AJAX-Fehler: Bitte versuche es später noch einmal.</div>');
		$("#messagecontainer").html(error);
	}
}

function showFormButtonLoading(id) {
	$("#"+id+"_button").hide();
	$("#"+id+"_loading").show();
}

function deleteMessage(id) {
	 beforeAjax();
	 $.ajax({
		type: "POST",
		url: "/kbumm/ajax/action/message_delete",
		data: "message_id="+id,
		success: function(json) {
			if (json && 'success' in json && json.success) {
				var tr = $("#message" + id);
				tr.children().each(function() {  
					$(this).wrapInner("<div />").children("div").slideUp(function() {tr.remove();})  
				});  	
			}
			else {
				ajaxError(json);
			}			
			if (json && 'messages' in json) {
				showMessages(json.messages);
			}				
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			ajaxError(errorThrown);
		}
	});
}

function restoreMessage(id) {
	 beforeAjax();
	 $.ajax({
		type: "POST",
		url: "/kbumm/ajax/action/message_restore",
		data: "message_id="+id,
		success: function(json) {
			if (json && 'success' in json && json.success) {
				var tr = $("#message" + id);
				tr.children().each(function() {  
					$(this).wrapInner("<div />").children("div").slideUp(function() {tr.remove();})  
				});  	
			}
			else {
				ajaxError(json);
			}
			if (json && 'messages' in json) {
				showMessages(json.messages);
			}				
				
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			ajaxError(errorThrown);
		}
	});
}

function showSendedMessage(siteMessages, message_id, message_html) {
	$.fancybox.close();
	if(siteMessages.length > 0) {
		showMessages(siteMessages);
	}
	
	if(message_html.length > 0 && $('#messagestream').length == 1) {
		//display message html
		$('#messagestream').prepend(message_html);
		var tr = $("#message" + message_id);
		tr.children().each(function() {  
			$(this).wrapInner("<div />").children("div").hide().slideDown()  
		}); 		
	}
}

function addFriend(user_id) {
	 beforeAjax();
	 var id = "addFriend"+user_id;
	 $("#"+id).hide();
	 displayLoader(id);
	 $.ajax({
		type: "POST",
		url: "/kbumm/ajax/action/request_friendship",
		data: "user_id="+user_id+"&privilege="+$("#privilege_addfriend_"+user_id).val(),
		success: function(json) {
			if (json && 'success' in json && json.success) {
				removeLoader(id);
				$("#"+id).before(json.html);
				$("#"+id).remove();
				$("#waitingConfirm"+user_id).fadeIn();
			}
			else {
				ajaxError(json);
				removeLoader(id);
				$("#"+id).show();
			}
			if (json && 'messages' in json) {
				showMessages(json.messages);
			}
				
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			ajaxError(errorThrown);
			removeLoader(id);
			$("#"+id).show();
		}
	});
}

function confirmFriend(user_id) {
	confirmIgnoreFriend(user_id, "confirm_friendship");
}

function ignoreFriend(user_id) {
	confirmIgnoreFriend(user_id, "ignore_friendship");
}

function confirmIgnoreFriend(user_id, command) {
	 beforeAjax();
	 var id = "confirmFriend"+user_id;
	 var id2 = "ignoreFriend"+user_id;
	 $("#"+id).hide();
	 $("#"+id2).hide();
	 displayLoader(id);
	 $.ajax({
		type: "POST",
		url: "/kbumm/ajax/action/"+command,
		data: "user_id="+user_id+"&privilege="+$("#privilege_confirmfriend_"+user_id).val(),
		success: function(json) {
			if (json && 'success' in json && json.success) {
				removeLoader(id);
				var tr = "friends_to_confirm_table_"+user_id;
				trDelete(tr);
				var confirm_count = parseInt($("#friends_to_confirm_count").html());
				if(!isNaN(confirm_count)) {
					var to_confirm = confirm_count - 1;
					if(to_confirm > 0) {
						$("#friends_to_confirm_count").html(""+to_confirm);
					}
					else {
						$("#friends_to_confirm").slideUp('slow', function() {
						    $("#friends_to_confirm").remove();
						  });						
					}
				}
			}
			else {
				ajaxError(json);
				removeLoader(id);
				$("#"+id).show();
				$("#"+id2).show();
			}
			if (json && 'messages' in json) {
				showMessages(json.messages);
			}
				
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			ajaxError(errorThrown);
			removeLoader(id);
			$("#"+id).show();
			$("#"+id2).show();
		}
	});
}

function updateFriendContext(user_id) {
	 beforeAjax();
	 var id = "privilege_addfriend_"+user_id;
	 $("#"+id).hide();
	 displayLoader(id);
	 $.ajax({
		type: "POST",
		url: "/kbumm/ajax/action/update_friend_context",
		data: "user_id="+user_id+"&privilege="+$("#privilege_addfriend_"+user_id).val(),
		success: function(json) {
			if (json && 'success' in json && json.success) {
				removeLoader(id);
				$("#"+id).show();
			}
			else {
				ajaxError(json);
				removeLoader(id);
				$("#"+id).show();
			}
			if (json && 'messages' in json) {
				showMessages(json.messages);
			}
				
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			ajaxError(errorThrown);
			removeLoader(id);
			$("#"+id).show();
		}
	});
}

function quitFriendship(user_id) {
	 beforeAjax();
	 $.ajax({
		type: "POST",
		url: "/kbumm/ajax/action/quit_friendship",
		data: "user_id="+user_id,
		success: function(json) {
			if (json && 'success' in json && json.success) {
				window.setTimeout("location.reload()", 1000);
			}
			else {
				ajaxError(json);
			}
			if (json && 'messages' in json) {
				showMessages(json.messages);
			}
				
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			ajaxError(errorThrown);
		}
	});	
}

function ignoreUser(user_id) {
	 beforeAjax();
	 $.ajax({
		type: "POST",
		url: "/kbumm/ajax/action/ignore_user",
		data: "user_id="+user_id,
		success: function(json) {
			if (json && 'success' in json && json.success) {
				window.setTimeout("location.reload()", 1000);
			}
			else {
				ajaxError(json);
			}
			if (json && 'messages' in json) {
				showMessages(json.messages);
			}
				
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			ajaxError(errorThrown);
		}
	});	
}

function groupBecomeMember(group_id) {
	updateMembership("group_become_member", "group", group_id);
}

function groupQuitMembership(group_id) {
	updateMembership("group_quit_membership", "group", group_id);
}

function eventBecomeMember(event_id) {
	updateMembership("event_become_member", "event", event_id);
}

function eventQuitMembership(event_id) {
	updateMembership("event_quit_membership", "event", event_id);
}

function updateMembership(id, type, item_id) {
	 beforeAjax();
	 $("#"+id).hide();
	 displayLoader(id);
	 $.ajax({
		type: "POST",
		url: "/kbumm/ajax/action/"+id,
		data: type+"_id="+item_id,
		success: function(json) {
			if (json && 'success' in json && json.success) {
				removeLoader(id);
				if('html' in json) {
					$("#template_content").html(json.html);
					kbummInit();
				}
			}
			else {
				ajaxError(json);
				removeLoader(id);
				$("#"+id).show();
			}
			if (json && 'messages' in json) {
				showMessages(json.messages);
			}
				
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			ajaxError(errorThrown);
			removeLoader(id);
			$("#"+id).show();
		}
	});
}

function groupRevokeAdmin(group_id, user_id, trDelete) {
	changeProperty("group", group_id, user_id, trDelete, "revoke_admin");
}

function groupGrantAdmin(group_id, user_id, trDelete) {
	changeProperty("group", group_id, user_id, trDelete, "grant_admin");
}

function groupUnbannUser(group_id, user_id, trDelete) {
	changeProperty("group", group_id, user_id, trDelete, "unbann_user");
}

function groupBannUser(group_id, user_id, trDelete) {
	changeProperty("group", group_id, user_id, trDelete, "bann_user");
}

function eventRevokeAdmin(event_id, user_id, trDelete) {
	changeProperty("event", event_id, user_id, trDelete, "revoke_admin");
}

function eventGrantAdmin(event_id, user_id, trDelete) {
	changeProperty("event", event_id, user_id, trDelete, "grant_admin");
}

function eventUnbannUser(event_id, user_id, trDelete) {
	changeProperty("event", event_id, user_id, trDelete, "unbann_user");
}

function eventBannUser(event_id, user_id, trDelete) {
	changeProperty("event", event_id, user_id, trDelete, "bann_user");
}

function changeProperty(type, item_id, user_id, doTrDelete, command) {
	 beforeAjax();
	 if(command == "revoke_admin") {
		 var id = type+"_revoke_admin" + user_id;
		 var id2 = type+"_grant_admin" + user_id;
	 }
	 else if(command == "grant_admin") {
		 var id2 = type+"_revoke_admin" + user_id;
		 var id = type+"_grant_admin" + user_id;
	 }
	 else if(command == "unbann_user") {
		 var id = type+"_unbann_user" + user_id;
		 var id2 = type+"_bann_user" + user_id;
	 }	 
	 else {
		 var id2 = type+"_unbann_user" + user_id;
		 var id = type+"_bann_user" + user_id;
	 }	 
	 
	 $("#"+id).hide();
	 displayLoader(id);
	 $.ajax({
		type: "POST",
		url: "/kbumm/ajax/action/"+type+"_"+command,
		data: type+"_id="+item_id+"&user_id="+user_id,
		success: function(json) {
			if (json && 'success' in json && json.success) {
				removeLoader(id);
				if(doTrDelete) {
					trDelete("dialog_users_"+user_id);
				}
				else if('html' in json) {
					$("#"+id).fadeOut();
					$("#"+id).before(json.html);
					$("#"+id2).hide();
					$("#"+id).remove();
					$("#"+id2).fadeIn();
				}
			}
			else {
				ajaxError(json);
				removeLoader(id);
				$("#"+id).show();
			}
			if (json && 'messages' in json) {
				showMessages(json.messages);
			}
				
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			ajaxError(errorThrown);
			removeLoader(id);
			$("#"+id).show();
		}
	});	
}

function eventInviteUser(event_id, user_id) {
	inviteUser("event", event_id, user_id);
}

function groupInviteUser(group_id, user_id) {
	inviteUser("group", group_id, user_id);
}

function inviteUser(type, item_id, user_id) {
	 beforeAjax();
	 var id = type+"_invite_user" + user_id;
	 
	 $("#"+id).hide();
	 displayLoader(id);
	 $.ajax({
		type: "POST",
		url: "/kbumm/ajax/action/"+type+"_invite_user",
		data: type+"_id="+item_id+"&user_id="+user_id,
		success: function(json) {
			if (json && 'success' in json && json.success) {
				removeLoader(id);
				if('html' in json) {
					$("#"+id).fadeOut();
					$("#"+id).before(json.html);
					$("#"+id).remove();
				}
			}
			else {
				ajaxError(json);
				removeLoader(id);
				$("#"+id).show();
			}
			if (json && 'messages' in json) {
				showMessages(json.messages);
			}
				
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			ajaxError(errorThrown);
			removeLoader(id);
			$("#"+id).show();
		}
	});	
}

function getThreadList(group_id, start) {
	beforeAjax();
	displayLoadingImage(".pagination_wrapper");
	 $.ajax({
			type: "POST",
			url: "/kbumm/ajax/action/group_get_thread_list",
			data: "group_id="+group_id+"&start="+start,
			success: function(json) {
				if (json && 'success' in json && json.success) {
					if('html' in json) {
						$("#thread_list").html(json.html);
					}
				}
				else {
					ajaxError(json);
				}
				if (json && 'messages' in json) {
					showMessages(json.messages);
				}
					
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				ajaxError(errorThrown);
			}
		});	
}

function groupDeletePosting(pid) {
	if(confirm("Willst du dieses Posting wirklich löschen?")) {
		beforeAjax();
		 $.ajax({
				type: "POST",
				url: "/kbumm/ajax/action/group_delete_posting",
				data: "pid="+pid,
				success: function(json) {
					if (json && 'success' in json && json.success) {
						trDelete("posting"+pid);
					}
					else {
						ajaxError(json);
					}
					if (json && 'messages' in json) {
						showMessages(json.messages);
					}
						
				},
				error: function(XMLHttpRequest, textStatus, errorThrown){
					ajaxError(errorThrown);
				}
			});			
	}
}

function groupDeleteThread(tid) {
	if(confirm("Willst du den Thread wirklich löschen?")) {
		beforeAjax();
		 $.ajax({
				type: "POST",
				url: "/kbumm/ajax/action/group_delete_thread",
				data: "tid="+tid,
				success: function(json) {
					if (json && 'success' in json && json.success) {
						trDelete("thread"+tid);
					}
					else {
						ajaxError(json);
					}
					if (json && 'messages' in json) {
						showMessages(json.messages);
					}
						
				},
				error: function(XMLHttpRequest, textStatus, errorThrown){
					ajaxError(errorThrown);
				}
			});			
	}
}

function groupConfirmMembership(group_id, user_id) {
	confirmIgnoreMembership("group", group_id, user_id, "group_confirm_membership", "groupmembers");
}

function groupIgnoreMembership(group_id, user_id) {
	confirmIgnoreMembership("group", group_id, user_id, "group_ignore_membership", "groupmembers");
}

function groupConfirmInvitation(group_id, user_id) {
	confirmIgnoreMembership("group", group_id, user_id, "group_confirm_membership", "groupinvitations");
}

function groupIgnoreInvitation(group_id, user_id) {
	confirmIgnoreMembership("group", group_id, user_id, "group_ignore_membership", "groupinvitations");
}

function eventConfirmMembership(event_id, user_id) {
	confirmIgnoreMembership("event", event_id, user_id, "event_confirm_membership", "eventmembers");
}

function eventIgnoreMembership(event_id, user_id) {
	confirmIgnoreMembership("event", event_id, user_id, "event_ignore_membership", "eventmembers");
}

function eventConfirmInvitation(event_id, user_id) {
	confirmIgnoreMembership("event", event_id, user_id, "event_confirm_membership", "eventinvitations");
}

function eventIgnoreInvitation(event_id, user_id) {
	confirmIgnoreMembership("event", event_id, user_id, "event_ignore_membership", "eventinvitations");
}

function confirmIgnoreMembership(type, item_id, user_id, command, id_prefix) {
	 beforeAjax();
	 var id = id_prefix+"confirm"+item_id+"_"+user_id;
	 var id2 = id_prefix+"ignore"+item_id+"_"+user_id;
	 $("#"+id).hide();
	 $("#"+id2).hide();
	 displayLoader(id);
	 $.ajax({
		type: "POST",
		url: "/kbumm/ajax/action/"+command,
		data: type+"_id="+item_id+"&user_id="+user_id,
		success: function(json) {
			if (json && 'success' in json && json.success) {
				removeLoader(id);
				var tr = id_prefix+"_to_confirm_table_"+item_id+"_"+user_id;
				trDelete(tr);
				var confirm_count = parseInt($("#"+id_prefix+"_to_confirm_count").html());
				if(!isNaN(confirm_count)) {
					var to_confirm = confirm_count - 1;
					if(to_confirm > 0) {
						$("#"+id_prefix+"_to_confirm_count").html(""+to_confirm);
					}
					else {
						$("#"+id_prefix+"_to_confirm").slideUp('slow', function() {
						    $("#"+id_prefix+"_to_confirm").remove();
						  });
					}
				}
			}
			else {
				ajaxError(json);
				removeLoader(id);
				$("#"+id).show();
				$("#"+id2).show();
			}
			if (json && 'messages' in json) {
				showMessages(json.messages);
			}
				
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			ajaxError(errorThrown);
			removeLoader(id);
			$("#"+id).show();
			$("#"+id2).show();
		}
	});
}

function becomeFan(type, type_id) {
	var id = "become_fan";
	 beforeAjax();
	 $("#"+id).hide();
	 displayLoader(id);
	 $.ajax({
		type: "POST",
		url: "/kbumm/ajax/action/"+id,
		data: "type="+type+"&type_id="+type_id,
		success: function(json) {
			if (json && 'success' in json && json.success) {
				removeLoader(id);
				if('html' in json) {
					$("#fans").html(json.html);
				}
				if ('messages' in json) {
					showMessages(json.messages);
				}
			}
			else {
				ajaxError(json);
				removeLoader(id);
				$("#"+id).show();
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			ajaxError(errorThrown);
			removeLoader(id);
			$("#"+id).show();
		}
	});
}


function getLink(id) {
	 beforeAjax();
	 $("#"+id+"_form").hide();
	 displayLoader(id+"_form", "float: none");
	 $.ajax({
		type: "POST",
		url: "/kbumm/ajax/action/get_link",
		data: "url="+$("#"+id+"_url").val(),
		success: function(json) {
			if (json && 'success' in json && json.success) {
				removeLoader(id+"_form");
				$("#"+id+"_content").html(json.html).slideDown();
			}
			else {
				ajaxError(json);
				removeLoader(id+"_form");
				$("#"+id+"_form").show();
			}
			if (json && 'messages' in json) {
				showMessages(json.messages);
			}
				
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			ajaxError(errorThrown);
			removeLoader(id+"_form");
			$("#"+id+"_form").show();
		}
	});
}

function previousImage(id) {
	var count = $("#"+id+"_images").children().size();
	var current_image = parseInt($("#"+id+"_image_nr").html());
	if(isNaN(current_image)) {
		current_image = 1;
	}
	else if(current_image > 1) {
		current_image--;
	}
	else {
		return;
	}
	
	if(current_image == 1 && !$("#"+id+"_image_previous").hasClass("inactive")) {
		$("#"+id+"_image_previous").addClass("inactive");
	}
	else if(current_image > 1 && $("#"+id+"_image_previous").hasClass("inactive")) {
		$("#"+id+"_image_previous").removeClass("inactive");
	}

	if(current_image == count && !$("#"+id+"_image_next").hasClass("inactive")) {
		$("#"+id+"_image_next").addClass("inactive");
	}
	else if(current_image < count && $("#"+id+"_image_next").hasClass("inactive")) {
		$("#"+id+"_image_next").removeClass("inactive");
	}
	
	$("#"+id+"_images").children().hide();
	$("#"+id+"_image"+current_image).show();
	$("#link_image").val($("#"+id+"_image"+current_image).attr("src"));
	$("#"+id+"_image_nr").html(""+current_image);
}

function nextImage(id) {
	var count = $("#"+id+"_images").children().size();
	var current_image = parseInt($("#"+id+"_image_nr").html());
	if(isNaN(current_image)) {
		current_image = 1;
	}
	else if(current_image+1 < count) {
		current_image++;
	}
	else {
		return;
	}
	
	if(current_image == 1 && !$("#"+id+"_image_previous").hasClass("inactive")) {
		$("#"+id+"_image_previous").addClass("inactive");
	}
	else if(current_image > 1 && $("#"+id+"_image_previous").hasClass("inactive")) {
		$("#"+id+"_image_previous").removeClass("inactive");
	}

	if(current_image+1 >= count && !$("#"+id+"_image_next").hasClass("inactive")) {
		$("#"+id+"_image_next").addClass("inactive");
	}
	else if(current_image < count && $("#"+id+"_image_next").hasClass("inactive")) {
		$("#"+id+"_image_next").removeClass("inactive");
	}
	
	$("#"+id+"_images").children().hide();
	$("#"+id+"_image"+current_image).show();
	$("#link_image").val($("#"+id+"_image"+current_image).attr("src"));
	$("#"+id+"_image_nr").html(""+current_image);
}

function favorite_image(command, image_id, imagegallery_id) {
	 beforeAjax();
	 $.ajax({
		type: "POST",
		url: "/kbumm/ajax/action/favorite_image",
		data: "command="+command+"&image_id="+image_id+"&imagegallery_id="+imagegallery_id,
		success: function(json) {
			if (json && 'success' in json && json.success) {
				if(command == "add") {
					$("#favorite_image").attr("href", "javascript:favorite_image('remove', "+image_id+", "+imagegallery_id+");");
					$("#favorite_image").html(json.label);
				}
				else {
					$("#favorite_image").attr("href", "javascript:favorite_image('add', "+image_id+", "+imagegallery_id+");");
					$("#favorite_image").html(json.label);
				}
				
				$("#fans").html(json.html);
			}
			else {
				ajaxError(json);
			}
			if (json && 'messages' in json) {
				showMessages(json.messages);
			}
				
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			ajaxError(errorThrown);
		}
	});
}

function getFavoriteImages(user_id, start) {
	 beforeAjax();
	 $.ajax({
		type: "POST",
		url: "/kbumm/ajax/action/get_favorite_images",
		data: "user_id="+user_id+"&start="+start,
		success: function(json) {
			if (json && 'success' in json && json.success) {
				$("#favorite_images").html(json.html);
				$("#favorite_images_nav").html(json.nav);
			}
			else {
				ajaxError(json);
			}
			if (json && 'messages' in json) {
				showMessages(json.messages);
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			ajaxError(errorThrown);
		}
	});
}

function trDelete(id) {
	var tr = $("#" + id);
	/*
	tr.children().each(function() {  
		$(this).wrapInner("<div />").children("div").slideUp(function() {tr.remove();})  
	});
	*/
	tr.fadeOut();
}

function displayLoader(id, style) {
	if(style != undefined) {
		$("#"+id).before('<div id="loader_'+id+'" class="loadingbutton" style="'+style+'" />');
	}
	else {
		$("#"+id).before('<div id="loader_'+id+'" class="loadingbutton" />');
	}
}

function removeLoader(id) {
	$("#loader_"+id).remove();
}

function clickedDoingTextarea(id) {
	if(!$("#"+id).hasClass("black")) {
		$("#"+id).addClass("black");
		$("#"+id).val("");
	}
}

function clickedDoingCommentTextarea(id) {
	if(!$("#"+id).hasClass("black")) {
		$("#"+id).addClass("black");
		$("#"+id).val("");
		$("#"+id+"_submit").slideDown();
	}
}

function showDoingComments(id) {
	if($("#doing_comments"+id).css("display") == "none") {
		$("#doing_comments"+id).fadeIn();
	}
	if($("#doing_comment"+id+"_form").css("display") == "none") {
		$("#doing_comment"+id+"_form").fadeIn();
	}
	$("#doing_comment"+id).focus();
}

function deleteDoing(id) {
	if(confirm("Willst du diesen Pinnwandeintrag mit allen Kommentaren wirklich löschen?")) {
	    beforeAjax();
		 $.ajax({
			type: "POST",
			url: "/kbumm/ajax/action/delete_doing",
			data: "id="+id,
			success: function(json) {
				if (json && 'success' in json && json.success) {
					trDelete("doing_"+id+"_tr");
				}
				else {
					ajaxError(json);
				}
				if (json && 'messages' in json) {
					showMessages(json.messages);
				}
					
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				ajaxError(errorThrown);
			}
		});
	}
}
function saveDoingComment(id) {
	 if($("#doing_comment"+id).val().length <= 0) {
		 return;
	 }
     beforeAjax();
	 $("#doing_comment"+id+"_submitbutton").hide();
	 displayLoader("doing_comment"+id+"_submitbutton", "float: right; margin-bottom: 3px");
	 var formdata = $("#doing_comment"+id+"_form").serialize();
	 $.ajax({
		type: "POST",
		url: "/kbumm/ajax/action/save_doing_comment",
		data: formdata,
		success: function(json) {
			if (json && 'success' in json && json.success) {
				removeLoader("doing_comment"+id+"_submitbutton");
				$("#doing_comment"+id+"_body").prepend(json.html);
				$("#doing_comment"+json.id+"_tr").fadeIn();
				$("#doing_comment"+id).val("");
				$("#doing_comment"+id+"_submitbutton").show();
				$("#doing_comment"+id+"_form").hide();
			}
			else {
				ajaxError(json);
				removeLoader("doing_comment"+id+"_submitbutton");
				$("#doing_comment"+id+"_submitbutton").show();
			}
			if (json && 'messages' in json) {
				showMessages(json.messages);
			}
				
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			ajaxError(errorThrown);
			removeLoader("doing_comment"+id+"_submitbutton");
			$("#doing_comment"+id+"_submitbutton").show();
		}
	});	
}

function deleteDoingComment(id) {
	if(confirm("Willst du diesen Kommentar wirklich löschen?")) {
	    beforeAjax();
		 $.ajax({
			type: "POST",
			url: "/kbumm/ajax/action/delete_doing_comment",
			data: "id="+id,
			success: function(json) {
				if (json && 'success' in json && json.success) {
					trDelete("doing_comment"+id+"_tr");
				}
				else {
					ajaxError(json);
				}
				if (json && 'messages' in json) {
					showMessages(json.messages);
				}
					
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				ajaxError(errorThrown);
			}
		});	
	}
}

function getLocationFans(id, start) {
    beforeAjax();
	 $.ajax({
		type: "POST",
		url: "/kbumm/ajax/action/get_location_fans",
		data: "id="+id+"&start="+start,
		success: function(json) {
			if (json && 'success' in json && json.success && 'html' in json) {
				$("#fans").html(json.html);
			}
			else {
				ajaxError(json);
			}
			if (json && 'messages' in json) {
				showMessages(json.messages);
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			ajaxError(errorThrown);
		}
	});	
}

function getImageFans(id, start) {
    beforeAjax();
	 $.ajax({
		type: "POST",
		url: "/kbumm/ajax/action/get_image_fans",
		data: "id="+id+"&start="+start,
		success: function(json) {
			if (json && 'success' in json && json.success && 'html' in json) {
				$("#fans").html(json.html);
			}
			else {
				ajaxError(json);
			}
			if (json && 'messages' in json) {
				showMessages(json.messages);
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			ajaxError(errorThrown);
		}
	});	
}

function toggle(id) {
	$("#"+id).toggle();
}

function show(id) {
	$("#"+id).show();
	if(id == "add_link") {
		$("#attach_link").val("1");
		$("#add_link_content").html("");
		$("#add_link_content").css("display", "none");
		$("#add_link_url").val("http://");
		$("#add_link_form").css("display", "block");
	}
	else if(id == "add_image") {
		$("#attach_image").val("1");
	}	
}

function hide(id) {
	$("#"+id).hide();
	if(id == "add_link") {
		$("#attach_link").val("0");
		$("#add_link_content").html("");
		$("#add_link_content").css("display", "none");
		$("#add_link_url").val("http://");
		$("#add_link_form").css("display", "block");
	}
	else if(id == "add_image") {
		$("#attach_image").val("0");
	}
}

function slideToggle(id) {
	$("#"+id).slideToggle();
}

function submit_doing() {
	$("#form_doing_submitbutton").hide();
	displayLoader("form_doing_submitbutton", "float: right; margin-top: 5px; margin-right: 5px");
	$("#form_doing").submit();
}
