jQuery(document).ready(function($) {
	$('input.route-planner')
		.click(function() {
			if (!$(this).is('.active')) {
				$(this)
					.addClass('active')
					.val('');
			}
		})
		/*
		.blur(function(){
			$(this)
				.removeClass('active')
				.val('Type je vertrekpunt...');
		})
		*/
	;
	$('.date-pick-month')
		.datePicker()
		.bind(
			'dateSelected',
			function(e, selectedDate, $td)
			{
				console.log('You selected ' + selectedDate);
			}
		);
	;
	$('.date-pick').datePicker();
	$('.time-pick').timeEntry({timeSteps: [1, 15, 0]});
	$('.collapse-body')
		.click(function() {
			$(this)
				.toggleClass('collapsed')
				.siblings()
					.toggle();
		})
		.toggleClass('collapsed')
		.siblings().hide();
	$('.subscription-type-selection')
		.click(function() {
			$('#subscription-type-selector')
				.empty()
				.append($(this).html())
				.toggleClass('collapsed')
				.siblings()
					.hide()
			;
			$('#subscription-type').attr('value', $(this).find('.subscription-uid').text());
		})
		.hover(
			function(){
				$(this).addClass('hover');
			},
			function(){
				$(this).removeClass('hover');
			}
		)
	;
	selectParkings();
	$('#city-selector').change(selectParkings);
	showCars();
	$('#subscription-count-selector').change(showCars);
});
function selectParkings(){
	try {
		var selectedCity = jQuery('#city-selector :selected').val();
		if (selectedCity) {
			var selectedParking = jQuery('#parking-selector :selected').val();
			jQuery('#parking-selector').empty();
			for (var c in parking_lookup) {
				if (selectedCity == parking_lookup[c].name) {
					for (p in parking_lookup[c].parkings) {
						var opt = jQuery('<option>')
							.val(parking_lookup[c].parkings[p].uid)
							.text(parking_lookup[c].parkings[p].name);
						// this doesn't seem to be enough to make the matching option selected....
						if (selectedParking == parking_lookup[c].parkings[p].uid) {
							opt.attr('selected', 'selected');
						}
						jQuery('#parking-selector').append(opt);
					}
					break;
				}
			}
		}
	} catch(e) {
	}
}
function showCars() {
	var countSelector = jQuery('#subscription-count-selector')
	if (countSelector) {
		var count = countSelector.val();
		jQuery('.depends-on-count').each(function(i, val) {
			if (i/2 < count) {
				jQuery(this).show();
			} else {
				jQuery(this).hide();
			}
		});
	}
}
