var trip_information = new Array();

function filter_resmal() {
	var filters = resmal_get_filters();
	var limit_result_count = 99999999;
	var matched_rows = 0;
	
	if (!filters['departure_date']) {
		limit_result_count = 10;
	}
	
	$$('table.travel-targets tr.filterable').each(function(table_row) {
		if (resmal_filter_match(table_row, filters) && matched_rows < limit_result_count) {
			table_row.setStyle('display', '');
			matched_rows++;
		} else {
			table_row.setStyle('display', 'none');
		}
	});
	
	if (filters['traveller_count'] && parseInt(filters['traveller_count']) >= 9) {
		$('travellergreaterthan9').setStyle('display', '');
	} else {
		$('travellergreaterthan9').setStyle('display', 'none');
	}
	
	// Hide all trip information
	$$('tr.trip-information-selected').removeClass('trip-information-selected');
	$$('div.destinationinfo').setStyle('display', 'none');
	$('destinationinfo_0').setStyle('display', 'block');
	
	update_all_booking_links();
}

function resmal_get_filters() {
	var filters = new Array();
	var filter_count = 0;
	
	$$('select.resmal-filter-key').each(function(selector) {
		if (selector.selectedIndex > 0 || selector.name == 'traveller_count') {
			filters[selector.name] = selector[selector.selectedIndex].value;
			filter_count++;
		}
	});
	
	return filters;
}

function resmal_filter_match(table_row, filters) {
	// Departure date
	if (filters['departure_date'] && !table_row.getElement('td.departure-month-' + filters['departure_date'])) {
		return false;
	}
	var availability_node = table_row.getElement('td.availability');
	var availability_match = availability_node.className.match(/availability-(\d+)/);
	var availability = availability_match ? parseInt(availability_match[1]) : null;
	// Criteria: don't show trips sooner than 30 days that are completely sold out
	if (availability == 0 && is_date_sooner_than_30_days(table_row)) {
		return false;
	}
	
	// Traveller count
	if (filters['traveller_count']) {
		// >= 9 passengers is not bookable via internet.
		if (parseInt(filters['traveller_count']) >= 9) {
			return false;
		}
		
		if (availability_node) {
			var is_available = (availability >= parseInt(filters['traveller_count']))
			
			set_booking_icon_for_row(table_row, is_available);
			// Criteria: in the "latest-10" list, don't show unavailable trips.
			if (!filters['departure_date'] && !is_available) {
				return false;
			}
		}
	}
	
	// Departure city
	if (filters['departure_city'] && table_row.getElement('td.departure_city') != null && table_row.getElement('td.departure_city').get('text') != filters['departure_city']) {
		return false;
	}
	
	return true;
}

function is_date_sooner_than_30_days(table_row) {
	var date_string = table_row.getElement('td.departure-date').className.match(/departure-date-(\d{4}-\d{2}-\d{2})/)[1];
	var date_parts = date_string.split(/-/);
	var provided_date = new Date(date_parts[0], date_parts[1] - 1, date_parts[2]);
	var date_30_days_from_now = new Date();
	date_30_days_from_now.setDate(date_30_days_from_now.getDate() + 30);
	
	return provided_date < date_30_days_from_now
}

function set_booking_icon_for_row(table_row, is_bookable) {
	table_row.getElement('td.availability span.show-if-available').setStyle('display', is_bookable ? '' : 'none');
	table_row.getElement('td.availability span.show-if-sold-out').setStyle('display', !is_bookable ? '' : 'none');
	
	table_row.removeClass('sold-out');
	if (!is_bookable) {
		table_row.addClass('sold-out');
	}
}

function update_all_booking_links() {
	$$('a.booking-link').each(function(link) {
		link.href = link.href.replace(/traveller_count=\d+/, 'traveller_count=' + $('traveller-count').get('value'));
	});
}



function resmal_more_info(resmal_id) {
	$$('div.resmal-more-info').setStyle('display', 'none');
	$('resmal-' + resmal_id).setStyle('display', 'block');
}

window.addEvent('domready', filter_resmal);
window.addEvent('domready', function() {
	$$('tr.has-trip-information').addEvent('click', function() {
		var target_element = $('destinationinfo_' + this.id.replace('trip-id-', ''));
		if (target_element != null) {
			$$('tr.trip-information-selected').removeClass('trip-information-selected');
			this.addClass('trip-information-selected');
			$$('div.destinationinfo').setStyle('display', 'none');
			target_element.setStyle('display', 'block');
		}
	});
});
