function run_single (data) {
	if (data.url) {
		window.location = data.url;
	} else {
		$('#qf_content').slideUp('fast', function () {
			$('#qf_content').html('<br />' + data.content);
			$('#qf_content').slideDown('fast');
		});
		
	}
}

function drop_b_change () {
	if ($('#qf_drop_c')) { $('#qf_drop_c').remove(); }
	
	// Check if this is a "none" option
	if ($('#qf_drop_b').val() != 'none') {
		
		// If a real option, find all entries of this option
		$.ajax({
			cache: false,
			dataType: 'json',
			type: 'POST',
			url: '/quickfinder/ajax_select_option2',
			data: { 
				website_id: $('#website_id').val(), 
				option1_value: $('#qf_drop_a').val(), 
				option2_value: $('#qf_drop_b').val() 
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				alert('Error getting quick finder data, please try again later.');
			},
			success: function (data, textStatus, XMLHttpRequest) {	
				
				if (data.has_single == 1) {
					run_single(data);
				} else {
					
					// Create a a dropdown for option3_items
					var select = $('<select />').attr('name', 'qf_drop_c').attr('id', 'qf_drop_c').addClass('dropdown');
					select.append($('<option />').val('none').html('Please select...'));
					
					$(data.option3_items).each(function(index, element) {
						select.append($('<option />').val(element).html(document.createTextNode(element)));
					});
					
					select.bind('change', function() {
						drop_c_change();
					});
					
					$('#quick_finder').append(select);
				}
			}
		});
		
	}
}

function drop_c_change () {
	if ($('#qf_drop_c').val() != 'none') { 
		
		$.ajax({
			cache: false,
			dataType: 'json',
			type: 'POST',
			url: '/quickfinder/ajax_select_option3',
			data: { 
				website_id: $('#website_id').val(), 
				option1_value: $('#qf_drop_a').val(), 
				option2_value: $('#qf_drop_b').val(), 
				option3_value: $('#qf_drop_c').val()
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				alert('Error getting quick finder data, please try again later.');
			},
			success: function (data, textStatus, XMLHttpRequest) {
				run_single(data);
			}
		});
		
	}
}

$(document).ready(function () {
	
	$('#qf_drop_a').bind('change', function() {
		// First remove any other boxes
		if ($('#qf_drop_b')) { $('#qf_drop_b').remove(); }
		if ($('#qf_drop_c')) { $('#qf_drop_c').remove(); }
		
		// Check if this is a "none" option
		if ($(this).val() != 'none') {
			
			// If a real option, find all entries of this option
			$.ajax({
				cache: false,
				dataType: 'json',
				type: 'POST',
				url: '/quickfinder/ajax_select_option1',
				data: { website_id: $('#website_id').val(), option1_value: $(this).val() },
				error: function (XMLHttpRequest, textStatus, errorThrown) {
					alert('Error getting quick finder data, please try again later.');
				},
				success: function (data, textStatus, XMLHttpRequest) {			
					if (data.has_single == 1) {
						run_single(data);
					} else {
						// Create a a dropdown for option2_items
						var select = $('<select />').attr('name', 'qf_drop_b').attr('id', 'qf_drop_b').addClass('dropdown');
						select.append($('<option />').val('none').html('Please select...'));
						
						$(data.option2_items).each(function(index, element) {
							select.append($('<option />').val(element).html(document.createTextNode(element)));
						});
						
						select.bind('change', function() {
							drop_b_change();
						});
						
						$('#quick_finder').append(select);
					}
				}
			});
			
		}
	});
	
});