function showAddOrDeleteRelation(action, entity, id) 
{

	var ac = new AjaxConfigurator();

	$.ajax({
	  	url: ac.url + 'mostrar-anadir-o-eliminar-relacion/',
		type: 'POST',
		data: 'action=' + action + '&id=' + id + '&entity=' + entity,
	 	cache: false,
	  	success: function(json) {
			jsonObj = eval(json)
	    	$('#fancy_div').html(jsonObj.html);
	  	}
	});
		
}

function addOrDeleteRelation(action, entity, id) 
{

	var ac = new AjaxConfigurator();

	$.ajax({
	  	url: ac.url + 'anadir-o-eliminar-relacion/',
		type: 'POST',
		data: 'action=' + action + '&id=' + id + '&entity=' + entity,
	 	cache: false,
	  	success: function(json) {
			$.fn.fancybox.close();
			jsonObj = eval(json);
			$('div#flash-messages').html(jsonObj.fl);
			if(jsonObj.add) {
				$('.relation-add-link').hide();
				$('.relation-delete-link').css('display', 'inline');
			} else {
				$('.relation-add-link').css('display', 'inline');
				$('.relation-delete-link').hide();
			}
	  	}
	});
		
}


function initializeRelationManager()
{

	var checkSet = $('.relation-type');

	checkSet.change(
		function(objEvent) {
			window.location.href = '/zu/mis-relaciones/ver/' + $('input[name=ver]:checked').val() + '/';
		}
	);

	$('.action-button').click(
		function(event) {

			// Primero lo cerramoss todos
			$('ul.action-menu').hide();
			$('button.action-button').removeClass('clicked');

			// Luego abrimos el que toca
			list = $(this).parent().find('ul');
			if(list.css('display') == 'none') {
				$(this).addClass('clicked');
				list.show();
			} else {
				$(this).removeClass('clicked');
				list.hide();
			}
			event.stopPropagation();
		}
	);

	/* 
	 *
	 * También funciona:
	 * 
	$('.action-button').blur(
		function(event) {
			list = $(this).parent().find('ul');
			$(this).removeClass('clicked');
			list.hide();
		}
	);
	*/

	$('body').click(
		function() {
			$('ul.action-menu').hide();
			$('button.action-button').removeClass('clicked');
		}
	);

}

