$(document).ready(function() {

	$('#home-widget .home-widget-tabs a').click(function() {
		$(this).closest('.home-widget-tabs').find('a').removeClass('active');
		$(this).addClass('active');
		$('#home-widget').find('div.home-widget-tab').hide();
		$('#home-widget').find('div.'+$(this).data('tab')).show();
	});
	
	$('#home-widget .home-widget-tab p a.submit').click(function() {
		$(this).closest('form').submit();
	});

	$('#home-widget .home-widget-tab-quote form').submit(function() {
		if (!$('#widget-quote-supplier').val()) {
			alert('You must select a destination and property to quote.');
			return false;
		}
	});

	$('#widget-quote-region').change(function() {
		$.ajax({
			url: '/ajax-home-widget.php',
			dataType: 'json',
			data: {
				request: 'suppliers',
				region: $(this).val()
			},
			success: function(data) {
				var options = '';
				$(data).each(function(i, d) {
					options += '<option value="'+d.supplier_code+'" data-url="'+d.url+'">'+d.supplier_name+'</option>'
				});
				$('#widget-quote-supplier').empty();
				if (options.length) {
					$('#widget-quote-supplier').append('<option value="">Select Property</option>');
					$('#widget-quote-supplier').append(options);
				} else {
					$('#widget-quote-supplier').append('<option value="">Select Destination First</option>');
				}
			}
		});
	});
	$('#widget-specials-region').change(function() {
		$.ajax({
			url: '/ajax-home-widget.php',
			dataType: 'json',
			data: {
				request: 'special_suppliers',
				region: $(this).val()
			},
			success: function(data) {
				var options = '';
				$(data).each(function(i, d) {
					options += '<option value="'+d.supplier_code+'">'+d.supplier_name+'</option>'
				});
				$('#widget-specials-supplier').empty();
				if (options.length) {
					$('#widget-specials-supplier').append('<option value="">All Properties</option>');
					$('#widget-specials-supplier').append(options);
				} else {
					$('#widget-specials-supplier').append('<option value="">Select Destination First</option>');
				}
			}
		});
	});
	
	$('#widget-destinations-search').focus(function() {
		if ($(this).val() == 'Search...') {
			$(this).val('');
		}
	});
	$('#widget-destinations-search').blur(function() {
		if ($(this).val() == '') {
			$(this).val('Search...');
		}
	});
	
	$('#widget-destinations-search').keyup(function() {
		var q = $(this).val();
		$('#widget-destinations-list a').each(function(i, el) {
			if ($(el).text().toLowerCase().match(q.toLowerCase())) {
				$(el).show();
				var regex = new RegExp('('+q+')', 'i');
				$(el).html($(el).text().replace(regex, '<span>$1</span>'));
			} else {
				$(el).html($(el).text());
				$(el).hide();
			}
		});
		$('#widget-destinations-list div h4').each(function(i, el) {
			$(el).closest('div').show();
			if ($(el).closest('div').find('a:visible').length == 0) {
				$(el).closest('div').hide();
			}
			if ($(el).text().toLowerCase().match(q.toLowerCase())) {
				$(el).closest('div').show();
				$(el).closest('div').find('a').show();	
				var regex = new RegExp('('+q+')', 'i');
				$(el).html($(el).text().replace(regex, '<span>$1</span>'));
			} else {
				$(el).html($(el).text());	
			}
		});
	});
	$('#widget-destinations-list a').hover(function() {
		$('#widget-destinations-list a').removeClass('hover');
		$(this).addClass('hover');
		$(this).focus();
	}, function() {
		$(this).removeClass('hover');
	});
	$('#widget-destinations-search').keydown(function(e) {
		if (e.keyCode == 40) { 	// down
			$('#widget-destinations-list a').removeClass('hover');
			$('#widget-destinations-list a:visible:first').focus();
			$('#widget-destinations-list a:visible:first').addClass('hover');
			return false;
		}
	});
	$('#widget-destinations-list a').keydown(function(e) {
		if (e.keyCode == 40) { 	// down
			if ($(this).text() == $('#widget-destinations-list a:visible:last').text()) {
				return false;
			}
			$('#widget-destinations-list a:visible').each(function(i, el) {
				if ($(el).text() == $('#widget-destinations-list a.hover').text()) {
					$('#widget-destinations-list a').removeClass('hover');
					$('#widget-destinations-list a:visible:eq('+(i+1)+')').focus();
					$('#widget-destinations-list a:visible:eq('+(i+1)+')').addClass('hover');	
					return false;
				}
			});
			return false;
		}
		if (e.keyCode == 38) {	// up
			if ($(this).text() == $('#widget-destinations-list a:visible:first').text()) {
				$('#widget-destinations-list a').removeClass('hover');
				$('#widget-destinations-list').scrollTop(0);
				$('#widget-destinations-search').focus();
				return false;
			}
			$('#widget-destinations-list a:visible').each(function(i, el) {
				if ($(el).text() == $('#widget-destinations-list a.hover').text()) {
					$('#widget-destinations-list a').removeClass('hover');
					$('#widget-destinations-list a:visible:eq('+(i-1)+')').focus();
					$('#widget-destinations-list a:visible:eq('+(i-1)+')').addClass('hover');	
					return false;
				}
			});
			return false;
		}
	});
	


	$('#home-widget-flights-depart-date-display').datepicker({
		minDate: 0,
		maxDate: '+2y',
		changeYear: true,
		dateFormat: 'D d M yy',
		altField: '#home-widget-flights-depart-date',
		altFormat: 'yy-mm-dd',
		beforeShow: function() {
			setTimeout(function() { $(".ui-datepicker").css("z-index", 6); }, 10);
		},
		onSelect: function(dateText, inst) {
			oDate = new Date($('#home-widget-flights-depart-date-display').datepicker('getDate'));
			oDate.setTime(oDate.getTime()+(1000*60*60*24));
			$('#home-widget-flights-return-date-display').datepicker('option', 'minDate', oDate);
		}
	});
	$('#home-widget-flights-return-date-display').datepicker({
		minDate: 1,
		maxDate: '+2y',
		changeYear: true,
		dateFormat: 'D d M yy',
		altField: '#home-widget-flights-return-date',
		altFormat: 'yy-mm-dd',
		beforeShow: function() {
			setTimeout(function() { $(".ui-datepicker").css("z-index", 6); }, 10);
		}
	});
	$('#home-widget-flights-depart-date-display').datepicker('setDate', '0');
	$('#home-widget-flights-return-date-display').datepicker('setDate', '+1');
	
	$('#home-widget-flights-return').change(function() {
		if ($(this).val() == '1') {
			$('#home-widget-flights-return-date-display').attr('disabled', '');
		} else {
			$('#home-widget-flights-return-date-display').attr('disabled', 'disabled');
		}
	});

});
