$(function(){
	$('#dialog-lightbox').dialog({
		autoOpen: false,
		modal: true,
		width: 350,
		open: function(event, ui) {
			$('#lightboxLoading').show();
			$('#lightboxAddNew').val('');
			$('#lightboxAddExistingContainer').hide();
			$('#lightboxAddExisting').attr('disabled', 'disabled');
			$('#lightboxAddExisting').children().remove();
			$('#lightboxAddExisting').append($('<option></option>').text('Loading lightboxes...'));
			$('#lightboxAddExistingContainer').show();

			$.ajax({
				type: 'get',
				url: '/lightbox/ajax-get-list/',
				success: function(data){
					if (data.length > 0) {
						$('#lightboxAddExisting').children().remove();
						$('#lightboxAddExisting').append($('<option></option>').text('-- Select a Lightbox --'));
						$.each(data, function(key, value){
							if ($.cookie('addToLightboxSelected') && $.cookie('addToLightboxSelected') == value.id) {
								$('#lightboxAddExisting').append($('<option></option>').attr('value', value.id).attr('selected', 'selected').text(value.title+' ('+value.image_count+')'));
							}
							else {
								$('#lightboxAddExisting').append($('<option></option>').attr('value', value.id).text(value.title+' ('+value.image_count+')'));
							}
						});
						$('#lightboxAddExisting').removeAttr('disabled');
					}
					else {
						$('#lightboxAddExistingContainer').hide();
					}

					$('#lightboxLoading').hide();
				},
				error: function(xhr, ts, et) {
					$('#lightboxAddExisting').children().remove();
					$('#lightboxAddExisting').append($('<option></option>').text('Error loading lightboxes'));
					$('#lightboxLoading').hide();
				},
				dataType: 'json'
			});
		},
		close: function(event, ui) {
		},
		buttons: {
			"Add Image": function(){
				if ($('#lightboxAddExisting').val() == '-- Select a Lightbox --' && $('#lightboxAddNew').val() == '') {
					alert('Please select a lightbox to add this image to or enter a name of a new lightbox');
					return;
				}

				$.ajax({
					type: 'get',
					url: '/lightbox/ajax-add-image/',
					data: {
						lightboxId: $('#lightboxAddExisting').val(),
						lightboxName: $('#lightboxAddNew').val(),
						imageId: $.imageId
					},
					success: function(data){
						//alert(data.code);
						if (data.state == 'noSelectionMade') {
							if ($('#lightboxAddExistingContainer').is(':visible')) {
								alert('No lightbox selected / No new lightbox name provided');
							}
							else {
								alert('No new lightbox name provided');
							}
						}
						else if (data.state == 'imageAlreadyExistsInLightbox') {
							alert('This image already exists in this lightbox');
						}
						else if (data.state == 'invalidLightboxName') {
							alert('Invalid lightbox name (you must provide a name for your lightbox)');
						}
						else if (data.state == 'lightboxNameAlreadyExists') {
							alert('A lightbox by this name already exists');
						}
						else if (data.state == 'success') {
							$('#dialog-lightbox').dialog('close');
						}
						else {
							alert('An error occured adding this image to your lightbox');
						}
					},
					error: function(xhr, ts, et) {
						alert('Failed adding image to your lightbox');
					},
					dataType: 'json'
				});
			},
			"Cancel": function(){
				$(this).dialog('close');
			}
		}
	});

	$('#lightboxAddExisting').change(function(){
		$('#lightboxAddNew').val('');

		$.cookie('addToLightboxSelected', $(this).val(), { path: '/' });
	});

	$('#lightboxAddNew').bind('keyup', function(){
		$('#lightboxAddExisting').val('');
	});
});
