$(document).ready(function() {
	// General, used across all pages

	// AJAX style bits
	$('a.confirm, a.gohome, a.gohome-admin, a.reload, a.silent').live('click', function() {
		if ($(this).hasClass('confirm') && $(this).hasClass('reload') && $(this).hasClass('silent')) {
			if (confirm($(this).attr('title') + '?')) {
				$.get($(this).attr('href'), function(data) {
					self.location.reload(true);
				});
			}

			return false; // Don't follow the link
		} else if ($(this).hasClass('confirm') && $(this).hasClass('gohome') && $(this).hasClass('silent')) {
			if (confirm($(this).attr('title') + '?')) {
				$.get($(this).attr('href'), function(data) {
					window.location = '/';
				});
			}

			return false; // Don't follow the link
		} else if ($(this).hasClass('confirm') && $(this).hasClass('gohome-admin') && $(this).hasClass('silent')) {
			if (confirm($(this).attr('title') + '?')) {
				$.get($(this).attr('href'), function(data) {
					window.location = '/admin';
				});
			}

			return false; // Don't follow the link
		} else if ($(this).hasClass('reload') && $(this).hasClass('silent')) {
			$.get($(this).attr('href'), function(data) {
				self.location.reload(true);
			});

			return false; // Don't follow the link
		} else if ($(this).hasClass('confirm')) {
			if (confirm($(this).attr('title') + '?')) {
				return true; // Follow the link
			}

			return false; // Don't follow the link
		} else if ($(this).hasClass('silent')) {
			$.get($(this).attr('href'), function(data) {
			});

			return false; // Don't follow the link
		}
	});

	// WYSIWYG form elements
	$('.input.textarea.wysiwyg textarea').tinymce({
		convert_urls : false,
		script_url : '/js/tinymce/jscripts/tiny_mce/tiny_mce.js',
		theme : 'advanced',
		theme_advanced_toolbar_location : 'top'
	});

	// Fade out the flash message
	$('#flash_message_inner').append('<a href="#" class="close">Close</a>');
	
	$('#flash_message_outer').hide();
	$('#flash_message_inner').parent().fadeIn(500);	
	
	$('#flash_message_inner a.close').click(function() {
		$('#flash_message_outer').animate({height: 0}, 300, 'swing');
		$('#flash_message_outer').animate({borderBottom: '0'}, 300, 'swing');
		
		setTimeout(function() {
			$('#flash_message_outer').css('border', 'none');
		}, 310);
		
		return false;
	});	

	// Specific to individual pages

	// Toggling things
	$('#kid_add_cloak:not(.show)').hide();

	$('#kid_add_toggle').toggle(function() {
		$('#main .submit input').val('Add');
		$('#kid_add_cloak').slideDown(500);
		return false;
	}, function() {
		$('#main .submit input').val('Next');
		$('#kid_add_cloak').slideUp(500);
		return false;
	});

	// Enable the hierarchial selection of how the parent heard about Barracudas
	// There's either 0 or 1 of these, so it really should be an if, not an each
	$('select#CustomerRefererCategoryId').each(function() {
		$.get('/referers/list_by_referer_category/' + $(this).val() + '/' + $('select#CustomerRefererId').val(), function(referers) {
			$('select#CustomerRefererId').html(referers);
		});
	});

	$('select#CustomerRefererCategoryId').change(function() {
		$.get('/referers/list_by_referer_category/' + $(this).val() + '/' + $('select#CustomerRefererId').val(), function(referers) {
			$('select#CustomerRefererId').html(referers);
		});
	});

	// Update the list of camps once a season has been selected.
	function updateCamps(selectObject, seasonID) {
		options = '<option value="">Select a camp</option>';

		$.each(seasonalCampsForSidebar[seasonID], function(campID, campName) {
			options += '<option value="' + campID + '">' + campName + '</option>';
		});

		selectObject.html(options);
		selectObject.val('');
	}

	// On page load
	$('form[action="/camps/check_availability"] input[name="data[Season][id]"][checked="checked"]').each(function() {
		seasonID = $(this).val();
		updateCamps($('#CampId'), seasonID);
	});

	$('form[action="/camps/book"] input[name="data[Booking][season_id]"][checked="checked"]').each(function() {
		seasonID = $(this).val();
		updateCamps($('#BookingCampId'), seasonID);
	});

	// On click
	$('form[action="/camps/check_availability"] input[name="data[Season][id]"]').click(function() {
		seasonID = $(this).val();
		updateCamps($('#CampId'), seasonID);
	});

	$('form[action="/camps/book"] input[name="data[Booking][season_id]"]').click(function() {
		seasonID = $(this).val();
		updateCamps($('#BookingCampId'), seasonID);
	});

	// PayPal doesn't allow EPP
	$('body.customers.select_payment div#card_numbers input').click(function() {
		if ($(this).val() == 'P') {
			$('#BookingEpp').attr('disabled', true);
			$('#BookingEpp').removeAttr('checked');
		} else {
			$('#BookingEpp').attr('disabled', false);
		}
	});

	// EPP doesn't allow contacting first
	$('div.input.checkbox.epp input').click(function() {
		if ($(this).attr('checked') == true) {
			$('#BookingRequiresConsent').attr('disabled', true);
			$('#BookingRequiresConsent').removeAttr('checked');
			$('#BookingRequiresConsent').before('<span class="requires_consent_disabled"></span>');
		} else {
			$('#BookingRequiresConsent').attr('disabled', false);
			$('.requires_consent_disabled').remove();
		}
	});

	// EPP doesn't allow contacting first
	$('span.requires_consent_disabled').live('click', function() {
		alert('Not applicable with the Easy Payment Plan. If paying in interest free instalments, your card will be debited automatically on the first working day of each month.');
	});
	
	// Newsletter Signup Form
	
	var fake_label = 'Enter your e-mail address';
	var email_input = $('#SubscriberEmailAddress');
	
	email_input.focus(function() { 
		if ($(this).val() == fake_label) {
			$(this).val('').removeClass('labelled');
		}
	});
	
	email_input.blur(function() { 
		if ($(this).val() == '' || $(this).val() == fake_label) { 
			$(this).addClass('labelled')
			$(this).val(fake_label);
		}
	});
	
	// Init Newsletter Signup Form
	email_input.blur();
	
});

