$(function() {

	/* !Handle Header Image */
	$('#header').click(function(){ document.location = $('#home-link').attr('href'); }).css('cursor', 'pointer').attr('title', '30 euro per persoon per nacht o.b.v. 2 personen per kamer');

	/* !Handle all 'external' links */
	$('a[rel*=external]').addClass('external').click(function() { return !window.open(this.href); });

	/* !Handle all 'pdf' links */
	$('a[href$=.pdf]').addClass('acrobat').click(function() { return !window.open(this.href); });

	/* !Toggle Location select boxes on SearchBoxes */
	var geen_voorkeur = '~~~';
	$('#location_country').change(function() {
		$(':input[name=LOCATION_TYPE][value=country_or_region]').attr('checked', 'checked');
		$('#location_city').val(geen_voorkeur);
		$('#location_value').val($('#location_country').val());
		if ($('#location_country').val() != geen_voorkeur)
		{
			$('#location_name').val($('#location_country option:selected').text());
		}
	});
	$(':input[name=LOCATION_TYPE][value=country_or_region]').click(function() {
		$('#location_city').val(geen_voorkeur);
		$('#location_name').val('');
		$('#location_value').val(geen_voorkeur);
	});
	$('#location_city').change(function() {
		$(':input[name=LOCATION_TYPE][value=city]').attr('checked', 'checked');
		$('#location_country').val(geen_voorkeur);
		$('#location_value').val($('#location_city').val());
		if ($('#location_city').val() != geen_voorkeur)
		{
			$('#location_name').val($('#location_city option:selected').text());
		}
	});
	$(':input[name=LOCATION_TYPE][value=city]').click(function() {
		$('#location_country').val(geen_voorkeur);
		$('#location_value').val(geen_voorkeur);
		$('#location_name').val('');
	});
	$('#homepage_zoekbox,#sidebar_zoekbox').submit(function() {
		$(':input[name=LOCATION_TYPE],:input[name=LOCATION_COUNTRY],:input[name=LOCATION_CITY]').attr('disabled', 'disabled');
	});
	$('#sidebar_zoekbox').each(function() {
		if ($('#location_country').val() != geen_voorkeur)
		{
			$('#location_value').val($('#location_country').val());
			$('#location_name').val($('#location_country option:selected').text());
		}
		if ($('#location_city').val() != geen_voorkeur)
		{
			$('#location_value').val($('#location_city').val());
			$('#location_name').val($('#location_city option:selected').text());
		}
	});
	$('#submit_button').click(function() {
		$(this.form).submit();
	});

	/* !Hotel media - open in Lightbox */
	$('.hotel_media a, a.hotel_image_link').lightBox({
		txtImage       : 'Foto',
		txtOf          : 'van de',
		overlayOpacity : '0.6'
	});
	$('.hotel_media img').hover(function() {
		$(this).stop().animate({'opacity': 1}, {duration: 'fast'}); 
	}, function() { 
		$(this).stop().animate({'opacity': 0.8}, {duration: 'slow'}); 
	}).css('opacity', 0.8);

	/* !Show Social Media Images */
	$('.share img').hover(function() {
		$(this).stop().animate({'opacity': 1}, {duration: 'fast'}); 
	},function() { 
		$(this).stop().animate({'opacity': 0.4}, {duration: 'slow'}); 
	}).click(function(event) {
		_gaq.push(['_trackEvent', 'Social Media Click', $(this).attr('alt'), $(this).parent().attr('href')]);
	}).css('opacity', 0.4);

	/* !Show Google Maps */
	$('#map').each(function() {
		googlemaps_initialize();
	});

	/* !Show Facebook Like Button */
	$('#facebook').each(function() {
		facebook_initialize();
	});

	/* !Toggle Selected Betaalmethode */
	$(':input[name^=debet]').focus(function() {
		$('#betalingsmethode_debet').attr('checked', 'checked');
	});
	$(':input[name^=ideal]').focus(function() {
		$('#betalingsmethode_ideal').attr('checked', 'checked');
	});
	$(':input[name^=creditcard]').focus(function() {
		$('#betalingsmethode_creditcard').attr('checked', 'checked');
	});

	/* !Handle Form Submit */
	$('#orderform').submit(function() {
		return handle_submit([
			{field: 'gender', rule: 'required', message: 'Geslacht is verplicht.'},
			{field: 'initials', rule: 'required', message: 'Voorletters is verplicht.'},
			{field: 'lastname', rule: 'required', message: 'Achternaam is verplicht.'},
			{field: 'streetname', rule: 'required', message: 'Straatnaam is verplicht.'},
			{field: 'streetnumber', rule: 'number', message: 'Huisnummer dient een getal te zijn.'},
			{field: 'postalcode', rule: 'required', message: 'Postcode is verplicht.'},
			{field: 'city', rule: 'required', message: 'Plaats is verplicht.'},
			{field: 'country', rule: 'required', message: 'Land is verplicht.'},
			{field: 'phone', rule: 'required', message: 'Telefoonnummer is verplicht.'},
			{field: 'email', rule: 'required', message: 'Emailadres is verplicht.'},
			{field: 'email_check', rule: 'equals', extra: 'email', message: 'Emailadressen dienen overeen te komen.'},
			{field: 'birthdate_d', rule: 'required', message: 'Geboortedag is verplicht.'},
			{field: 'birthdate_m', rule: 'required', message: 'Geboortemaand is verplicht.'},
			{field: 'birthdate_y', rule: 'required', message: 'Geboortejaar is verplicht.'},
			{field: 'betalingsmethode', rule: 'required', message: 'Betalingsmethode is verplicht.'}
		], 'errors', 'error_container');
	});                                                                                                                                                                                                                      

	/* !Hide Loading Spinner */
	$('#ajax-loader').hide();

	/* !AJAX Bonnetje */
	$('#orderform').delegate('input.sud', 'change', function() {
		update_bonnetje();
	}).each(function() {
		update_bonnetje();
	});

	/* !Trigger Change Event on Click - IE Fix */
	if ($.browser.msie)
	{
		$('select').click(function() {
			$(this).trigger('change');
		});
		$(':checkbox').click(function() {
			$(this).trigger('change');
		});
		$(':radio').click(function() {
			$(this).trigger('blur');
			$(this).trigger('focus');
			$(this).trigger('change');
		});
	}

});

