$(document).ready(function ()
{
	/* ---------------------------------------------------------------------------------------------------------------------------------
																		FORUMSVERWALTUNG SORTIERUNG
	----------------------------------------------------------------------------------------------------------------------------------*/
	
	//Reihenfolge der Kategorien
	$(".boardsortable").sortable(
	{
		cancel: 'a, input, li > ul',
		axis: 'y',
		tolerance: 'pointer',
		connectWith: '.deletethem'
	});
	
	//Reihenfolge der Foren in einer Kategorie
	$(".categorysortable").sortable(
	{
		cancel: 'a, input',
		axis: 'y',
		tolerance: 'pointer',
		connectWith: '.categorysortable, .deletethem',
		receive: function(event, ui)
		{
			var id = ui.item.attr('id');
			var teile = id.split("_");
			var id2 = ui.item.parent().parent().attr('id');
			var teile2 = id2.split("_");
			teile[1] = teile2[1];
			id = teile.join("_");
			ui.item.attr('id', id);
		}
	});
	
	//"Mülleimer"
	$(".deletethem").sortable(
	{
		tolerance: 'pointer',
		items: '',
		receive: function(event, ui)
		{
			ui.item.hide();
		}
	});
	
	//Neue Kategorie anlegen
	$("#new_category").click(function()
	{
		var request = "forumcontrol.php?action=newcategory";
		$.get(request + "&" + nocaching(), function(data)
		{
			$(".boardsortable").append('\n'
				+ '						<li id="category_' + data + '" class="s_category">'
				+ '							<input class="catname" type="text" name="catname_' + data + '" value="Neue Kategorie" />'
				+ '							<ul class="categorysortable catsortable">'
				+ '							</ul>'
				+ '						</li>');
			
			$(".categorysortable:last").sortable(
			{
				cancel: 'a, input',
				axis: 'y',
				tolerance: 'pointer',
				connectWith: '.categorysortable, .deletethem',
				receive: function(event, ui)
				{
					var id = ui.item.attr('id');
					var teile = id.split("_");
					var id2 = ui.item.parent().parent().attr('id');
					var teile2 = id2.split("_");
					teile[1] = teile2[1];
					id = teile.join("_");
					ui.item.attr('id', id);
				}
			});
		});
	});
	
	//Neues Forum in letzter Kategorie anlegen
	$("#new_forum").click(function()
	{
		if($(".boardsortable").children().size() > 0)
		{
			var request = "forumcontrol.php?action=newforum&parent=";
			var id = $(".boardsortable").children(":last").attr('id');
			var teile = id.split("_");
			var parent = teile[1];
			request += parent;
			
			$.get(request + "&" + nocaching(), function(data)
			{
				if(data != "false")
				{
					$(".categorysortable:last").append('\n'
						+ '										<li id="category_' + parent + '_' + data + '" class="s_forum">'
						+ '											<input class="boardname" type="text" name="boardname_' + data + '" value="Neues Forum" />'
						+ '											<input class="boarddescription" type="text" name="boarddescription_' + data + '" />'
						+ '											<input type="checkbox" name="boardlocked_' + data + '" value="1" /> schließen'
						+ '										</li>');
				}
			});
		}
		else
		{
			alert("Es ist noch keine Kategorie vorhanden. Lege erst eine Kategorie an!");
		}
	});
	
	//Speichern der Struktur des Forums
	$("#save_structure").click(function()
	{
		//Reihenfolge der Kategorien
		var request = "forumcontrol.php?action=savestructure&" + $(".boardsortable").sortable('serialize');
		var teile;
		//Reihenfolge der Foren pro Kategorie
		$(".categorysortable").each(function()
		{
			request += "&" + $(this).sortable('serialize');
		});
		//Namen der Kategorien
		$(".s_category").each(function()
		{
			teile = $(this).attr('id').split("_");
			request += "&cat" + teile[1] + "=" + $(this).children(".catname").val();
		});
		//Namen der Foren
		$(".s_forum").each(function()
		{
			teile = $(this).attr('id').split("_");
			request += "&forum" + teile[2] + "=" + $(this).children(".boardname").val();
			request += "&forumdesc" + teile[2] + "=" + $(this).children(".boarddescription").val();
			//Foren schließen/öffnen
			if($(this).children(".boardlocked").attr("checked"))
			{
				request += "&locked[" + teile[2] + "]=1";
			}
			else
			{
				request += "&locked[" + teile[2] + "]=0";
			}
		});
		//Kategorien löschen
		$(".deletethem").children(".s_category").each(function()
		{
			teile = $(this).attr('id').split("_");
			request += "&deletecat[]=" + teile[1];
		});
		//Foren löschen
		$(".deletethem").children(".s_forum").each(function()
		{
			teile = $(this).attr('id').split("_");
			request += "&deleteforum[]=" + teile[2];
		});
		//Absenden des Ganzen
		$.get(request + "&" + nocaching(), function(data)
		{
			alert(data);
		});
	});
	
	//Thread verschieben
	$(".draggablethread").draggable(
	{
		containment: '.scrollcontainer'
	});
	$(".s_forum").droppable(
	{
		accept: '.draggablethread',
		hoverClass: 'hovering',
		drop: function(event, ui)
		{
			var request = "forumcontrol.php?movethread=" + ui.draggable.attr('id').substr(15) + "&to=" + $(this).attr('id').substr(5);
			var target = "thread.php?id=" + ui.draggable.attr('id').substr(15);
			$.get(request + "&" + nocaching(), function(data)
			{
				window.location.href = target;
			});
		}
	});
	
	/* ---------------------------------------------------------------------------------------------------------------------------------
																		FORUM
	----------------------------------------------------------------------------------------------------------------------------------*/
	
	//"Neues Thema"-Button
	$("#new_thread_button").click(function()
	{
		$("#new_thread_button").hide();
		$("#add_poll").hide();
		$("#new_thread").slideDown("500");
	});
	
	//"Neuer Beitrag"-Button
	$("#new_post_button").click(function()
	{
		$("#new_post_button, #new_post_button_top").hide();
		$("#new_post").slideDown("500");
	});
	$("#new_post_button_top").click(function()
	{
		$("#new_post_button, #new_post_button_top").hide();
		$("#new_post").show();
		
		window.location.hash = 'edit';
	});
	
	//Kategorie ein/ausblenden
	$(".hidecat").toggle(function()
	{
		$(this).html("einblenden").parent().parent().parent().children("tr:gt(0)").fadeOut(300);
	}, function()
	{
		$(this).html("ausblenden").parent().parent().parent().children("tr:gt(0)").fadeIn(300);
	});
	
	//Beitrag löschen
	$(".deletepost").click(function()
	{
		var this_button = $(this);
		var confirmed = confirm("Soll der Beitrag wirklich gelöscht werden?");
		
		if(confirmed)
		{
			var request = "thread.php?delete=" + $(this).parent().parent().parent().attr('id').substr(4);
			$.ajax(
			{
				url: request,
				dataType: 'xml',
				success: function(data)
				{
					if(data.getElementsByTagName("success")[0].firstChild.nodeValue == 1)
					{
						this_button.parent().parent().parent().fadeOut(300);
					}
				}
			});
		}
	});
	
	//Thread löschen
	$(".deletethread").click(function()
	{
		var confirmed = confirm("Soll das Thema wirklich gelöscht werden?");
		
		if(confirmed)
		{
			var request = "board.php?delete=" + $(this).parent().parent().parent().parent().attr('id').substr(6);
			window.location.href = request;
		}
	});
	
	//Thread abonnieren
	$("#abo").click(function()
	{
		this_button = $(this);
		$.get("abos.php?abonnate=" + this_button.parent().attr('id').substr(6) + "&" + nocaching(), function(data)
		{
			if(data == "1")
			{
				this_button.html('<img src="images/icons/32/eye_delete.png" alt="" /> <span>Abonnement entfernen</span>');
				this_button.attr('title', 'Abonnement entfernen');
			}
			else
			{
				this_button.html('<img src="images/icons/32/eye.png" alt="" /> <span>Abonnieren</span>');
				this_button.attr('title', 'Abonnieren');
			}
		});
	});
	
	//Abo entfernen
	$(".removeabo").click(function()
	{
		this_button = $(this);
		$.get("abos.php?abonnate=" + this_button.parent().parent().attr('id').substr(3) + "&" + nocaching(), function(data)
		{
			if(data == "1")
			{
				$.get("abos.php?abonnate=" + this_button.parent().parent().attr('id').substr(3) + "&" + nocaching(), function(data)
				{
					this_button.parent().parent().fadeOut(500);
					window.setTimeout("checkNumAbos()", 600);
				});
			}
			else
			{
				this_button.parent().parent().fadeOut(500);
				window.setTimeout("checkNumAbos()", 600);
			}
		});
	});
	
	/////Titel prüfen
	$('#new_thread').submit(function()
	{
		if($('#new_thread #title').val() == "")
		{
			alert("Bitte gib einen Titel für dein Thema ein!");
			return false;
		}
	});
	$('#new_post, #edit_post').submit(function()
	{
		if($('#new_post #title, #edit_post #title').val() == "")
		{
			alert("Bitte gib einen Titel für deinen Beitrag ein!");
			return false;
		}
	});
	
	/////Umfragen erstellen
	$('#new_thread #add_poll_button').click(function()
	{
		$(this).hide();
		$('#new_thread #add_poll').slideDown(500);
	});
	
	$('#add_poll #add_choice_button').click(function()
	{
		$('#add_poll #choices').append('<div class="newchoice"><input type="text" name="u_choice[]" /> <div class="delete mybutton" title="Löschen"><img src="images/icons/16/chart_bar_delete.png" alt="Löschen" /></div></div>');
		$('#add_poll #choices .delete').last().click(function()
		{
			deletethischoice($(this));
		});
	});
	
	$('#add_poll #choices .delete').click(function()
	{
		deletethischoice($(this));
	});
	
	function deletethischoice(deletebuttonclicked)
	{
		if($('#add_poll #choices .delete').size() > 1)
		{
			deletebuttonclicked.parent().remove();
		}
	}
});

//Prüft, ob noch Abos angezeigt werden und blendet gegebenenfalls Meldung ein
function checkNumAbos()
{
	if($("tr.row:visible").size() < 1)
	{
		$(".noabos").fadeIn(200);
	}
}
