$(document).ready( function() {
    // Make messages fade in
    $(".message").hide().fadeIn();

    // Shrink the input to the size of the content
    var searchq = $("#search input#q");
    if (searchq.val() != "") {
        var shadow = $("<span></span>").css({
                position: 'absolute',
                top: 0,
                left: 0,
                fontSize: searchq.css('fontSize'),
                fontFamily: searchq.css('fontFamily'),
                lineHeight: searchq.css('lineHeight'),
                resize: 'none'
            }).appendTo(document.body);

        shadow.text(searchq.val());
        searchq.attr('_orig_width', "" + searchq.width() + "px");
        searchq.css('width', shadow.width() + 15);
        shadow.remove();

        // Focus should grow the input and select all
        searchq.focus( function() {
            if (this.value == this.defaultValue) {
                var w = searchq.attr('_orig_width') || '8em';
                searchq.css('width', w);
                this.select();
            }
        } );
    }

    // Setup the addsource_url hint
    $("#addsource_url").blur( function() {
        if ($(this).val() == "")
            $(this).val("http://");
        if ($(this).val() == "http://")
            $(this).css("color", "#999");
    } ).focus( function() {
        if ($(this).val() == "http://") {
            $(this).val("");
            $(this).css("color", "#000");
        }
        if ($("#captcha_cnt").is(":hidden"))
            $("#captcha_cnt").slideDown("normal");
    } ).blur();

    // If the hash is #source_SOURCEID, highlight it
    if (m = location.hash.match(/^#?(source_\w*)$/)) {
        var source = $("li#" + m[1]);
        source.animate( { "backgroundColor": "#fc6" }, 750 );
    }

    // Checkbox/field disabling actions
    $("#close").change( function() {
        if ($(this).is(":checked")) {
            $("#closenode_redirect").attr("disabled", "").focus();
            if ($("#closenode_redirect").val() == "")
                $("#closenode_redirect").val("home")
        } else {
            $("#closenode_redirect").attr("disabled", "disabled");
        }
    } ).change();
} );
