function show_info(){
	var row = $(this).attr("rel");
	if($("#inner_info_"+row).hasClass("plus")){
		$("#loading_content_destination").clone();
		tb_show(null, '#?TB_inline&height=80&width=500&inlineId=loading_content_destination&modal=true', false);
		var testing_target_row = $(this).parents('tr:first');
		var target_form = $(this).parents('td:first').children('form');
		if($(this).hasClass("mock_controls")){
			$("#inner_info_"+row).addClass("minus");
			$("#inner_info_"+row).removeClass("plus");
			target_form = $(this).parents('td').siblings(":eq(0)").children('form');
		}else{
			if($(this).children("span").text() == "Collapse"){
				$(this).children("span").text("Expand");
				$("#display_wrap_"+row).hide();
				$(this).parents('tr:eq(0)').removeClass('display_row');
			}else{
				$(this).children("span").text("Collapse");
				$(this).addClass("minus");
				$(this).removeClass("plus");
				$("#display_wrap_"+row).show();
				$(this).parents('tr:eq(0)').addClass('display_row');
			}
		}
		
		$(this).bind("ajaxStart", function(){
			$(this).unbind('ajaxStart');
			$(this).unbind('ajaxComplete');
			$(this).show();
		});
		
		$.ajax({
			type: "POST",
			url: "/lib/ajax/trip_options_ajax.php",
			cache: false, 
			data: target_form.serialize() + "&row=" + row + "&section=flight_hotel",
			error: function(msg1, msg2, msg3){
				tb_remove();
			},
			success: function(msg){
				var target_row  = $('tr#row_' + row);
				testing_target_row.after(msg);
			}, 
			complete: function(){
				tb_remove();
			}
		});
	}
	$(this).unbind('click');
	$(this).bind("click", hide_info);
}

function hide_info(){
	var row = 'row_' + $(this).attr("rel");
	var row_num = $(this).attr("rel");
	var my_parent = $(this).parents('tr#'+row+':first');
	my_parent.next('tr').remove();
	my_parent.removeClass("display_row");
	if($(this).hasClass("mock_controls")){
		$("#inner_info_"+row_num).addClass("plus");
		$("#inner_info_"+row_num).removeClass("minus");
	}else{
		$(this).children("span").text("Expand");
		$(this).addClass("plus");
		$(this).removeClass("minus");
	}
	$(this).unbind('click');
	$(this).bind("click", show_info);
}

function show_info_hotel(){
	var row = $(this).attr("rel");
	if($("#inner_hotel_info_"+row).hasClass("plus")){
		$("#loading_content_destination").clone();
		tb_show(null, '#?TB_inline&height=80&width=500&inlineId=loading_content_destination&modal=true', false);
		var testing_target_row = $(this).parents('tr:first');
		var target_form = $(this).parents('td:first').children('form');
		if($(this).hasClass("mock_controls")){
			target_form = $(this).parents('td').siblings(":eq(0)").children('form');
			$("#inner_hotel_info_"+row).addClass("minus");
			$("#inner_hotel_info_"+row).removeClass("plus");
		}else{
			if($(this).children("span").text() == "Collapse"){
				$(this).children("span").text("Expand");
				$("#display_wrap_"+row).hide();
				$(this).parents('tr:eq(0)').removeClass('display_row');
			}else{
				$(this).children("span").text("Collapse");
				$(this).addClass("minus");
				$(this).removeClass("plus");
				$("#display_wrap_"+row).show();
				$(this).parents('tr:eq(0)').addClass('display_row');
			}
		}
		
		$(this).bind("ajaxStart", function(){
			$(this).unbind('ajaxStart');
			$(this).unbind('ajaxComplete');
			$(this).show();
		});
		$.ajax({
			type: "POST",
			url: "/lib/ajax/trip_options_ajax.php",
			cache: false, 
			data: target_form.serialize() + "&row=" + row + "&section=hotel",

			error: function(msg1, msg2, msg3){
				tb_remove();
			},
			success: function(msg){
				var target_row  = $('tr#hotel_row_' + row);
				testing_target_row.after(msg);
			}, 
			complete: function(){
				tb_remove();
			}
		});
	}
	$(this).unbind('click');
	$(this).bind("click", hide_info_hotel);
}

