$(document).ready(function ()
{
	/* ---------------------------------------------------------------------------------------------------------------------------------
																		ALBUMS
	----------------------------------------------------------------------------------------------------------------------------------*/
		
	//Tabs
	var $album_tabs = $("#albumtabs").tabs(
	{
		tabTemplate: '',
		panelTemplate: '',
		add: function(event, ui)
		{
			//$(ui.panel).append('<div class="reallynophotos">Es gibt noch keine Fotos in diesem Album.</div>');
		},
		select: function(event, ui)
		{
			if($(ui.panel).hasClass("own"))
			{
				reload_tag_cloud($(ui.panel).children(".tagcloud"));
			}
			else
			{
				reload_tag_cloud($(ui.panel).children(".tagcloud"), 1);
			}
			
			//Bilder des Albums nachladen
			$(ui.panel).find(".photo img").each(function()
			{
				if($(this).attr("src") == "")
				{
					$(this).attr("src", $(this).attr("alt"));
				}
			});
		}
	});
	
	//Photos in Alben einsortieren
	$("#albumtabs.sortable .albumcontent .photo.own").draggable(
	{
		revert: 'invalid',
		zIndex: 1,
		opacity: 0.35,
		helper: 'clone',
		cancel: 'input'
	});
	
	//Photos auf Tabs fallen lassen
	var $album_tab_items = $("#albumtabs.sortable ul:first li").droppable(
	{
		accept: '#albumtabs.sortable .albumcontent .photo',
		tolerance: 'pointer',
		hoverClass: 'ui-state-hover',
		drop: function(ev, ui)
		{
			movePhoto(ev, ui, $(this));
		}
	});
	
	rename_album_button($("#albumtabs.sortable .album.own"));
	
	///////Alben löschen/hinzufügen
	var $album_name_input = $('#new_album_name'), $album_description_input = $('#new_album_description');
	
	//Dialog-Form für neues Album
	var $new_album_dialog = $('#new_album_dialog').dialog(
	{
		autoOpen: false,
		modal: true,
		width: 400,
		buttons:
		{
			'Abbrechen': function()
			{
				$(this).dialog('close');
			},
			'Anlegen': function()
			{
				new_album();
				$(this).dialog('close');
			}
		},
		open: function()
		{
			$album_name_input.focus();
		},
		close: function()
		{
			$new_album_form[0].reset();
		}
	});
	
	//Bei Submit im Form für neues Album wird die Funktion new_album aufgerufen
	var $new_album_form = $('form',$new_album_dialog).submit(function()
	{
		new_album();
		$new_album_dialog.dialog('close');
		return false;
	});
	
	//Legt per AJAX ein neues Album an und zeigt dieses an
	function new_album()
	{
		var pname = $album_name_input.val() || 'Neues Album';
		var pdescription = $album_description_input.val() || '';
		
		//Neues Album speichern (AJAX), als Rückgabe die ID
		$.post("ownphotos.php",
		{
			newalbum: pname,
			description: pdescription
		},
		function(data)
		{
			var elements = data.getElementsByTagName("result")[0].childNodes;
			var x;
			var values = new Array();
			for(x in elements)
			{
				if(elements[x].nodeName && elements[x].nodeName != "#text" && elements[x].firstChild)
				{
					values[elements[x].nodeName] = elements[x].firstChild.nodeValue;
				}
			}
			
			var id = values['id'];
			var name = values['name'];
			
			$("#albumtabs #albums li.album.unordered").after('<li class="album own deleteable ui-state-default ui-droppable"><button class="delete_album">Album löschen</button><a href="#album-' + id + '">' + name + '</a></li>');
			$("#albumtabs #album-0").after('<div class="albumcontent own" id="album-' + id + '"><div class="tagcloud"></div><div class="reallynophotos">Es gibt noch keine Fotos in diesem Album.</div></div>');
			$album_tabs.tabs('add', '#album-' + id, name);
			$("#albumtabs #album-" + id).insertAfter($("#albumtabs #album-0"));
			
			rename_album_button($("#albumtabs #albums li.album.unordered").next("li.album"));
			delete_album_button($("#albumtabs #albums li.album.unordered").next("li.album").children(".delete_album"));
			
			$("#albumtabs #albums li.album.unordered + li.album").droppable(
			{
				accept: '#albumtabs.sortable .albumcontent .photo',
				tolerance: 'pointer',
				hoverClass: 'ui-state-hover',
				drop: function(ev, ui)
				{
					movePhoto(ev, ui, $(this));
				}
			});
			
			$album_tab_items = $album_tab_items.add($("#albumtabs #albums li.album.unordered + li.album"));
		});
	}
	
	//Der "Neues Album anlegen"-Button öffnet den Dialog
	$('#add_album').click(function()
	{
		$new_album_dialog.dialog('open');
	});
	
	//"Album löschen"-Buttons initialisieren
	delete_album_button($("#albums .delete_album"));
	
	//Dialog zum löschen von Alben mit Fotos
	var $delete_album_dialog = $('#delete_album_dialog').dialog(
	{
		autoOpen: false,
		modal: true,
		width: 400,
		buttons:
		{
			'Abbrechen': function()
			{
				$(this).dialog('close');
			},
			'Nein': function()
			{
				delete_album();
				$(this).dialog('close');
			},
			'Ja': function()
			{
				delete_album_photos();
				$(this).dialog('close');
			}
		}
	});
	
	//Dialog zum löschen leerer Alben
	var $delete_empty_album_dialog = $('#delete_empty_album_dialog').dialog(
	{
		autoOpen: false,
		modal: true,
		width: 400,
		buttons:
		{
			'Abbrechen': function()
			{
				$(this).dialog('close');
			},
			'OK': function()
			{
				delete_album();
				$(this).dialog('close');
			}
		}
	});
	
	//Macht das übergebene Objekt zum "Album löschen"-Button
	function delete_album_button(thebutton)
	{
		thebutton.button(
		{
			icons:
			{
				primary: 'ui-icon-close'
			},
			text: false
		}).click(function()
		{
			var href = $(this).parent().children("a").attr("href");
			var teile = href.split("-");
			var id = teile[1];
			
			delete_album_id = id;
			
			if($(href).children(".photo").size() > 0)
			{
				$delete_album_dialog.dialog('open');
			}
			else
			{
				$delete_empty_album_dialog.dialog('open');
			}
		});
	}
	
	//Löscht das angegebene Album und die enthaltenen Fotos
	function delete_album_photos()
	{
		if(delete_album_id != 0)
		{
			id = delete_album_id;
			delete_album_id = 0;
			
			if($("#albumtabs .album:eq(" + $album_tabs.tabs('option', 'selected') + ")").children("a").attr("href") == "#album-" + id)
			{
				$album_tabs.tabs('select', '#album-0');
			}
			
			$("#album-" + id).remove();
			$("#albums .album a[href=#album-" + id + "]").parent().fadeOut(500);
			
			$.get("ownphotos.php?deletealbum=" + id + "&deletephotos=1&" + nocaching());
		}
	}
	
	//Löscht das angegebene Album und verschiebt die enthaltenen Fotos nach "Ungeordnet"
	function delete_album()
	{
		if(delete_album_id != 0)
		{
			id = delete_album_id;
			delete_album_id = 0;
			
			if($("#albumtabs .album:eq(" + $album_tabs.tabs('option', 'selected') + ")").children("a").attr("href") == "#album-" + id)
			{
				$album_tabs.tabs('select', '#album-0');
			}
			
			$("#album-" + id).children(".photo").hide().appendTo($("#album-0"));
			$("#album-0").children(".nophotos").fadeOut(200);
			$("#album-0").children(".reallynophotos").fadeOut(200);
			
			sortPhotos($("#album-0"));
			$("#album-0").children(".photo").show('slow');
			
			$("#album-" + id).remove();
			$("#albums .album a[href=#album-" + id + "]").parent().fadeOut(500);
			
			$.get("ownphotos.php?deletealbum=" + id + "&" + nocaching());
		}
	}
	
	/////Photos löschen
	
	//Dustbin
	var $dustbin = $("#albumtabs.sortable #dustbin").droppable(
	{
		accept: '#albumtabs.sortable .albumcontent .photo',
		tolerance: 'pointer',
		hoverClass: 'ui-state-hover',
		drop: function(ev, ui)
		{
			var id = ui.draggable.attr("id");
			var teile = id.split("_");
			var photo_id = teile[1];
			
			delete_photo_id = photo_id;
			
			$delete_photo_dialog.dialog('open');
		}
	});
	
	//Dialog zum löschen von Alben mit Fotos
	var $delete_photo_dialog = $('#delete_photo_dialog').dialog(
	{
		autoOpen: false,
		modal: true,
		width: 400,
		buttons:
		{
			'Abbrechen': function()
			{
				$(this).dialog('close');
			},
			'Ok': function()
			{
				delete_photo();
				$(this).dialog('close');
			}
		}
	});
	
	//Löscht das angegebene Foto
	function delete_photo()
	{
		if(delete_photo_id != 0)
		{
			var album = $("#photo_" + delete_photo_id).parent();
			
			$("#photo_" + delete_photo_id).remove();
			album_to_check = album;
			window.setTimeout("checkNumPhotos(album_to_check)", 1000);
			
			$.get("ownphotos.php?deletephoto=" + delete_photo_id + "&" + nocaching());
			
			if(album.hasClass("own"))
			{
				reload_tag_cloud(album.children(".tagcloud"));
			}
			else
			{
				reload_tag_cloud(album.children(".tagcloud"), 1);
			}
			
			delete_photo_id = 0;
		}
	}
	
	//Verschiebt ein Foto in ein Album (on drop)
	function movePhoto(ev, ui, $item)
	{
		old = ui.draggable.parent();
		var $list = $($item.find('a').attr('href'));
		
		//Speichern
		var id = ui.draggable.attr('id');
		var teile = id.split("_");
		var photoid = teile[1];
		var id = $list.attr('id');
		var teile = id.split("-");
		var albumid = teile[1];
		$.get("ownphotos.php?movephoto=" + photoid + "&to=" + albumid + "&" + nocaching());
		
		if(ui.draggable.parent().hasClass("own"))
		{
			reload_tag_cloud(ui.draggable.parent().children(".tagcloud"));
		}
		else
		{
			reload_tag_cloud(ui.draggable.parent().children(".tagcloud"), 1);
		}
		
		ui.draggable.hide('slow', function()
		{
			$(this).appendTo($list);
			
			sortPhotos(ui.draggable.parent());
			
			$list.children(".nophotos").hide();
			$list.children(".reallynophotos").hide();
			$(this).show('slow');
			window.setTimeout("checkNumPhotos(old)", 600);
		});
	}
	
	//Sortiert die Fotos in einem Album
	function sortPhotos(album)
	{
		var bilder = new Array();
		album.children(".photo").each(function()
		{
			datum = $(this).children(".date").text();
			bilder[bilder.length] = new Array(datum, $(this));
		});
		bilder.sort(function(a, b)
		{
			return a[0] - b[0];
		});
		
		var i;
		for(i=0; i<bilder.length; i++)
		{
			bilder[i][1].appendTo(album);
		}
	}
	
	////Fotos hochladen
	
	//"Fotos hochladen"-Button
	$("#add_photos").click(function()
	{
		$new_photos_dialog.dialog("open");
	});
	
	//"Fotos hochladen"-Dialog
	var $new_photos_dialog = $("#new_photos_dialog").dialog(
	{
		autoOpen: false,
		modal: true,
		width: 400,
		close: function()
		{
			window.location.href = 'ownphotos.php';
		}
	});
	
	//Fotos hochladen
	$("#photos_input").uploadify({
		'uploader': 'modules/gallery/swf/uploadify.swf',
		'script': 'modules/gallery/scripts/uploadify.php',
		'cancelImg': 'images/icons/16/delete.png',
		'scriptData': 
		{
			'session': $('#sessid').val()
		},
		'fileExt': '*.jpg;*.gif;*.png',
		'fileDesc': 'Bilder (*.jpg,*.gif,*.png)',
		'buttonText': 'Bilder auswählen',
		'auto': true,
		'multi': true
	});
	
	//Tags speichern
	$(".photo_tags").change(function()
	{
		$tagfield = $(this);
		var id = $(this).parent().attr("id");
		var teile = id.split("_");
		var photo = teile[1];
		
		$.get("ownphotos.php?tagphoto=" + photo + "&tags=" + $(this).val() + "&" + nocaching(), function(data)
		{
			if($tagfield.parent().parent().hasClass("own"))
			{
				reload_tag_cloud($tagfield.parent().parent().children(".tagcloud"));
			}
			else
			{
				reload_tag_cloud($tagfield.parent().parent().children(".tagcloud"), 1);
			}
		});
	});
	
	//Tagcloud neu laden
	function reload_tag_cloud($tagcloud, $own)
	{
		var id = $tagcloud.parent().attr("id");
		var teile = id.split("-");
		var album = teile[1];
		
		$tagcloud.load("ownphotos.php?tagcloud=" + album + "&own=" + $own + "&" + nocaching());
	}
	
	///////Umbenennen von Alben
	
	//Öffnen des Dialogs
	function rename_album_button(album)
	{
		album.dblclick(function()
		{
			var id = $(this).children("a").attr("href");
			var teile = id.split("-");
			var albumid = teile[1];
			
			$rename_album_id_input.val(albumid);
			$rename_album_name_input.val($(this).children("a").children(".album_name").text());
			$rename_album_description_input.val($(this).children("a").children(".album_description").text());
			$rename_album_dialog.dialog("open");
		});
	}
	
	var $rename_album_id_input = $('#rename_album_id'), $rename_album_name_input = $('#rename_album_name'), $rename_album_description_input = $('#rename_album_description');
	
	//Dialog-Form fürs Umbenennen eines Albums
	var $rename_album_dialog = $('#rename_album_dialog').dialog(
	{
		autoOpen: false,
		modal: true,
		width: 400,
		buttons:
		{
			'Abbrechen': function()
			{
				$(this).dialog('close');
			},
			'Umbenennen': function()
			{
				rename_album();
				$(this).dialog('close');
			}
		},
		open: function()
		{
			$rename_album_name_input.focus();
		},
		close: function()
		{
			$rename_album_form[0].reset();
		}
	});
	
	//Bei Submit im Form fürs Umbenennen eines Albums wird die Funktion new_album aufgerufen
	var $rename_album_form = $('form',$rename_album_dialog).submit(function()
	{
		rename_album();
		$rename_album_dialog.dialog('close');
		return false;
	});
	
	//Umbenennen
	function rename_album()
	{
		var id = $rename_album_id_input.val();
		var pname = $rename_album_name_input.val() || $("#albums .album a[href=#album-" + id + "]").children(".album_name").text();
		var pdescription = $rename_album_description_input.val() || '';
		
		$("#albums .album a[href=#album-" + id + "]").children(".album_name").html(pname);
		$("#albums .album a[href=#album-" + id + "]").children(".album_description").html(pdescription);
		
		$rename_album_dialog.dialog('close');
		
		$.post("ownphotos.php",
		{
			renamealbum: id,
			name: pname,
			description: pdescription
		});
	}
	
	/////Alben anzeigen
	
	// Usercontrol
	$('#users .delete').click(function()
	{
		var $deletebutton = $(this);
		var id = $(this).parent().parent().attr('id').substr(4);
		var confirmation = confirm("Soll der Nutzer wirklich gelöscht werden?");
		
		if(confirmation)
		{
			$.get('usercontrol.php?action=delete&id=' + id + '&' + nocaching(), function(data)
			{
				if(data == 1)
				{
					$deletebutton.parent().parent().remove();
				}
			});
		}
	});
});

//Prüft, ob noch Photos angezeigt werden und blendet gegebenenfalls Meldung ein
function checkNumPhotos(list)
{
	if(list.children(".photo").size() == 0)
	{
		list.children(".nophotos").fadeIn(200);
	}
}
