var reqInProg = false;
var contactform;

function setup() {
	$('input#find').focus(function() {
	   $(this).val('');
	   $(this).unbind('focus');
	   return true;
	}).change(function() {
	   findPosts();
	});
	$('form#findposts').submit(function() { findPosts(); return false ; });
	contactform = $('form#contact');
	contactform.submit(function() {
		$.post('/contact.php', $(this).serialize(), function(data) {
			if (data != "1") {
				$('#contact_status').text("Thanks for your thoughts!");
			} else {
				$('#contact_status').text("You must enter your email and some text.");
			}
		});
		return false;
	});
}

function findPosts() {
    if (!reqInProg) {
        arg = arguments[0];
        if (arg == null || arg == '') {
            arg = "s="+$('input#find').val();
        }
        $.getJSON('/wp-content/themes/cyclonic/find.php',arg,function(data) {
            var $ol = $('ol#find_list');
            $ol.empty();
			if (data.length >= 1) {
				for (var i=0;i<data.length;i++) {
					$ol.append($('<li><a href="'+data[i][1]+'">'+data[i][0]+'</a></li>'));
				}
			} else {
				$ol.append($("<li>Your search yielded no results</li>"));
			}
			$("div#find_loader").hide();
            reqInProg = false;
        });
		$("div#find_loader").show();
        reqInProg = true;
    }
    return;
}