var imgPath = '/tridion/images/';
//var imgPath = 'images/';

$(function() {

	// booking form initialisation

	// tabbing 
	
	var calOpen;
	
	tabs = $('#bookingForm .tabs li');
	panels = $('#bookingForm .panel');

	$('#bookingForm .tabs li a').click(function() {
	
		if (calOpen) closeCalendarById(calOpen);
	
		tabs.removeClass('selected');
		panels.css({'display':'none'});
		qs = this.href.split('?')[1];
		target = null;
		
		vars = qs.split('&');
		
		$.each(vars,function(i,val) {
		
			if (val.split('=')[0] == 'activeTab') target = val.split('=')[1];
		
		});
		
		if (target != null) {
	
			target = $('#'+target);
			
			$(this).parent().addClass('selected');
			
			if (target.hasClass('panel')) {
				target.show();
			} else {
				target.parents('div.panel').show();
			};
		
		}
		
		return false;
	
	});
	
	// add calendar icons
	
	cal = document.createElement('img');
	cal.src = imgPath + 'new_calendar_tcm4-808153.gif';
	cal.width = '14';
	cal.height = '14';
	cal.className = 'calendar';
	
	$('#bookingForm input.date').after(cal);
	
	$('.calendar').click(function() {
		p = $(this).prev().get(0).id;
		
		switch (p){
			case 'departureDate':
				n = 'returnDate';
				break;
			case 'returnDate':
				n = 'departureDate';
				break;
			case 'departureDateHotel':
				n = 'returnDateHotel';
				break;
			case 'returnDateHotel':
				n = 'departureDateHotel';
				break;
			case 'uscheckin':
				n = 'uscheckout';
				break;
			case 'uscheckout':
				n = 'uscheckin';
				break;
			default:
				n = null;
		}
		
		initElementIds(p,n);
		calOpen = p;
		displayCalendarOnClick(document.getElementById(p),'dd/mm/yy',p,n);
		
	});
	
	// events
	
	$('#bookingForm select').each(function() {
		this.onmousewheel = function() {
			return false;
		}
	});
	
	// give each form (except hotel search) a validation call on submit
	$('#bookingForm form:not(#hotelSearch)').submit(function() {
		return submitTest.check();
	});
	
	// logic for return/one-way field controls
	$('input[name=isreturn]').click(function() {
		isRoundTrip = (this.value == 'yes') ? true : false;
		doRoundTrip(isRoundTrip);
	});		
	
	// update flight form when departure or arrival airport changes
	$('#departurepanel').change(function() {
		obDepChanged();
	});
	$('#arrivalpanel').change(function() {
		obArrivalChanged();
	});
	
	// trigger date pickers on flight form
	$('#departureDate').keyup(function() {
		changeDate(this,'dd/mm/yy');	
	}).click(function() {
		initElementIds('departureDate','returnDate');
		calOpen = 'departureDate';
		displayCalendarOnClick(this,'dd/mm/yy','departureDate','returnDate');
	}).select(function() {
		initElementIds('departureDate','returnDate');
		calOpen = 'departureDate';
		displayCalendar(this,'dd/mm/yy','departureDate','returnDate');
	});
	$('#returnDate').keyup(function() {
		changeDate(this,'dd/mm/yy');
	}).click(function() {
		initElementIds('departureDate','returnDate');
		calOpen = 'returnDate';
		displayCalendarOnClick(this,'dd/mm/yy','returnDate','departureDate');	
	}).select(function() {
		initElementIds('departureDate','returnDate');
		calOpen = 'returnDate';
		displayCalendar(this,'dd/mm/yy','returnDate','departureDate');	
	});
	
	// update flight form when cabin class changes	
	$('#classType').change(function() {
		obCabinChanged();
	});
	
	// update flight form when search type changes
	$('input[name=search_type]').click(function() {
		bookingTypeChanged();
	});
	
	// trigger date pickers on hotel form
	$('#departureDateHotel').keyup(function() {
		changeDate(this,'dd/mm/yy');
	}).click(function() {
		initElementIds('departureDateHotel','returnDateHotel');
		calOpen = 'departDateHotel';
		displayCalendarOnClick(this,'dd/mm/yy','departDateHotel','returnDateHotel',1);	
	}).select(function() {
		initElementIds('departureDateHotel','returnDateHotel');
		calOpen = 'departDateHotel';
		displayCalendar(this,'dd/mm/yy','departDateHotel','returnDateHotel',1);	
	});
	$('#returnDateHotel').keyup(function() {
		changeDate(this,'dd/mm/yy');
	}).click(function() {
		initElementIds('departureDateHotel','returnDateHotel');
		calOpen = 'returnDateHotel';
		displayCalendarOnClick(this,'dd/mm/yy','returnDateHotel','departureDateHotel',1);
	}).select(function() {
		initElementIds('departureDateHotel','returnDateHotel');
		calOpen = 'returnDateHotel';
		displayCalendar(this,'dd/mm/yy','returnDateHotel','departureDateHotel',1);	
	});
	
	// initialise octopus hotel form default date (6 days aheaad) and stay (1 night)
	defaultDelay = 6;
	var stayLength = 1;

	var a = new Date();
	var b = new Date();
	a.setDate(a.getDate()+defaultDelay);

	adjustCheckout(a);

	$('#checkin').val(formatDate(a));	
	$('#nights').val(stayLength);
	
	// only display a checkout date if it can be set (no point showing if JS is off)
	$('#checkout').show();
	
	$('#checkin').blur(function() {

		if (this.value != formatDate(a)) {
		
			temp = this.value.split('/');
			
			a.setDate(temp[0]);
			a.setMonth(temp[1]-1);
			a.setFullYear('20'+temp[2]);

			adjustCheckout(a);
						
		}
		
	});
	
	$('#nights').blur(function() {
	
		if (this.value != stayLength) {
		
			stayLength = parseInt(this.value);			

			adjustCheckout(a);
			
		}
	
	});
	
	
	// trigger date pickers on octopus hotel form - checkin
	$('#bookingForm #checkin').keyup(function() {
		changeDate(this,'dd/mm/yy');
	}).click(function() {
		initElementIds('checkin');
		calOpen = 'checkinHotel';
		displayCalendarOnClick(this,'dd/mm/yy','checkinHotel',null,1);	
	}).select(function() {
		initElementIds('checkin');
		calOpen = 'checkinHotel';
		displayCalendar(this,'dd/mm/yy','checkinHotel',null,1);	
	});	


	// helper functions for octopus hotel dates
	
	// format date to dd/mm/yy
	function formatDate(date) {	
		return pad(date.getDate()) + date.getDate() + '/' + pad((date.getMonth()+1)) + (date.getMonth()+1) + '/' + date.getFullYear().toString().substr(2);				
	}

	// pad a leading zero onto day or month if < 10
	function pad(val) {
		p = (val.toString().length == 1) ? '0' : '';			
		return p;
	}
	
	// calculate and display checkout date when checkin date or length of stay changes
	function adjustCheckout(date) {
		b.setTime(date.valueOf());			
		b.setDate(b.getDate()+stayLength);
	
		$('#checkout span').html(formatDate(b));	
	}
	
	// trigger date pickers on US hotel form  - in / out
	$('#in').keyup(function() {
		changeDate(this,'dd/mm/yy');	
	}).click(function() {
		initElementIds('in','out');
		calOpen = 'in';
		displayCalendarOnClick(this,'dd/mm/yy','in','out');
	}).select(function() {
		initElementIds('in','out');
		calOpen = 'in';
		displayCalendar(this,'dd/mm/yy','in','out');
	});
	$('#out').keyup(function() {
		changeDate(this,'dd/mm/yy');
	}).click(function() {
		initElementIds('in','out');
		calOpen = 'out';
		displayCalendarOnClick(this,'dd/mm/yy','out','in');	
	}).select(function() {
		initElementIds('in','out');
		calOpen = 'out';
		displayCalendar(this,'dd/mm/yy','out','in');	
	});

	// helper function for US hotel rooms
		
	roomItems = $('.hotelSearchUS .room');

	// get current selected rooms (allowing for select persistance on reload etc)
	roomsOnLoad = $('.hotelSearchUS select#rooms').val();

	//hide person-per-room units for unselected rooms (i.e. higher values than the rooms select is set to)
	roomItems.slice(roomsOnLoad).hide();	
	
	// display appropriate number of person-per-room units when number of rooms changes	
	$('.hotelSearchUS select#rooms').change(function() {
		roomItems.hide().slice(0,this.value).show();	
	});
	
	

});

//ie enter does nto work where we have display:none at load time.

$(function(){
    $('input').keydown(function(e){
        if (e.keyCode == 13) {
            $(this).parents('form').submit();
            return false;
        }
    });
});



