var _lightboxType = '';
var _lightboxid = 0;

function add_to_lightbox()
{
	if (typeof(lightboxesDisabled) != 'boolean') lightboxesDisabled = false;
	if (lightboxesDisabled)
	{
		alert('You cannot add Live photos to a lightbox! Please wait until these photos have been added to an archive.');
		return;
	}
	
	_lightboxType = 'add';
	open_menu('lightbox', 'dashboard-menu-selected', true);
}

function move_to_lightbox(lightbox)
{
	_lightboxType = 'move';
	_lightboxid = lightbox;
	open_menu('lightbox', 'dashboard-menu-selected', true);
}

function rename_lightbox()
{
	var selected = $('#lightbox-list input:checked');
	if (selected.length < 1) return alert('Please select a lightbox first');
	var lightboxid = selected.val();
	var label = $('label[for="lightbox' + lightboxid + '"]');
	var oldname = label.text().match(/^(.*) \(\d*\)/)[1];
	var newname = prompt('Please enter the new name for this lightbox', oldname);
	
	if ((newname.length < 1) || (oldname == newname)) return false;
	if (newname.length > 30) return alert('The lightbox name may only be 30 characters long!');
	
	var data = {'action': 'rename_lightbox', 'lightboxid': lightboxid, 'name': newname};
	$.post(
		'ajax.php',
		data,
		function(data, status, request)
		{
			if (status != 'success') return alert('Unknown AJAX error: Status is ' + status);
			if (data.status == 0) return alert(data.error);
			if (data.status != 1) return alert('Unknown AJAX error: Status ID is ' + data.status);
			label.text(data.name + ' (' + data.count + ')');
			var extra = label.append('<span> ...Renamed</span>').find('span');
			extra.css({'color': '#000', 'font-weight': 'bold'});
			extra.animate(
				{opacity: 0},
				6000,
				function()
				{
					extra.remove();
				}
			);
		},
		'json'
	);
}

function create_lightbox()
{
	var items = {};
	var add = (((_lightboxType == 'add') || (_lightboxType == 'move')) && ($('#dashboard-menu-selected').is(':visible')));
	
	if (add)
	{
		$('input.select:checked').each(function() {items[$(this).attr('rel')] = true;});
	}
	
	var name = prompt('Please enter the name you wish to give this new lightbox');
	
	if (name.length < 1) return false;
	if (name.length > 30) return alert('The lightbox name may only be 30 characters long!');
	
	var data = {'action': 'create_lightbox', 'items': items, 'name': name, 'type': _lightboxType};
	$.post(
		'ajax.php',
		data,
		function(data, status, request)
		{
			if (status != 'success') return alert('Unknown AJAX error: Status is ' + status);
			if (data.status == 0) return alert(data.error);
			if (data.status != 1) return alert('Unknown AJAX error: Status ID is ' + data.status);
			if (data.lightboxid < 1) return alert('Unknown AJAX error: Lightboxid is ' + data.lightboxid);
			
			var form = $('#lightbox-list-f');
			form.append('<input id="lightbox' + data.lightboxid + '" type="radio" onclick="select_lightbox(' + data.lightboxid + ')" value="' + data.lightboxid + '" name="lightbox">');
			form.append('<label for="lightbox' + data.lightboxid + '"></label><br />');
			var label = $('label[for="lightbox' + data.lightboxid + '"]');
			label.text(data.name + ' (' + (data.count) + ')');
			var extra = label.append('<span> ...Created</span>').find('span');
			var scrollheight = $('#lightbox-list').attr('scrollHeight');
			$('#lightbox-list').animate({'scrollTop': scrollheight}, 500);
			extra.css({'color': '#000', 'font-weight': 'bold'});
			extra.animate(
				{'opacity': 0},
				6000,
				function()
				{
					extra.remove();
				}
			);
			if (add)
			{
				if (_lightboxType == 'move') $('div.item-selected').hide();
				$('input.select:checked').attr('checked', false).hide();
				$('div.item-selected').removeClass('item-selected');
			}
		},
		'json'
	);
}

function add_keywords()
{
	var items = $('input.select:checked');
	if (items.length < 1) return alert('Please select some pictures first');
	var keywords = prompt('Please enter the new keywords you wish to add, seperated by a comma:');
	keywords = keywords.replace(/^\s+|\s+$/g, '');
	if (keywords.length < 1) return;
	var data = {'action': 'add_keywords', 'items': {}, 'keywords': keywords};
	
	items.each(function() {data.items[$(this).attr('rel')] = true;})
	$.post(
		'ajax.php',
		data,
		function(data, status, request)
		{
			if (status != 'success') return alert('Unknown AJAX error: Status is ' + status);
			if (data.status == 0) return alert(data.error);
			if (data.status != 1) return alert('Unknown AJAX error: Status ID is ' + data.status);
			
			$('input.select:checked').attr('checked', false).hide();
			$('div.item-selected').removeClass('item-selected');
			close_menus();
		},
		'json'
	);
}

