$(function() {
	$("#venue").autocomplete({
		source: "/admin/utils/json/jquery-ui-callbacks.php?list=event_venues",
		minLength: 2
	});
	
	$("#teacher").autocomplete({
		source: "/admin/utils/json/jquery-ui-callbacks.php?list=event_teachers",
		minLength: 2
	});
	
	$("#author").autocomplete({
		source: "/admin/utils/json/jquery-ui-callbacks.php?list=resource_author",
		minLength: 2
	});
	
	$("#type").autocomplete({
		source: "/admin/utils/json/jquery-ui-callbacks.php?list=event_types",
		minLength: 2
	});
	
	$("#classification").autocomplete({
		source: "/admin/utils/json/jquery-ui-callbacks.php?list=event_classifications",
		minLength: 2
	});
	
	// User edit form stuff
	if($('#send_welcome_mail').length){
		$('#send_welcome_mail').change(function(){
			if (this.checked) {
				if($('#contact_person').val() == '') {
					alert('You must set the contact person before you can send the welcome email.');
					this.checked = false;
				}
			} 
		});	
	}
	
	if ($('#password_generate').length){
		$('#password_generate').change(function(){
			if (this.checked) {
				$(this).closest('tr').siblings().find('td').hide();
			} else {
				$(this).closest('tr').siblings().find('td').show();
			} 
		});
		
		// Hide the password boxes if this is ticked when the page loads
		$('#password_generate').trigger('change');
		
	}
	
	// End user edit form
	
	// Activity logging 
	if ($('#activity-log-form').length) {
		$('#activity-log-form').dialog({ autoOpen: false, modal: true, width: 500 });
		$('#activity_id').change(function(){
			if(this.selectedIndex) {
				$('#activity_date').closest('div').fadeIn();
			} else {
				$('#activity_date').closest('div').fadeOut();
				$('#show_activity_date, #activity_date').val('');
			}
		});
		$("#show_activity_date" ).datepicker({
			changeMonth: true,
			changeYear: true,
			altFormat: "yy-mm-dd",
			showAnim: "slideDown",
			dateFormat: "D, d MM, yy",
			altField: "#activity_date"			
		});
		
		$('#add-member-note').click(function() {
			$('#activity-log-form').dialog('open');
			clearForm($('#activity-log-form'));
			return false;
		});		
	}
	// End of activity logging
	
	
	// Activity reports builder
	if ($('#ar-filter-form').length) {
		$('#ar-filter-form').dialog({ autoOpen: false, modal: true, width: 500 });
		var formObj = $('#ar-filter-form').find('form');
		
		$('#add-filter').click(function(){
			$('#filter_type').val('');
			clearForm(formObj);
			$('#ar-filter-form').dialog('open');
		});
		
		$('#filter_type').change(function(){
			clearForm(formObj);
			activeFilter = $(this).val();
			$('#'+getFilterFieldName(activeFilter)).closest('div').fadeIn();
			if (activeFilter == 'keyword') {
				$('#search_fields').closest('div').fadeIn();
			}
			if (activeFilter.length) {
				$('#done').closest('div').fadeIn();
			}
			showDateRange(activeFilter);			
		});
		
		$('#activity').change(function(){
			showDateRange(activeFilter);
		}) 				
		
		
		$('#date_range').change(function(){
			if (this.selectedIndex == (this.options.length-1)) {
				$('#date_from, #date_to').closest('div.hidden').fadeIn();
			} else {
				$('#date_from, #date_to').closest('div.hidden').hide();
			}
		});
		
		var dates = $( "#date_from, #date_to" ).datepicker({
			changeMonth: true,
			changeYear: true,
			altFormat: "yy-mm-dd",
			showAnim: "slideDown",
			dateFormat: "D, d MM, yy",
			onSelect: function( selectedDate ) {
				var option = this.id == "date_from" ? "minDate" : "maxDate",
					instance = $( this ).data( "datepicker" ),
					date = $.datepicker.parseDate(
						instance.settings.dateFormat ||
						$.datepicker._defaults.dateFormat,
						selectedDate, instance.settings );
				dates.not( this ).datepicker( "option", option, date );
			}
		});
		
		$("#date_from, #date_to" ).each(function() {
			$(this).datepicker("option", "altField", "#db_"+this.id);		
		});
		
		$("#done").click(function() {
			saveFilter(this.form);	
		});
		
		$("#done-add").click(function() {
			saveFilter(this.form, false);	
		});
	}		
	// End - Activity reports builder
	
});


$.fn.formToJSON = function()
{
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};


