
function refresh_hc(element) {
	// hide cycling region if not ENGLAND
	if ($('#hc option:selected').text() == 'England') {
		toggle_field($('#region_id'), true);
	} else {
		toggle_field($('#region_id'), false);
		return true; // No need to update the options list as it's not visible anyway
	}
	
	// element is only defined when the 'change' event is fired
	if (element) {
		var hc_val = $('#hc').val();
		var reg_select = new Array();
		
		$('#region_id').find('option').remove();
		
		reg_select.push('<option value="">Region (any)</option>');
		for (var i in hcr) {
			if (hcr[i].hc_id == hc_val || hc_val == '') {
				reg_select.push('<option value="' + hcr[i].r_id + '">' + hcr[i].r_name + '</option>');
			}
		}
		
		$('#region_id').html(reg_select.join(''));
		toggle_field($('#region_id'), true);
	}
	
}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* update filter options so that only those relevant to selected event types are shown */
function refresh_event_type(element) {
	if ($("input#eventtype-sport").attr('checked')) {
		toggle_field($("select#zuv_bc_discipline_filter_id"), true);
		
		if ($("select#zuv_bc_discipline_filter_id").val() != '') {
			toggle_field($("select#zuv_bc_race_category_id"), true);
			toggle_field($("select#zuv_bc_licence_discipline_id"), true);
		} else {
			toggle_field($("select#zuv_bc_race_category_id"), false);
			toggle_field($("select#zuv_bc_licence_discipline_id"), false);
		}
	} else {
		toggle_field($("select#zuv_bc_discipline_filter_id"), false);
		toggle_field($("select#zuv_bc_race_category_id"), false);
		toggle_field($("select#zuv_bc_licence_discipline_id"), false);
	}
}

function refresh_discipline(element) {
	if ($('#zuv_bc_discipline_filter_id').val() != '') {
		// if (element) {
			// clear out values
			$('select#zuv_bc_race_category_id').find('option').remove().end().append('<option value="">(loading options...)</option>').val('');
			$('select#zuv_bc_licence_discipline_id').find('option').remove().end().append('<option value="">(loading options...)</option>').val('');
			
			// populate
			$.getJSON(
				"/zuvvi/ext/component/bc/com_bc_eventfinder/ajax/ccoptions.php",
				{ "discipline" : $("select#zuv_bc_discipline_filter_id").val() },
				function (json) {
					// populate selects with response
					if (json.classifications) {
						$('select#zuv_bc_licence_discipline_id').find('option').remove().end().append('<option value="">Rider Category (any)</option>').val('');
						if (json.classifications.length == 0) {
							toggle_field($('select#zuv_bc_licence_discipline_id'), false);
						} else {
							for (var k in json.classifications) {
								$('select#zuv_bc_licence_discipline_id').append('<option value="'+k+'">'+json.classifications[k]+'</option>');
							}
							toggle_field($("select#zuv_bc_licence_discipline_id"), true);
						}
					}
					
					if (json.categories) {
						$('select#zuv_bc_race_category_id').find('option').remove().end().append('<option value="">Event/Race Classification (any)</option>').val('');
						if (json.categories.length == 0) {
							toggle_field($('select#zuv_bc_race_category_id'), false);
						} else {
							for (var k in json.categories) {
								$('select#zuv_bc_race_category_id').append('<option value="'+k+'">'+json.categories[k]+'</option>');
							}
							toggle_field($("select#zuv_bc_race_category_id"), true);
						}
					}
				}
			);
		// } else {
			// toggle_field($("select#zuv_bc_race_category_id"), true);
			// toggle_field($("select#zuv_bc_licence_discipline_id"), true);
		// }
	} else {
		toggle_field($("select#zuv_bc_race_category_id"), false);
		toggle_field($("select#zuv_bc_licence_discipline_id"), false);
	}
}

function toggle_field(field, show) {
	// var dd = field.parent();
	// var dt = dd.prev('dt');
	if (show) {
		// dt.show();
		// dd.show();
		field.removeAttr('disabled');
	} else {
		// dt.hide();
		// dd.hide();
		field.val('');
		field.attr('disabled', 'disabled');
	}
	
	// refresh_selectmenu(field);
}

