$(document).ready(function() {
    var qry = function() {
	var el = document.getElementById('qfield');

	/* prevent double-submit: once we have handled this, we delete attr */
	if (el.query != el.value) {
	    el.query = el.value;

	    $("#searchcontentcontainer").slideUp(500, function() {
		$.get(el.form.action, { 'q': el.query },
		    function(xml) {
			$("#searchcontent").html(xml);
			$("#realcontent").slideUp(500, function() {
			    $("#searchcontentcontainer").slideDown(500);
			});
		    }, "html")
	    });
	}

	return false;
    };

    /* disable form submit */
    $("#dontsubmitme").bind("submit", qry);

    /* find the search field, stick some handlers to it */
    $("#qfield").bind("change", qry
    ).bind("blur", function() {
	this.style.opacity = 0.3;
	this.style.filter = "alpha(opacity=30)";
    }).bind("focus", function() {
	this.style.opacity = 0.8;
	this.style.filter = "alpha(opacity=80)";
	if (this.value == this.defaultValue)
	    this.value = "";
    });

    /* the close search button reverses what the result ajax does */
    $("#closesearch").bind("click", function() {
	$("#searchcontentcontainer").slideUp(500, function() {
	    var el = document.getElementById('qfield');
	    el.query = null;
	    $("#realcontent").slideDown(500);
	});
    });
});