function move_photos_new()
{
	var items = $('input.select:checked');
	if (items.length < 1) return alert('Please select some pictures first');
	var name = prompt('Please enter the name you wish to give this new archive.');
	while ((!name.match(/^\d{8}/)) || (name.length > 45)) name = prompt("The name you have entered is either too long or not in a correct format. It must be no more than 45 chars long, and formatted with the date at the start - for example, '20090801 Test Archive'.\nPlease pick a new one.", name);
	var data = {'action': 'move_to_new_archive', 'items': {}, 'name': name};
	if (name.length < 1) return;
	items.each(function() {data.items[$(this).attr('rel')] = true;})
	
	$('div.search-result:not(.item-selected)').hide();
	scroll(0,0);
	if (confirm('Are you sure you want to move these photos to the new archive "' + name + '"?'))
	{
		$.post(
			'ajax.php',
			data,
			function(data, status, request)
			{
				if (status != 'success') return alert('Unknown AJAX error: Status is ' + status);
				if (data.status == 0) return alert(data.error);
				if (data.status != 1) return alert('Unknown AJAX error: Status ID is ' + data.status);
				
				$('div.item-selected').animate(
					{opacity: 0},
					4000,
					function()
					{
						$(this).remove();
					}
				);
				
				$('div.search-result:not(.item-selected)').show();
				close_menus();
				document.location.href = 'admin/archive.php?do=edit&aid=' + encodeURIComponent(data.archiveid);
			},
			'json'
		);
	}
	else
	{
		$('div.search-result:not(.item-selected)').show();
		return;
	}
}

function move_photos_existing()
{
	var items = $('input.select:checked');
	if (items.length < 1) return alert('Please select some pictures first');
	var archiveid = prompt('Please enter the ID of the archive you wish to move these images to.');
	if ((parseInt(archiveid) < 1) || (isNaN(archiveid))) return;
	var data = {'action': 'move_to_existing_archive', 'items': {}, 'archiveid': archiveid};
	items.each(function() {data.items[$(this).attr('rel')] = true;})
	
	$.post(
		'ajax.php',
		{'action': 'get_archive_name', 'archiveid': archiveid},
		function(data1, status1, request1)
		{
			if (status1 != 'success') return alert('Unknown AJAX error: Status is ' + status1);
			if (data1.status == 0) return alert(data1.error);
			if (data1.status != 1) return alert('Unknown AJAX error: Status ID is ' + data1.status);
			var name = data1.name;
			
			$('div.search-result:not(.item-selected)').hide();
			scroll(0,0);
			if (confirm('Are you sure you want to move these photos to\n\n"' + name + '"?'))
			{
				$.post(
					'ajax.php',
					data,
					function(data2, status2, request2)
					{
						if (status2 != 'success') return alert('Unknown AJAX error: Status is ' + status2);
						if (data2.status == 0) return alert(data2.error);
						if (data2.status != 1) return alert('Unknown AJAX error: Status ID is ' + data2.status);
						
						$('div.item-selected').animate(
							{opacity: 0},
							4000,
							function()
							{
								$(this).remove();
							}
						);
						
						$('div.search-result:not(.item-selected)').show();
						close_menus();
					},
					'json'
				);
			}
			else
			{
				$('div.search-result:not(.item-selected)').show();
				return;
			}
		}
	);
}

function edit_photos()
{
	if ($('#search_method').length < 1) return;
	if (!confirm("Are you sure you want to edit these photos?")) return;
	
	$('#search_method').attr('action', 'admin/photos.php?ref=' + encodeURIComponent(document.location));
	$('#search_method').submit();
}