function hide_info_hotel(){
	var row = 'row_' + $(this).attr("rel");
	var row_num = $(this).attr("rel");
	var my_parent = $(this).parents('tr#'+row+':first');
	my_parent.next('tr').remove();
	my_parent.removeClass("display_row");
	if($(this).hasClass("mock_controls")){
		$("#inner_hotel_info_"+row_num).addClass("plus");
		$("#inner_hotel_info_"+row_num).removeClass("minus");
	}else{
		$(this).children("span").text("Expand");
		$(this).addClass("plus");
		$(this).removeClass("minus");
	}
	$(this).unbind('click');
	$(this).bind("click", show_info_hotel);
}

function changeImage(id, src){
	var image;
	if (image = document.getElementById(id)) {
		image.setAttribute('src', src);
	}
}

function show_destination(){
	var row = $(this).attr("rel");
	if($("#info_"+row).hasClass("plus")){
		$("#loading_content").clone();
		tb_show(null, '#?TB_inline&height=80&width=500&inlineId=loading_content&modal=true', false);
		var testing_target_row  = $(this).parents('tr#row_' + row + ':first');

		if($(this).hasClass("mock_controls")){
			$("#info_"+row).addClass("minus");
			$("#info_"+row).removeClass("plus");
		}else{
			if($(this).children("span").text() == "Collapse") {
				$(this).children("span").text("Expand");
				$("#display_wrap_"+row).hide();
				$(this).parents('tr:eq(0)').removeClass('display_row');
			} else {
				$(this).children("span").text("Collapse");
				$(this).addClass("minus");
				$(this).removeClass("plus");
				$("#display_wrap_"+row).show();
				$(this).parents('tr:eq(0)').addClass('display_row');
			}
		}
		
		$(this).bind("ajaxStart", function(){
			$(this).unbind('ajaxStart');
			$(this).unbind('ajaxComplete');
			$(this).show();
		});
		$.ajax({
			type: "POST",
			url: "/lib/ajax/trip_options_ajax.php",
			cache: false, 
			data: "section=destination&price_range="+$("#price_range_"+row).val()+"&destination_full="+$("#destination_full_"+row).val()+"&tour_code="+$("#tour_code_"+row).val()+"&departure_code="+$("#departure_code_"+row).val()+"&occupancy="+$("#occupancy_"+row).val()+"&max_occupancy="+$("#max_occupancy_"+row).val()+"&unique_code="+$("#unique_code_"+row).val()+"&row="+row,
			error: function(msg1, msg2, msg3){
				tb_remove();
			},
			success: function(msg){
				var target_row  = $('tr#row_' + row);
				testing_target_row.after(msg);
				$(".more_info").bind("click", show_info);
				$(".mock_controls").bind("click", show_info);
				if($("#price_range_results").length > 0){
					$("#sorting_results_hotel").hide(); 
					$("#price_range_results").tablesorter({headers:{5:{sorter:false}}, cssHeader:"sort_header", cssAsc:"sort_header_asc", cssDesc:"sort_header_desc"});
					$("#price_range_results").bind("sortStart",function() { 
						$("#sorting_results").show(); 
						$(".show_destination").remove();
						if($(this).hasClass("mock_controls")){
							$("#price_range_results #info_"+row).addClass("plus");
							$("#price_range_results #info_"+row).removeClass("minus");
						}else{
							$("#price_range_results .td_details span a").children("span").text("Expand");
							$("#price_range_results .td_details span a").addClass("plus");
							$("#price_range_results .td_details span a").removeClass("minus");
						}
						$(".display_row").removeClass();
					}).bind("sortEnd",function() { 
						$("#sorting_results").hide();
					}); 
				}
			}, 
			complete: function(){
				tb_remove();
			}
		});
	}
	$(this).unbind('click');
	$(this).bind("click", hide_destination);
}

