$(document).ready(function() {
    //initiate FancyBox for contact team memeber
    $("a.send-message").fancybox({
        'callbackOnShow'        :   contactTeamLoaded,
        'callbackOnClose':  function() {
            // make sure we're not listening for form submissions still:
            $('form#contactTeam').unbind('submit');
        }
    });
});

function contactTeamLoaded() {
    /* callback to handle successful load of the contact team box: */
    // set up handler for form submit:
    $('form#contactTeam').submit(function(e) {
        e.preventDefault();
        $('form#contactTeam button').attr({'disabled':'disabled'}).addClass('disabled');
        // build field values into JSON to send to the server:
        var data = new Object();
        $(this).find(':input').each(function() {
            var inputName = $(this).attr('name');
            if (inputName != '') {
                data[inputName] = $(this).val();
            }
        });
        $.post($('form#contactTeam').attr('action'), data, handleContactTeamPost);
    });
}

function handleContactTeamPost(html) {
    /* callback to handle response from AJAX post for contact team form */
    $('#fancy_ajax').html(html);
    // if the form has been reloaded, there are errors, so handle this case:
    if ($('#fancy_ajax').find('form#contactTeam').length > 0) {
        contactTeamLoaded();
    }
    // if the thank you message has been loaded, resize the fancy box to an
    // appropriate height
    if ($('div.contactTeamThankYou').length > 0) {
        $('#fancy_outer').height($('div.contactTeamThankYou').height() + 20 + 'px');
    }
    
}