$(function() {
    $(':image,:submit').removeAttr('disabled'); // first, always enable submit buttons

    $('form').submit(function() {
        if ($(this).find(':image,:submit').length == 0 || $(this).find(':image,:submit').attr('disabled'))
            return false;

        // Disable submit button
        $(this).find(':submit').attr('disabled', 'disabled').addClass('loading');

        // Show any Ajax loading indicators
        $(this).find('div.form-status .ajax-loader').show();
    });

    // Intercept form submissions and transmit them via Ajax
    // Requires <form class="hijax"> and <div class="ajax-replace">

    $('form.hijax').submit(function() {
        var form = $(this);

        var type = form.attr('method').toUpperCase();
        var url = form.attr('action');
        var data = form.serialize();
        var timeout = 10000; // error out after 10 seconds

        function complete(xhr, status) {
            // Hide any lingering loading indicators
            form.find('.ajax-loader').hide();
            form.find(':submit').removeClass('loading');
        }

        function success(data, status) {
            form.find('.ajax-replace').replaceWith(data);
        }

        function error(xhr, status, err) {
            var msg = (status == 'timeout') ? 'Request timed out.' : 'An error occurred.';
            var data = createMessage('error', msg + ' Please try your request later.');
            form.find('.form-status').append(data);
            // Re-enable submit buttons so user can try again
            form.find(':image,:submit').removeAttr('disabled');
        }

        // Remove any lingering messages
        form.find('.success,.error,.message').remove();

        $.ajax({
            type: type,
            url: url,
            data: data,
            timeout: timeout,
            complete: complete,
            success: success,
            error: error
        });

        return false;
    });

    $($('#sidenav li a[href=' + location.pathname + ']').get(0)).parent().addClass('active');

    $('#profile-controls select[name=profile_token]').change(function() {
        var select = $(this).get(0);
        if (select.selectedIndex != 0) {
            select.form.submit();
        }
    });

    // footer contact form
    $('#footer :text').focus(function() { if (this.value == this.defaultValue) this.value = ''; });
    $('#footer :text').blur(function() { if (this.value == '') this.value = this.defaultValue; });

    var textareaDefault = $('#footer textarea').val();
    $('#footer textarea').focus(function() { if ($(this).val() == textareaDefault) $(this).val(''); });
    $('#footer textarea').blur(function() { if ($(this).val() == '') $(this).val(textareaDefault); });

});

/* Utility functions
*/

function createMessage(status, text) {
    var p = document.createElement('p');
    var t = document.createTextNode(text);
    p.className = (status == 'timeout') ? 'error' : status;
    p.appendChild(t);
    return p;
}