/* !Handle Form, Check Fields and Active Loading Spinner */
function handle_submit(rules, error_list_id, error_list_container)
{
	// Empty / Create errors Array
	var errors = new Array();

	// Loop through Rules
	$.each(rules, function(i, rule)
	{
		// Get the values for requested Fields
		if ($(':input[name=' + rule.field + ']').length > 1)
		{
			// Multiple options: get checked or selected value
			switch($(':input[name=' + rule.field + ']').attr('type'))
			{
				case 'checkbox':
				case 'radio':
					var value = $(':input[name=' + rule.field + ']:checked').val();
					break;
				case 'select-one':
					var value = $(':input[name=' + rule.field + ']:selected').val();
					break;
			}
		}
		else
		{
			// Only one option: get value
			var value = $(':input[name=' + rule.field + ']').val();
		}

		// Check the values for requested Fields
		switch(rule.rule)
		{
			// Is the field required
			case 'required':
				// Check for empty or undefined
				if (value == '' || typeof(value) == 'undefined')
				{
					// Add Error to Error Array
					errors[errors.length] = rule.message;
				}
				break;

			// Is it a Integer
			case 'number':
				if (value == '' || typeof(value) == 'undefined' || !parseInt(value))
				{
					// Add Error to Error Array
					errors[errors.length] = rule.message;
				}
				break;

			// Does it equal a other field
			case 'equals':
				var comparison = $('#' + rule.extra).val();
				if (value == '' || typeof(value) == 'undefined' || comparison != value)
				{
					// Add Error to Error Array
					errors[errors.length] = rule.message;
				}
				break;
		}
	});

	// We have Errors
	if (errors.length)
	{
		if (!$('#' + error_list_id).length)
		{
			$('#' + error_list_container).html('<ul id="errors" style="clear: both; color: red;"></ul>');
		}

		// Empty ErrorList
		$('#' + error_list_id).html('');

		// Append Errors to ErrorList
		$.each(errors, function(i, error_message)
		{
			$('<li>' + error_message + '</li>').appendTo($('#' + error_list_id));
		});

		// Scroll to Top
		$('html, body').animate({scrollTop: 0});

		// Dont't submit the form
		return false;
	}

	// Hide Submit Button
	$('#submit-button').hide();

	// Show Loading Spinner
	$('#ajax-loader').show();

	// Submit the form
	return true;
}

/* !Request Updated Receipt */
function update_bonnetje()
{
	// Replace Receipt with Loading Spinner
	$('#bonnetje').html('<img src="/images/ajax-loader.gif" alt="Uw bonnetje wordt bijgewerkt" style="margin: 10px auto 20px 80px;"/>');

	// Get new Receipt Information
	$.get('/reserveren/ajax_order.html', $('.reservation_data, input:checkbox:checked.sud').serialize(), function(data) {

		// Update Receipt Content
		$('#bonnetje').replaceWith($(data).filter('#bonnetje'));

		// Update Paynent Content
		$('#payment_info').replaceWith($(data).filter('#payment_info'));

	});
}