function show_hotel_destination(){
	var row = $(this).attr("rel");
	if($("#hotel_info_"+row).hasClass("plus")){
		$("#loading_content_hotel").clone();
		tb_show(null, '#?TB_inline&height=80&width=500&inlineId=loading_content_hotel&modal=true', false);
		var testing_target_row = $(this).parents('tr#hotel_row_' + row + ':first');
		if($(this).hasClass("mock_controls")){
			$("#hotel_info_"+row).addClass("minus");
			$("#hotel_info_"+row).removeClass("plus");
		}else{
			if($(this).children("span").text() == "Collapse"){
				$(this).children("span").text("Expand");
				$("#display_wrap_"+row).hide();
				$(this).parents('tr:eq(0)').removeClass('display_row');
			}else{
				$(this).children("span").text("Collapse");
				$(this).addClass("minus");
				$(this).removeClass("plus");
				$("#display_wrap_"+row).show();
				$(this).parents('tr:eq(0)').addClass('display_row');
			}
		}
		
		$(this).bind("ajaxStart", function(){
			$(this).unbind('ajaxStart');
			$(this).unbind('ajaxComplete');
			$(this).show();
		});
		$.ajax({
			type: "POST",
			url: "/lib/ajax/trip_options_ajax.php",
			cache: false, 
			data: "section=destination_hotel&price_range="+$("#h_price_range_"+row).val()+"&destination_full="+$("#destination_full_"+row).val()+"&tour_code="+$("#h_tour_code_"+row).val()+"&departure_code="+$("#h_departure_code_"+row).val()+"&occupancy="+$("#h_occupancy_"+row).val()+"&max_occupancy="+$("#h_max_occupancy_"+row).val()+"&unique_code="+$("#h_unique_code").val()+"&row="+row,
			error: function(msg1, msg2, msg3){
				tb_remove();
			},
			success: function(msg){
				var target_row  = $('tr#hotel_row_' + row);
				testing_target_row.after(msg);
				$(".more_info").bind("click", show_info_hotel);
				$(".mock_controls").bind("click", show_info_hotel);
				if($("#price_range_results").length > 0){
					$("#sorting_results").hide(); 
					$("#price_range_results").tablesorter({headers:{5:{sorter:false}}, cssHeader:"sort_header", cssAsc:"sort_header_asc", cssDesc:"sort_header_desc"});
					$("#price_range_results").bind("sortStart",function() { 
						$("#sorting_results_hotel").show(); 
						$(".show_destination").remove();
						if($(this).hasClass("mock_controls")){
							$("#price_range_results #hotel_info_"+row).addClass("plus");
							$("#price_range_results #hotel_info_"+row).removeClass("minus");
						}else{
							$("#price_range_results .td_details span a").children("span").text("Expand");
							$("#price_range_results .td_details span a").addClass("plus");
							$("#price_range_results .td_details span a").removeClass("minus");
						}
						$(".display_row").removeClass();
					}).bind("sortEnd",function() { 
						$("#sorting_results_hotel").hide();
					}); 
				}
			}, 
			complete: function(){
				tb_remove();
			}
		});
	}
	$(this).unbind('click');
	$(this).bind("click", hide_hotel_destination);
}

function hide_destination(){
	var row = 'row_' + $(this).attr("rel");
	var row_num = $(this).attr("rel");
	var my_parent = $(this).parents('tr#'+row+':first');
	my_parent.next('tr').remove();
	my_parent.removeClass("display_row");
	if($(this).hasClass("mock_controls")){
		$("#info_"+row_num).addClass("plus");
		$("#info_"+row_num).removeClass("minus");
	}else{
		$(this).children("span").text("Expand");
		$(this).addClass("plus");
		$(this).removeClass("minus");
	}
	$(this).unbind('click');
	$(this).bind("click", show_destination);
}

function hide_hotel_destination(){
	var row = 'row_' + $(this).attr("rel");
	var row_num = $(this).attr("rel");
	var my_parent = $(this).parents('tr:first');
	my_parent.next('tr').remove();
	my_parent.removeClass("display_row");
	if($(this).hasClass("mock_controls")){
		$("#hotel_info_"+row_num).addClass("plus");
		$("#hotel_info_"+row_num).removeClass("minus");
	}else{
		$(this).children("span").text("Expand");
		$(this).addClass("plus");
		$(this).removeClass("minus");
	}
	$(this).unbind('click');
	$(this).bind("click", show_hotel_destination);
}


