// Lightbox-style modal box that shows contacts to invite to a group/room/whatever.
//
// Usage: 1) Add this script to the page.
//        2) Give an <a> a class of "invitebox", and a lightbox will open to its href.
$(document).ready(function() {
    // Set up event handlers for the contact invitebox:
    $('.invitelink').live('click', function(e) { invitebox_show($(this).attr('href')); e.preventDefault(); });
    $('#invitebox .closeinvites').live('click', function() { $('#invitebox').hide(); });
    $('#invitebox .finishbutton').live('click', function() { $('#invitebox').hide().empty(); });
});


function invitebox_show(html_url) {
    if (!$('#invitebox').length) { $('body').append('<div id="invitebox"></div>'); }
    var invitebox = $('#invitebox');
    if (!invitebox.html()) {
        $.get(html_url, function(data) {
            // Load and execute each script in the returned data before injecting the html:
            $(data).find('script').each(function() {
                $.getScript($(this).attr('src'));
            });
            invitebox.html(data);
            $('#contactlist div').quicksearch({
                position: 'before',
                attached: '#contactlist',
                labelText: 'Filter contacts:'
            });
        });
    }
    // Bind Esc key to closing the invitebox:
    $(document).keyup(function(e) { if (e.keyCode == 27) { invitebox.hide(); } });

    invitebox.show().css('left', $(window).width()/2 - 300);    // Show and center the contact box
}

function invitebox_post() { 
    if (validate_contacts()) {
        var form = $('#invitebox form');
        var post_data = {'emails': form.find('#emails_id').val(), 
                         'contacts': form.find('#contacts_id').val() };
        $.ajax({
                type: 'POST',
                url: $(form).attr('action'),
                data: post_data,
                success: invitebox_success,
                error: function(request,status,error) {
                            alert('Sorry, an error while sending your invites. Please try again.');
                            $('#waitmessage').hide();
                            $('#inviteform, #invitebuttons').show();
                       }
              });
    }
    return false;
}

function invitebox_success(response) {
    var emails = response.split(',');
    var confirm_box = $('#confirmmessage');
    $.each(emails, function(index,email) {
        confirm_box.find('ul').append('<li>' + email + '</li>');
    });
    $('#inviteform, #waitmessage, #invitebuttons').hide();
    confirm_box.show();
}

function invitebox_wait_msg() {
    $('#inviteform, #confirmmessage, #invitebuttons').hide();
    $('#waitmessage').show();
}

function validate_contacts() {
    // Get all selected checkboxes, and create a value for the hidden input:
    c_checked = [];
    $('#contactlist input:checked:enabled').each( function() {
        c_checked.push( this.value );
    });
    var emails_entered = $('#emails_id').val().match('@');
    if (c_checked.length === 0 && !emails_entered) {
        alert("Please select the people that you want to invite.");
        return false;
    }
    $('#contacts_id').val(c_checked.join(','));
    invitebox_wait_msg();
    return true;
}

