// jQuery code to control form display and widgets

$(document).ready(function(){
						   
						   
	//set up the booking form calendars
	
	$("#frm-book-depart-month").after('<a id="flyoutCalendarDepartLink" href="#none"><img src="/media/aerlinguscom/styleassets/images/booking-engine/btn-calendar.gif" width="25" height="20" alt="Select dates" /></a>');
	$("#frm-book-return-month").after('<a id="flyoutCalendarReturnLink" href="#none"><img src="/media/aerlinguscom/styleassets/images/booking-engine/btn-calendar.gif" width="25" height="20" alt="Select dates" /></a>');
	
	$("#page-container").append('<div id="flyoutCalendarDepart" class="flyout calendar"><div class="tl"><div class="tr"><h2>Departure date</h2></div></div><div class="ml"><div class="mr"><div id="calendar-container-depart"></div></div></div><div class="bl"><div class="br"></div></div></div>');
	
	$("#page-container").append('<div id="flyoutCalendarReturn" class="flyout calendar"><div class="tl"><div class="tr"><h2>Return date</h2></div></div><div class="ml"><div class="mr"><div id="calendar-container-return" class="return"></div></div></div><div class="bl"><div class="br"></div></div></div>');
	
	$('#flyoutCalendarDepart').emc_flyout( { flyoutLink:$('#flyoutCalendarDepartLink') } );
	$('#flyoutCalendarReturn').emc_flyout( { flyoutLink:$('#flyoutCalendarReturnLink') } );
	
	var epoch_calendar;
	
	$('#flyoutCalendarDepartLink').click(function() {
		//create a new instance of the calendar
		epoch_calendar = null;
		$('#calendar-container-depart,#calendar-container-return').empty();
		epoch_calendar = new Epoch('epoch_basic','flat',document.getElementById('calendar-container-depart'),false,'frm-book-depart-day','frm-book-depart-month');
				
		//set it's date to match the fields
		var selectedMonth = $('#frm-book-depart-month').val();
		selectedMonthArray = selectedMonth.split('-');
		selectedMonth = selectedMonthArray[0] - 1;
		var selectedYear = '20' + selectedMonthArray[1];
		var selectedDay = $('#frm-book-depart-day').val();	
		var activeDates = new Array(new Date(selectedYear,selectedMonth,selectedDay));
		epoch_calendar.addDates(activeDates);
		epoch_calendar.goToMonth(selectedYear,selectedMonth);
		fixFlyout($('#flyoutCalendarDepart'));
		return false;
	})
	
	$('#flyoutCalendarReturnLink').click(function() {
		//create a new instance of the calendar
		epoch_calendar = null;
		$('#calendar-container-depart,#calendar-container-return').empty();
		epoch_calendar = new Epoch('epoch_basic','flat',document.getElementById('calendar-container-return'),false,'frm-book-return-day','frm-book-return-month');
				
		//set it's date to match the fields
		var selectedMonth = $('#frm-book-return-month').val();
		selectedMonthArray = selectedMonth.split('-');
		selectedMonth = selectedMonthArray[0] - 1;
		var selectedYear = '20' + selectedMonthArray[1];
		var selectedDay = $('#frm-book-return-day').val();	
		var activeDates = new Array(new Date(selectedYear,selectedMonth,selectedDay));
		epoch_calendar.addDates(activeDates);
		epoch_calendar.goToMonth(selectedYear,selectedMonth);
		fixFlyout($('#flyoutCalendarReturn'));
		return false;
	})
	
	// end Calendar
	
	
	//$(".col-1.airport").after('<div class="flyout predictive"><div class="tl"><div class="tr"></div></div><div class="ml"><div class="mr"></div></div><div class="bl"><div class="br"></div></div></div>');
	
		
	/*
		add the location flyout and links to the DOM
	*/
	
	
	$('body').emc_flyoutLocations({ 
		flyoutLinks : {
			0 : {
				textField : 'frm-book-depart',
				flyoutTitle : 'Travelling from'
			},
			1 : {
				textField : 'frm-book-destination',
				partnerField : 'frm-book-depart',
				flyoutTitle : 'Travelling to'
			},
			2 : {
				textField : 'frm-checkin-depart',
				flyoutTitle : 'Travelling from'
			},
			3 : {
				textField : 'frm-checkin-destination',
				partnerField : 'frm-checkin-depart',
				flyoutTitle : 'Travelling to'
			},
			4 : {
				textField : 'frm-manage-depart',
				flyoutTitle : 'Travelling from'
			},
			5 : {
				textField : 'frm-manage-destination',
				partnerField : 'frm-manage-depart',
				flyoutTitle : 'Travelling to'
			},
			6 : {
				textField : 'frm-wherefly-departure',
				flyoutTitle : 'Travelling from'
			}
		}									  
	});
	
	/*
	$('.flyout.location .tr').width($('.flyout.location').width() - 6);
	$('.flyout.location .br').width($('.flyout.location').width() - 6);
	$('.flyout.location.destination .tr').width($('.flyout.location.destination').width() - 6);
	$('.flyout.location.destination .br').width($('.flyout.location.destination').width() - 6);
	*/
	
	//show the return fields
	$("#frm-book-return").bind(
		'focus',
		function() {
			$("#frm-book-return-box").show();
			/*
			$("#frm-book-destination").show();
			$("#frm-book-destination").removeClass('optional');
			$("#frm-destination-picker").show();
			$(".airport span").show();
			*/
		}
	);
	//hide the return fields
	$("#frm-book-one-way").bind(
		'focus',
		function() {
			$("#frm-book-return-box").hide();
			/*
			$("#frm-book-destination").hide();
			$("#frm-book-destination").addClass('optional');
			$("#frm-book-destination-picker").hide();
			$(".airport span").hide();
			*/
		}
	);
	
	/* set up airport locations predictive text */
	
	//book your flight
	$('#frm-book-depart').emc_predictiveText();
	$('#frm-book-destination').emc_predictiveText({ partner: 'frm-book-depart' });
	//web checkin
	$('#frm-checkin-depart').emc_predictiveText();
	$('#frm-checkin-destination').emc_predictiveText({ partner: 'frm-checkin-depart' });
	//manage your booking
	$('#frm-manage-depart').emc_predictiveText();
	$('#frm-manage-destination').emc_predictiveText({ partner: 'frm-manage-depart' });
	//where we fly
	$('#frm-wherefly-departure').emc_predictiveText();
	
	
	
	$('.flyout.location a').mousedown(function() {
		var location = $(this).text();
		$('.location').hide();
		return false;
	});		
	
	$('.date a img').css({ display: 'inline'});
	$('.airport a img').css({ display: 'inline'});
});
