		$(document).ready(function(){
			$('#what').click(function() {
				if($(this).val() == 'What are you looking for?') {
					$(this).val('');
				}
			});

			$('#what').keyup( function() {
				autofill($(this), 'category');
			});

			$('#where').click(function() {
				if($(this).val() == 'Where do you want to find it?') {
					$(this).val('');
				}
			});

			$('#where').keyup( function() {
				autofill($(this), 'location');
			});

			$('#find').click(function() {
				$('#what').css({'border': '3px solid white'});
				$('#where').css({'border': '3px solid white'});

				if($('#what').val() == 'What are you looking for?' || $('#what').val() == '') {
					$('#what').css({'border': '3px solid red'});
					return false;
				}else if($('#where').val() == 'Where do you want to find it?' || $('#where').val() == '') {
					$('#where').css({'border': '3px solid red'});
					return false;
				}else{
					$('#main_search').submit();
					return true;
				}
			});


			function autofill(obj, type) {
				$.getJSON('api.php?json=true&type=' + type + '&q=' + obj.val(), function(data) {
					var items = [];

					$.each(data, function(key, val) {
						items.push('<a class="autofill" rel="' + obj.attr('id') + '" style="display: block; margin-bottom: 5px; cursor: pointer;">' + val + '</a>');
					});

					var data = items.join('');
					if(data == '') {
						$('#' + type + ' .bubble_text').html( $('#' + type + ' .bubble_text').attr('rel') );
						$('#header #' + type).hide();
					}else{
						$('#' + type + ' .bubble_text').html(data);
						$('#header #' + type).show();
					}
				});
			}

			$('a.autofill').live('click', function() {
				var input = $(this).attr('rel');
				var text = $(this).html();
				text = text.replace( /\&amp;/g, '&' );


				$('#' + input).val( text );

				$(this).parent().html( $(this).parent().attr('rel') );

				$('#header #' + $(this).parent().parent().attr('id')).hide();
			});

				
		});