$(document).ready(function(){
	// Begin Filters	
	$(".nights").click(function(){
		checked_value = this.checked;
		var selected_value = $(this).attr("title");
		$("table tbody tr").each(function(){
			var row = $(this).attr("title");
			if(checked_value == false){
				if($("#nights_"+row).val() == selected_value){
					$("#row_"+row).hide();
				}
				if($("#h_nights_"+row).val() == selected_value){
					$("#hotel_row_"+row).hide();
				}
			}else{
				if($("#nights_"+row).val() == selected_value){
					$("#row_"+row).show();
				}
				if($("#h_nights_"+row).val() == selected_value){
					$("#hotel_row_"+row).show();
				}
			}
		});
	});	
	$(".destinations").click(function(){
		checked_value = this.checked;
		var selected_value = $(this).attr("title");
		$("table tbody tr").each(function(){
			var row = $(this).attr("title");
			if(checked_value == false){
				if($("#destination_"+row).val() == selected_value){
					$("#row_"+row).hide();
				}
				if($("#h_destination_"+row).val() == selected_value){
					$("#hotel_row_"+row).hide();
				}
			}else{
				if($("#destination_"+row).val() == selected_value){
					$("#row_"+row).show();
				}
				if($("#h_destination_"+row).val() == selected_value){
					$("#hotel_row_"+row).show();
				}
			}
		});
	});

	$(".location").click(function(){
		checked_value = this.checked;
		var selected_value = $(this).attr("title");
		$("table tbody tr").each(function(){
			var row = $(this).attr("title");
			if(checked_value == false){
				if($("#location_"+row).val() == selected_value){
					$("#row_"+row).hide();
				}
				if($("#h_location_"+row).val() == selected_value){
					$("#hotel_row_"+row).hide();
				}
			}else{
				if($("#location_"+row).val() == selected_value){
					$("#row_"+row).show();
				}
				if($("#h_location_"+row).val() == selected_value){
					$("#hotel_row_"+row).show();
				}
			}
		});
	});
	$(".type").click(function(){
		checked_value = this.checked;
		var selected_value = $(this).attr("title");
		$("table tbody tr").each(function(){
			var row = $(this).attr("title");
			if(checked_value == false){
				if($("#room_type_"+row).val() == selected_value){
					$("#row_"+row).hide();
				}
				if($("#h_room_type_"+row).val() == selected_value){
					$("#hotel_row_"+row).hide();
				}
			}else{
				if($("#room_type_"+row).val() == selected_value){
					$("#row_"+row).show();
				}
				if($("#h_room_type_"+row).val() == selected_value){
					$("#hotel_row_"+row).show();
				}
			}
		});
	});
	// End Filters


    // Edit farefinder search.
	$(".edit_fare_search").click(function(){
		$("#edit_fare_search").show();
	});
	$("#close_edit_fare_search").click(function(){
		$("#edit_fare_search").hide();
	});


    // Sorting functionality
	if($("#destination_results").length > 0){
		$("#sorting_results").hide(); 
		$("#destination_results").tablesorter({headers:{0:{sorter:false}}, cssHeader:"sort_header", cssAsc:"sort_header_asc", cssDesc:"sort_header_desc"});
		$("#destination_results").bind("sortStart",function() { 
			$("#sorting_results").show(); 
			$(".show_destination").remove();
			$(".show_trip_info").remove();
			if($(this).children("tbody").children("tr").children(".td_destination").children("span").children("a").hasClass("mock_controls")){
				$(".td_controls span a").addClass("plus");
				$(".td_controls span a").removeClass("minus");
			}else{
				$(".destination").children("span").text("Expand");
				$(".destination").addClass("plus");
				$(".destination").removeClass("minus");
			}
			$(".display_row").removeClass();
		}).bind("sortEnd",function() { 
			$("#sorting_results").hide();
		});
	}
	
	if($("#destination_hotel_results").length > 0){
		$("#sorting_results").hide();
		$("#destination_hotel_results").tablesorter({headers:{0:{sorter:false}}, cssHeader:"sort_header", cssAsc:"sort_header_asc", cssDesc:"sort_header_desc"});
		$("#destination_hotel_results").bind("sortStart",function() { 
			$("#sorting_results_hotel").show(); 
			$(".show_destination").remove();
			$(".show_trip_info").remove();
			if($(this).children("tbody").children("tr").children(".td_destination").children("span").children("a").hasClass("mock_controls")){
				$(".td_controls span a").addClass("plus");
				$(".td_controls span a").removeClass("minus");
			}else{
				$(".hotel_destination").children("span").text("Expand");
				$(".hotel_destination").addClass("plus");
				$(".hotel_destination").removeClass("minus");
			}
			$(".display_row").removeClass();
		}).bind("sortEnd",function() { 
			$("#sorting_results_hotel").hide();
		}); 
	}
	
	// Resubmit search.
	$("#submit_custom_search").click(function(){
		var week = $("#travel_date").val();
		var price_range = $("#price_range").val();
		var occupancy = $("#occupancy").val();
		var departure_code = $("#departure_code").val();
		location.href = "/trip_finder/price_range.php?week="+week+"&price_range="+price_range+"&occupancy="+occupancy+"&departure_code="+departure_code;
	});
	$("tr[@id^=display_wrap_]").hide();

	// Show/hide bindings.

	$(".destination").bind("click", show_destination);
	$(".hotel_destination").bind("click", show_hotel_destination);
});