function select_lightbox(id)
{
	if (!$('#dashboard-menu-selected').is(':visible')) return;
	
	var items = $('input.select:checked');
	if (items.length < 1) return alert('Please select some pictures first');
	
	if (_lightboxType == 'add')
	{
		if (!confirm('Are you sure you wish to add ' + items.length + ' photo(s) to this lightbox?')) return;
	}
	else if (_lightboxType == 'move')
	{
		if (!confirm('Are you sure you wish to move ' + items.length + ' photo(s) to the selected lightbox? They will be removed from the current lightbox.')) return;
	}
		
	var data = {'action': 'add_to_lightbox', 'items': {}, 'lightboxid': id, 'type': _lightboxType, 'currentLightboxid': currentLightboxid};
	items.each(function() {data.items[$(this).attr('rel')] = true;})
	$.post(
		'ajax.php',
		data,
		function(data, status, request)
		{
			if (status != 'success') return alert('Unknown AJAX error: Status is ' + status);
			if (data.status == 0) return alert(data.error);
			if (data.status != 1) return alert('Unknown AJAX error: Status ID is ' + data.status);
			
			var label = $('label[for="lightbox' + data.lightboxid + '"]');
			if (label.length < 1) return alert('Unknown AJAX error: Cannot find lightbox label');
			label.text(data.name + ' (' + (data.newcount + data.oldcount) + ')');
			var extra = label.append('<span> ...' + data.newcount + ' images added</span>').find('span');
			extra.css({'color': '#000', 'font-weight': 'bold'});
			extra.animate(
				{opacity: 0},
				6000,
				function()
				{
					extra.remove();
				}
			);
			
			if (_lightboxType == 'move')
			{
				var clabel = $('label[for="lightbox' + data.clightboxid + '"]');
				if (clabel.length < 1) return alert('Unknown AJAX error: Cannot find current lightbox label');
				clabel.text(data.cname + ' (' + (data.cnewcount + data.coldcount) + ')');
				var cextra = clabel.append('<span> ...' + data.cnewcount + ' images removed</span>').find('span');
				cextra.css({'color': '#000', 'font-weight': 'bold'});
				cextra.animate(
					{opacity: 0},
					6000,
					function()
					{
						cextra.remove();
					}
				);
				
				$('div.item-selected').animate(
					{opacity: 0},
					4000,
					function()
					{
						$(this).remove();
					}
				);
			}
			
			$('input.select:checked').attr('checked', false).hide();
			$('div.item-selected').removeClass('item-selected');
		},
		'json'
	);
}

function open_lightbox()
{
	var selected = $('#lightbox-list input:checked');
	if (selected.length < 1) return alert('Please select a lightbox first');
	var lightboxid = selected.val();
	document.location.href = 'search.php?lightboxid=' + lightboxid;
}

function delete_lightbox()
{
	var selected = $('#lightbox-list input:checked');
	if (selected.length < 1) return alert('Please select a lightbox first');
	var lightboxid = selected.val();
	var radio = $('input#lightbox' + lightboxid);
	var label = $('label[for="lightbox' + lightboxid + '"]');
	var br = label.next();
	
	if (!confirm('Are you sure you want to delete this lightbox?')) return;
	
	var data = {'action': 'delete_lightbox', 'lightboxid': lightboxid};
	$.post(
		'ajax.php',
		data,
		function(data, status, request)
		{
			if (status != 'success') return alert('Unknown AJAX error: Status is ' + status);
			if (data.status == 0) return alert(data.error);
			if (data.status != 1) return alert('Unknown AJAX error: Status ID is ' + data.status);
			var extra = label.append('<span> ...Removed</span>').find('span');
			extra.css({'color': '#000', 'font-weight': 'bold'});
			label.add(radio).animate(
				{opacity: 0},
				6000,
				function()
				{
					extra.remove();
					br.remove();
					label.remove();
					radio.remove();
				}
			);
		},
		'json'
	);
}

function remove_from_lightbox()
{
	var items = $('input.select:checked');
	if (items.length < 1) return alert('Please select some pictures first');
	
	if(!confirm('Are you sure you wish to remove ' + items.length + ' images from this lightbox?')) return;
		
	var data = {'action': 'remove_from_lightbox', 'items': {}, 'lightboxid': currentLightboxid};
	items.each(function() {data.items[$(this).attr('rel')] = true;})
	$.post(
		'ajax.php',
		data,
		function(data, status, request)
		{
			if (status != 'success') return alert('Unknown AJAX error: Status is ' + status);
			if (data.status == 0) return alert(data.error);
			if (data.status != 1) return alert('Unknown AJAX error: Status ID is ' + data.status);
			
			$('div.item-selected').animate(
				{opacity: 0},
				4000,
				function()
				{
					$(this).remove();
				}
			);
		},
		'json'
	);
}

function send_images(method, type)
{
	var photos = '';
	
	$('input.select:checked').each(
		function()
		{
			if (photos.length > 0) photos += '&';
			photos += encodeURIComponent($(this).attr('name'));
		}
	);
	
	window.open('download.php?m=' + method + '&t=' + type + '&' + photos, 'ama_preview', 'location=no,status=no,width=625,height=1000,toolbar=no,scrollbars=yes,menubar=no');
}

function download_photos()
{
	var form = $('#search_method')[0];
	
	if (!confirm("Are you sure you want to download these photos? If you select more than one photo, it may take a few minutes to compress them into a downloadable zip file.")) return;
	
	form.action = 'download.php';
	form.submit();
}

function download_comps()
{
	var form = $('#search_method')[0];
	
	if (!confirm("Are you sure you want to download these comps? If you select more than one photo, it may take a few minutes to compress them into a downloadable zip file.")) return;
	
	form.action = 'download.php?w&t=p';
	form.submit();
}