$(document).ready(function() {
	// Add a special select to the supervisors selection box
	if( $('select[multiple].alternative.supervisors').length ) {
		$('select[multiple].alternative.supervisors').asmSelect({
			addItemTarget: 'bottom',
			animate: true,
			removeLabel: '<img src="/img/_system/delete.gif" alt="Remove" />',
			onClick: "$('#radio_'+optionId).attr('checked','checked');",
			extraContent: '$("<input type=\'radio\'></input>").attr(\'id\', \'radio_\'+optionId ).attr(\'value\', $O.val() ).attr(\'name\', \'data[Supervisors][lead][\' + $O.val() + \']\').attr(\'checked\', $O.val() == lead ? \'checked\' : \'\' )'
		});
	}

	// Add a special select to the student selection box
	if( $('select[multiple].alternative.students').length ) {
		$('select[multiple].alternative.students').asmSelect({
			addItemTarget: 'bottom',
			animate: true,
			removeLabel: '<img src="/img/_system/delete.gif" alt="Remove" />',
			onClick: "$('#check_'+optionId).attr('checked',!$('#check_'+optionId).attr('checked'));",
			extraContent: '$("<input type=\'checkbox\'></input>").attr(\'id\', \'check_\'+optionId ).attr(\'value\', $O.val() ).attr(\'name\', \'data[Students][lead][\' + $O.val() + \']\').attr(\'checked\', jQuery.inArray( parseInt( $O.val() ), lead_for ) >= 0 ? \'checked\' : \'\' )'
		});
	}

	// Add a special select to the member selection box
	if( $('select[multiple].alternative.members').length ) {
		$('select[multiple].alternative.members').asmSelect({
			addItemTarget: 'bottom',
			animate: true,
			removeLabel: '<img src="/img/_system/delete.gif" alt="Remove" />',
			extraContent: '$("<label></label>").attr(\'for\', \'role_\'+optionId ).append(\'Role\').append($("<input type=\'text\'></input>").attr(\'id\', \'role_\'+optionId ).attr(\'name\', \'data[Members][role][\'+ $O.val() +\']\').attr(\'value\', roles[$O.val()] ? roles[$O.val()] : \'\' ))',
			extraContentPosition: 'after'
		});
	}

	// Add a special select to the group selection box
	if( $('.people select[multiple].alternative.groups').length ) {
		$('select[multiple].alternative.groups').asmSelect({
			addItemTarget: 'bottom',
			animate: true,
			removeLabel: '<img src="/img/_system/delete.gif" alt="Remove" />',
			extraContent: '$("<label></label>").attr(\'for\', \'role_\'+optionId ).append(\'Role\').append($("<input type=\'text\'></input>").attr(\'id\', \'role_\'+optionId ).attr(\'name\', \'data[Groups][\' + $O.val() + \'][GroupsPerson][role]\').attr(\'value\', roles[$O.val()] ? roles[$O.val()] : \'\' ))',
			extraContentPosition: 'after'
		});
	}

	// Add a special select to the group selection box
	if( $('.events select[multiple].alternative.groups').length ) {
		$('select[multiple].alternative.groups').asmSelect({
			addItemTarget: 'bottom',
			animate: true,
			removeLabel: '<img src="/img/_system/delete.gif" alt="Remove" />'
		});
	}


	// Add an event to reveal/hide the appropriate students/supervisors form depending
	// on the value of the audience
	$("input[name='data[Person][audience_id]']").change( function() {
		audience = $("input[name='data[Person][audience_id]']:checked + label").html();
		if( audience == 'Academic Staff' || audience == 'Research Staff' ) {
			$('#supervisors').hide();
			$('#supervising').show();
		}
		else if( audience == 'Postgraduate' ) {
			$('#supervisors').show();
			$('#supervising').hide();
		}
		else {
			$('#supervisors').hide();
			$('#supervising').hide();			
		}
	});

	$("input[name='data[Person][audience_id]']").trigger('change');
	
	// Add in a handler for administration index deletes
	$(".admin a.delete").click( function(e) {
		if( confirm('Are you sure you want to delete this record?') ) {
			$.post( e.currentTarget.href );
			$(e.currentTarget).closest('tr').remove();
		}
	});
	
	// Deal with any tabs
	if( $(".tabs").length ) {
	  $(".tabs").tabs();
	}
})
