$(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 (pane.is(':empty')) pane.load(this.getTabs().eq(i).find('a').attr('href'));
		}
	}).slideshow({
		autopause: true,
		autoplay: true,
		clickable: false,
		interval: 12500
	});

	$('a[rel]').overlay({

		closeOnClick: true,
		top: 'center',
		expose: {
			color: '#164F83',
			loadSpeed: 100,
			opacity: 0.5
		},
		onBeforeLoad: function() {
			var wrap = this.getContent();
			wrap.load(this.getTrigger().attr('href'));
		}
	});


	// Search results toggle
	$('.toggle').toggle(function() {
		$(this).parent().parent().find('.content_pad').show('fast');
		$(this).parent().find('.secondary p').text('Hide departure dates');
		$(this).attr('title', 'Hide departure dates');
	}, function() {
		$(this).parent().parent().find('.content_pad').hide('fast');		
		$(this).parent().find('.secondary p').text('Show departure dates');
		$(this).attr('title', 'Show departure dates');
	});


	// Balance spotlights
	$('.section_spotlights').each(function() {

		var balance_height = 0;
		var max_balance_height = 0;
		$(this).find('.spotlight .heading_pad').each(function() {

			balance_height = $(this).height();
			if (balance_height > max_balance_height) max_balance_height = balance_height;

		});
		$(this).find('.spotlight .heading_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('.spotlight .content_pad').each(function() {

			balance_height = $(this).height();
			if (balance_height > max_balance_height) max_balance_height = balance_height;

		});
		$(this).find('.spotlight .content_pad').each(function() {

			$(this).css('padding-bottom', parseInt($(this).css('padding-bottom').replace('px','')) + max_balance_height - $(this).height());


		});

	})

	//$('.lightbox a').lightBox();


});




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_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;	
			}
		}
		
		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);
		
	});
	
	
}
