function PriceGrids() {

    this.bindEventHandlers = function(tableId) {
		var owner = this;	
        jQuery('#departing-from-' + tableId).change(function(e) {
            var selectedOption = jQuery('#' + e.currentTarget.id).val();

            owner.displaySpecifcTableBody(tableId, selectedOption);
        });
    };

    this.displaySpecifcTableBody = function(tableId, selectedOption) {
        jQuery('table#' + tableId).show();
        jQuery('table#' + tableId).children('tbody').hide();
        jQuery('table#' + tableId).children('tbody.airportGroup_' + selectedOption).show();
    };

    this.configureAllGrids = function() {
        var owner = this;

     this.success = function(data) {
            var tableId = jQuery(data).find('table').attr('id');
			
			if (tableId !== undefined) {
			    jQuery('div#preload-' + tableId).replaceWith(data);
			    jQuery('table#' + tableId).show();

				var priceTextInfo = jQuery('#priceTextInfo-' + tableId);

				if (priceTextInfo !== null && priceTextInfo !== undefined && priceTextInfo.val() !== '' && priceTextInfo.length !== 0) {
					jQuery('#' + tableId).after('<p class="price-text">' + priceTextInfo.val() + '</p>');
				}
                
				owner.bindEventHandlers(tableId);
				
				if (tableId.indexOf('_') !== -1) {
					var anyLondonOption = 2;
					owner.displaySpecifcTableBody(tableId, anyLondonOption);
				}
			
			} else {
				var errorDivId = jQuery(data).attr('id');
				jQuery('#preload-' + errorDivId).children('div').replaceWith(data);
			}
        };

        jQuery('div.price-grid').each(function() {
            var id = jQuery(this).attr('id').replace('preload-', '').replace(/-/g, '/');
            jQuery.ajax({
                url: '/holidays/pricegrid/' + id,
                type: 'GET',
                dataType: 'html',
                cache: true,
                data: null,
                success: owner.success
            });
        });
    };
}

jQuery(document).ready(function() {
    var priceGrids = new PriceGrids();
    priceGrids.configureAllGrids();
});
