$(document).ready(function() {

	$('#section_banner ul').tabs('#panes div', {
		current: 'selected',
		effect: 'fade',
		fadeInSpeed: 750,
		rotate: true,
		tabs: '#section_banner a',
		onBeforeClick: function(event, i) {
			var pane = this.getPanes().eq(i);
			if ( jQuery.trim(pane.text()) == '' ) { 
				pane.load(this.getTabs().eq(i).find('a').attr('href'), function() {
					pane.find('a').unbind().bind('click', function() {
						banner_click();
					});
					pane.find('area').unbind().bind('click', function() {
						banner_click();
					});
				});
			}
		}
	}).slideshow({
		autopause: true,
		autoplay: true,
		clickable: false,
		interval: 8500
	});

	$('.section_spotlights').each(function() {
		var balance_height = 0;
		var max_balance_height = 0;
		$(this).find('.primary .pad').each(function() {
			balance_height = $(this).height();
			if (balance_height > max_balance_height) max_balance_height = balance_height;
		});
		$(this).find('.primary .pad').each(function() {
			$(this).css('padding-bottom', parseInt($(this).css('padding-bottom').replace('px','')) + max_balance_height - $(this).height());
		});

		var balance_height = 0;
		var max_balance_height = 0;
		$(this).find('.secondary .pad').each(function() {
			balance_height = $(this).height();
			if (balance_height > max_balance_height) max_balance_height = balance_height;
		});
		$(this).find('.secondary .pad').each(function() {
			$(this).css('padding-bottom', parseInt($(this).css('padding-bottom').replace('px','')) + max_balance_height - $(this).height());
		});
	})

	if ( jQuery.trim($('#search_query_control').val()) == '' ) {
		$('#search_query_control')
			.focus(function() {
				if ( $('#search_query_control').hasClass('greyed') ) {
					$('#search_query_control')
						.removeClass('greyed')
						.val('');
				}
			})
			.keypress(function() {
				if ( $('#search_query_control').hasClass('greyed') ) {
					$('#search_query_control')
						.removeClass('greyed')
						.val('');
				}
			})
			.addClass('greyed')
			.val('Safari');
	}
	$('#box_secondary_search form').submit(function() {
		if ( $('#search_query_control').hasClass('greyed') && jQuery.trim($('#search_query_control').val()) == 'Safari' ) {
			$('#search_query_control')
				.removeClass('greyed')
				.val('');
		}
	});


	var enquire_count = 0;
	$('#departures').find('.departure').each(function() {
		if ( jQuery.trim($(this).find('.book_enquire').text().toLowerCase()) != 'book now' ) {
			enquire_count++;
		}
	});
	if ( enquire_count == 0 ) {
		$('#toggle_departures').hide();
	}
	$('#toggle_departures').click(function() {
		return show_hide_departures(this);
	});

	$('.lightbox').lightBox({
		fixedNavigation: true,
		imageLoading: '/img/lightbox-ico-loading.gif',
		imageBtnClose: '/img/lightbox-btn-close.gif',
		imageBtnPrev: '/img/lightbox-btn-prev.gif',
		imageBtnNext: '/img/lightbox-btn-next.gif'
	});
});



