$(document).ready(function(){
	// Trip
	
	$('.tab_parent').tabs();		$('.tablink').click(function()	{		if($(this).text() == 'Package') {			$('#Price').show();		}		else {			$('#Price').hide();		}	});
	
	$('.more_links').css('width', '250px');	$('.more_links').css('margin-right', '25px');	
	$("#open_overview").click(function() {		$(".tab_parent").tabs("select", "#Info");	});		$(".special_tab").click(function() {		$('#Price').show();		if (!location.href.match("#Prijzen")) {			location.href = location.href+"#Prijzen";
		}		else {			location.href = location.href;
		}
		return false;	});
		$(".info_link").click(function() {		return false;	});
	
	
	// Compare
	
	$('.remember_acc_link').click(function(){		remember_trip(); //tools.js
		return false;	});
		$('.compare_acc_link').click(function(){		remember_trip(); //tools.js
		return false;	});
		$('.email_acc_link').click(function(){		email_trip(); //tools.js
		return false;	});
	
	
	// Booking Calendar
		$('.person_selector select').change(function() {		update_calendar();	});
		$('.airport_selector input[type="checkbox"]').live('click', function() {		update_calendar();	});
	
	$('.time_selector select').bind('change', update_time_range);
	
	$('.more_booking_dates .more_booking_dates_link').live('click', function() {		update_calendar(true);
		return false;	});
	
	$('.price .price_data').live('mouseover mouseout', function(event) {
		$(this).closest('tr').toggleClass('hover', event.type == 'mouseover');
		$(this).toggleClass('hover', event.type == 'mouseover');
	});		$('.price .price_data').live('click', function()	{		if ($(this).hasClass('status_unavailable')) {			return false;		}
		
		var parent = $(this).closest('.price');		
		var seats_remaining = $.trim($('.availability:eq(0)', this).text());
		seats_remaining = seats_remaining.substr(1);
		seats_remaining = seats_remaining.substr(0, seats_remaining.length - 1);
		
		var url =  window.location.href.replace(/#.*/, "");
				$.ajax({			type: "POST",			url: "/boeken-aanpassen/ajax",			data: {
				action: 'set_booking_data',
				trip_id: $('#booking_calendar input[name="trip_id"]').val(),
				timestamp: $('input[name="timestamp"]', parent).val(),
				duration: $('input[name="duration"]', parent).val(),
				tarief: $('input[name="tarief"]', this).val(),
				tarief_id: $('input[name="tarief_id"]', this).val(),
				arrangement_id: $('input[name="arrangement_id"]', this).val(),
				flight_status: $('input[name="flight_status"]', this).val(),
				num_adults: $('.select_adults').val(),
				num_kids: $('.select_kids').val(),
				num_babies: $('.select_babies').val(),
				seats_remaining: seats_remaining,
				trip_url: url
			},
			beforeSend: function() {
				$('<div class="ajax-loading"></div>').appendTo('#booking_calendar').bgiframe().css({
					width: $('#booking_calendar').innerWidth()+'px',
					height: $('#booking_calendar').innerHeight()+'px'
				});
				
				$('<div class="ajax-loading"></div>').appendTo('#travel_party_box').bgiframe().css({
					width: $('#travel_party_box').innerWidth()+'px',
					height: $('#travel_party_box').innerHeight()+'px'
				});			},			success: function(data) {				window.location = '/boeken-aanpassen';			}		});	});
	
	$('#toggle_availability a').live('click', function()
	{
		var toggle = $('#toggle_availability a .show:visible').length == 1;
		$('.price_calendar .availability', $(this).closest('#booking_calendar')).toggle(toggle);
		$('#toggle_availability a .show').toggle(!toggle);
		$('#toggle_availability a .hide').toggle(toggle);
		return false;
	});});function update_calendar(show_all){	//get trip id	var trip_id = $('#booking_calendar input[name="trip_id"]').val();		//get airports	var str_airports = "";	$('.airport_selector input:checked').each(function() {		str_airports += $(this).val() + ",";	});	str_airports = str_airports.substr(0, str_airports.length - 1);	
	//get time range
	var time_range;
	if (!show_all && $('.time_selector select').length > 0) {
		time_range = $('.time_selector select').eq(0).val();
	}
	else {
		time_range = 'all';
	}
	
	//get num people
	var num_people;
	if ($('select[name="num_kids"]').length > 0)
	{		num_people = 
			parseInt($('select[name="num_adults"]').val()) + 
			parseInt($('select[name="num_kids"]').val());
	}
	else
	{
		num_people = parseInt($('select[name="num_adults"]').val());
	}		$.ajax({		type: "POST",		url: "/trip/ajax",		data: {			action: 'update_calendar',
			trip_id: trip_id,			num_people: num_people,			airports: str_airports,
			time_range: time_range		},		beforeSend: function() {
			$('<div class="ajax-loading"></div>').appendTo('#booking_calendar').bgiframe().css({
				width: $('#booking_calendar').innerWidth()+'px',
				height: $('#booking_calendar').innerHeight()+'px'
			});
			
			$('<div class="ajax-loading"></div>').appendTo('#travel_party_box').bgiframe().css({
				width: $('#travel_party_box').innerWidth()+'px',
				height: $('#travel_party_box').innerHeight()+'px'
			});		},
		error: function() {
			$('#booking_calendar .ajax-loading').remove();
			$('#travel_party_box .ajax-loading').remove();
			update_calendar(show_all);
		},		success: function(html) {			$('#price_calendar_parent').html(html);
			$('#booking_calendar .ajax-loading').remove();
			$('#travel_party_box .ajax-loading').remove();
			
			if ($('#toggle_availability').length == 1) {
				$('.price_calendar .availability', '#booking_calendar').toggle($('#toggle_availability a .show:visible').length != 1);
			}
			
			$('.time_selector select').unbind('change');
			$('.time_selector select').bind('change', update_time_range);		}	});};

var current_city = 0;var current_info = 0;

function show_info_tab(city_id, info_id){	current_city = city_id;	current_info = info_id;	//var id = $(element).find('a').attr('id');	if($('#info_div_' + city_id + '_' + info_id).text() == "")	{		$.ajax({			type: "POST",			url: "info/ajax",			data: "info_id=" + info_id,			success: update_info_tab		});	}	else {		$('#info_div_' + city_id + '_' + info_id).slideToggle();	}	return false;};function update_info_tab(html){	var div = $('#info_div_' + current_city + '_' + current_info);	div.html(html);	div.find(".accommodation_gallery").each(make_accomodation_gallery);	div.slideToggle();};

function update_time_range()
{
	var value = $(this).val();
	$('.time_selector select').not(this).each(function() {
		$(this).val(value);
	});	update_calendar();
};