/**
 * Manually refresh a selectmenu object after dynamically modifying it
 */
function refresh_selectmenu(select) {
	$(select).change();
	return;
	select = $(select);
	select.selectmenu('destroy'); // Without this, the following width setting will not take effect, but keeping this, we get a 'setting a property that has only a getter' javascript exception
	select.selectmenu({ width: '220px' });
	
	// The following would be required if the 'destroy' call wasn't called
	// if (select.attr('disabled')) {
		// select.selectmenu('disable');
	// } else {
		// select.selectmenu('enable');
	// }
}

function init_bookmark_link() {
	$('a.bookmark').click(function () {
		var match = $(this).attr('href').match(/^(.+)(un)?bookmark=(\d+)$/);
		if (match) {
			var url = match[1];
			var event_id = match[3];
		}
		
		if ($(this).hasClass('bookmarked')) {
			$.post('/events/ajax_unbookmark', {
				unbookmark: event_id
			});
			
			// Update url/class
			$(this).attr('href', url + 'bookmark=' + event_id);
			$(this).removeClass('bookmarked');
			
			// Update the text
			if ($(this).html() == 'Event bookmarked') {
				$(this).html('Bookmark this event').attr('title', 'Click to remove from my events');
			} else {
				$(this).html($(this).html().replace('unbookmark', 'bookmark')).attr('title', 'Click to add to my events');
			}
		} else {
			$.post('/events/ajax_bookmark', {
				bookmark: event_id
			});
			
			// Update url/class
			$(this).attr('href', url + 'unbookmark=' + event_id);
			$(this).addClass('bookmarked');
			
			// Update the text
			if ($(this).html() == 'Bookmark this event') {
				$(this).html('Event bookmarked').attr('title', 'Click to add to my events');
			} else {
				$(this).html($(this).html().replace('bookmark', 'unbookmark')).attr('title', 'Click to remove from my events');
			}
		}
		
		init_help_bubble();
		return false;
	});
}

function init_show_more_races() {
	$('.show_races_extended').live('click', function() {
		
		$(this).parents('tbody').children('.races_extended').show();
		$(this).hide();
		
		return false;
	});
}

function init_show_event_races() {
	$('.show_event_races').live('click', function() {
		var race_table_container_id = $(this).attr('id').replace('show-', '');
		
		var link = this;
		var loaded = $.data(this, 'loaded');		
		
		if ($(this).html() == 'View races' || $(this).html() == 'Hide races') {
			$(this).html($(this).html() == 'View races' ? 'Hide races' : 'View races');
		}
		else if ($(this).html() == 'More info' || $(this).html() == 'Hide') {
			$(this).html($(this).html() == 'More info' ? 'Hide' : 'More info');
		}
		
		
		if (!loaded) { // only load data once	
			
			$('#'+race_table_container_id).html('<div class="loading-indicator-bar"></div><p></p>').show();
			var event_id = race_table_container_id.replace('event-races-table-', '');
			
			$.get('/events/ajax_event_races_table', {id: event_id, action_name: $('#action_name').val()}, function (response) {
				$.data(link, 'loaded', true);				
				$('#'+race_table_container_id).html(response);
				
				if (typeof init_trim_tables == 'function') {				
					init_trim_tables();
				}
			});
			
			
		}
		
		return false;
	});
}




$(document).ready(function(){
	// When home country is changed, we need to update the list of selectable regions
	$("#hc").change(refresh_hc);
	refresh_hc();
	
	// update which form elements to show depending on checkboxes
	// $("input#eventtype-sport").change(refresh_event_type);
	// $("input#eventtype-education").change(refresh_event_type);
	// $("input#eventtype-recreation").change(refresh_event_type);
	// refresh_event_type();
	
	$("select#zuv_bc_discipline_filter_id").change(refresh_discipline);
	//refresh_discipline();
	
	// Initialise bookmark/unbookmark links
	init_bookmark_link();
	
	$('.in-field-labels label').inFieldLabels();
	
	init_show_more_races();
	init_show_event_races();	
});