function updateSearchOptions(destination) {
	var item = $(destination);
	var options = item.attr('options');
	var index = item.attr('selectedIndex');

	jQuery.getJSON('/json.php?call=ExperienceDAO.getExperienceList&destinationID=' + escape(options[index].value), null, function(data) {
		var x;
		var item;
		
		item = document.getElementById('search_type_control');
		if ( item ) {
			if ( data.length != undefined ) {
				item.length = data.length + 1;	
				for ( x = 0; x < data.length; x++ ) {
					item.options[x + 1].value = data[x].experienceID;
					item.options[x + 1].text = data[x].name;
				}
			}	
			else {
				item.length = 1;	
			}
		}

		item = document.getElementById('search_results_filter_experience_control');
		if ( item ) {
			if ( data.length != undefined ) {
				item.length = data.length + 1;	
				for ( x = 0; x < data.length; x++ ) {
					item.options[x + 1].value = data[x].experienceID;
					item.options[x + 1].text = data[x].name;
				}
			}	
			else {
				item.length = 1;	
			}
		}
	});	

	jQuery.getJSON('/json.php?call=DepartureDAO.getDeparturePointList&destinationID=' + escape(options[index].value), null, function(data) {
		var x;
		var item;
		var html;
		
		html = '';
		html += '<select id="search_results_filter_departure_point_control" name="departure_point">';
		html += '<option value="">Departing from...</option>';
		
		for ( x = 0; x < data.length; x++ ) {
			if ( data[x].name.toLowerCase() != 'coach stops' ) {
				html += '<optgroup label="' + data[x].name + '">';
				for ( y = 0; y < data[x].data.length; y++ ) {
					for ( z = 0; z < data[x].data[y].data.length; z++ ) {
						html += '<option value="' + data[x].data[y].data[z].departurePointID + '">' + data[x].data[y].data[z].name + '</option>';
					}
				}
				html += '</optgroup>';
			}
			else {
				html += '<optgroup label="' + data[x].name + '"></optgroup>';
				for ( y = 0; y < data[x].data.length; y++ ) {
					html += '<optgroup label="' + data[x].data[y].name + '" style="padding-left: 20px">';
					for ( z = 0; z < data[x].data[y].data.length; z++ ) {
						html += '<option value="' + data[x].data[y].data[z].departurePointID + '">' + data[x].data[y].data[z].name + '</option>';
					}
					html += '</optgroup>';
				}
			}
		}		
		
		html += '</select>';
		
		item = $('#search_results_filter_departure_point_control');
		item.replaceWith(html);
		
	});
}

			function find_address_list() {
				var item = $('#brochures_by_post_address_postcode_control');
				
				$('#brochures_by_post_address_list_control_block').hide();
				$('#brochures_by_post_address_list_control_loading').show();
				
				jQuery.getJSON('/json.php?call=AddressDAO.getAddressResolveList&postcode=' + escape(item.val()), null, function(data) {
					var select = document.getElementById('brochures_by_post_address_list_control');
					var address;
					
					select.length = data.length + 1;
					select.options[0].value = '';
					select.options[0].text = 'Manually Enter';		
					for ( x = 0; x < data.length; x++ ) {
						
						address = new Array();
						if ( data[x].name ) {
							address[address.length] = data[x].name;
						}
						if ( data[x].address1 ) {
							address[address.length] = data[x].address1;
						}
						if ( data[x].address2 ) {
							address[address.length] = data[x].address2;
						}
						if ( data[x].address3 ) {
							address[address.length] = data[x].address3;
						}
						if ( data[x].town ) {
							address[address.length] = data[x].town;
						}
						if ( data[x].county ) {
							address[address.length] = data[x].county;
						}
						select.options[x + 1].text = address.join(', ');
						select.options[x + 1].value = data[x].index;
					}
			
					$('#brochures_by_post_address_list_control_block').show();
					$('#brochures_by_post_address_list_control_loading').hide();
				});
				
				return false;
			}
			
			function find_address_update(name) {
				var item = $('#brochures_by_post_address_postcode_control');
				var list = $('#brochures_by_post_address_list_control');
			
				if ( list.val() == '' ) {
					$('#brochures_by_post_house_control').attr('readOnly', false);
					$('#brochures_by_post_address_1_control').attr('readOnly', false);
					$('#brochures_by_post_address_2_control').attr('readOnly', false);
					$('#brochures_by_post_address_3_control').attr('readOnly', false);
					$('#brochures_by_post_address_town_city_control').attr('readOnly', false);
					$('#brochures_by_post_address_county_control').attr('readOnly', false);
					$('#brochures_by_post_country_control_temp').attr('readOnly', false);
					
					$('#brochures_by_post_country_control').show();
					$('#brochures_by_post_country_control_temp').hide();
				}
				else {
					jQuery.getJSON('/json.php?call=AddressDAO.getAddressResolveList&postcode=' + escape(item.val()) + '&index=' + escape(list.val()), null, function(data) {
						$('#brochures_by_post_house_control').val(data[0].name);
						$('#brochures_by_post_address_1_control').val(data[0].address1);
						$('#brochures_by_post_address_2_control').val(data[0].address2);
						$('#brochures_by_post_address_3_control').val(data[0].address3);
						$('#brochures_by_post_address_town_city_control').val(data[0].town);
						$('#brochures_by_post_address_county_control').val(data[0].county);
						$('#brochures_by_post_address_postcode_control').val(data[0].postcode);
						$('#brochures_by_post_country_control').val(data[0].country.countryID);
						$('#brochures_by_post_country_control_temp').val(data[0].country.name);
			
						$('#brochures_by_post_house_control').attr('readOnly', true);
						$('#brochures_by_post_address_1_control').attr('readOnly', true);
						$('#brochures_by_post_address_2_control').attr('readOnly', true);
						$('#brochures_by_post_address_3_control').attr('readOnly', true);
						$('#brochures_by_post_address_county_control').attr('readOnly', true);
						$('#brochures_by_post_address_town_city_control').attr('readOnly', true);
						$('#brochures_by_post_country_control_temp').attr('readOnly', true);
			
					$('#brochures_by_post_country_control').hide();
					$('#brochures_by_post_country_control_temp').show();
					});
				}
				
				return false;
			}

function banner_click() {
	var YWATracker = YWA.getTracker("1000103633796");
	YWATracker.setAction(6);
	YWATracker.setCF(19, jQuery.trim($('#section_banner').find('.links').find('.selected').text()));
	YWATracker.submit_action();
}

function link_click(item) {
	var YWATracker = YWA.getTracker("1000103633796");
	YWATracker.setAction(8);
	YWATracker.setCF(19, jQuery.trim($(item).text()));
	YWATracker.submit_action();
		
	return true;
}

function show_hide_departures(item) {
	if ( jQuery.trim($(item).text()).toLowerCase() != 'show all tours' ) {
		$(item).html('Show all tours');
	}
	else {
		$(item).html('Only show tours<br/>bookable online');
	}
	
	$('#departures').find('.departure').each(function() {
		if ( jQuery.trim($(this).find('.book_enquire').text().toLowerCase()) != 'book now' ) {
			$(this).parent().toggle();
		}
	});
	return false;
}

