// Adds functionality to search page - 'select all' check boxes, hide and show
// personalisation section and calculate number of pages.

startup = function() {
    $('#id_all_hotels').click(function() { $('.checkbox_hotel').attr('checked', $(this).is(':checked')); });

    $('#id_all_hotels').click(function() { $('.checkbox_hotel').attr('checked', $(this).is(':checked')); });
	$('#id_all_facs').click(function() { $('.checkbox_fac').attr('checked', $(this).is(':checked')); });
	$('#id_all_leisure_facs').click(function() { $('.checkbox_leisure_fac').attr('checked', $(this).is(':checked')); });

	$('#id_layout_dirlisting').click(function() {
        $('#id_all_hotels').attr('checked', true);
        $('.checkbox_hotel').attr('checked', true);
        $('#optional-contents INPUT[type="checkbox"]').attr('checked', false);
        $('#optional-contents INPUT[type="text"]').attr('value', '');
        $('#optional-contents').hide();
    });
	$('#id_layout_glance').click(function() {
        $('#optional-contents INPUT[type="checkbox"]').attr('checked', false);
        $('#optional-contents INPUT[type="text"]').attr('value', '');
        $('#optional-contents').hide();
    });
	$('#id_layout_personalised').click(function() {
        $('#optional-contents').show();
    });

    if ($('#id_layout_personalised').is(':checked')) {
        $('#optional-contents').show();
    }

    $('input').click(function() {
        pages();
    });

    pages();
};

pages = function() {
    var hotels = $('.checkbox_hotel')
    var h = 0;

    $('.checkbox_hotel').filter(':checked').each(function() { h += 1; });

	if (h == 0) {
		$('#pagenum').html('0');
		return;
	}

	if ($('#id_layout_dirlisting').attr('checked')) {
		$('#pagenum').html(Math.round(h * 0.5 + 1));
		return;
	}
	else if ($('#id_layout_glance').attr('checked')) {
		$('#pagenum').html(Math.round(h * 3 + 1));
		return;
	}

	else if ($('#id_layout_personalised').attr('checked')){
		var p = h * 4 + 1;

        $('.checkbox_fac').filter(':checked').each(function() { p = p + h * 0.2; });
        $('.checkbox_leisure_fac').filter(':checked').each(function() { p = p + h * 0.2; });
        $('.checkbox_spec').filter(':checked').each(function() { p = p + h * 0.2; });

		if ($('#id_destination_guide').attr('checked')) {
            p = p + h * 2;
        }

		$('#pagenum').html(Math.round(p));
		return;
	}
};


$(document).ready(function() {
    startup();
})

