// Participation/observation button
function Participation(button_id, color, title_on, title_off, image_on, image_off) {
    this.button = $('#' + button_id);
    this.color = color;
    this.style_on = new Array(title_on, image_on);
    this.style_off = new Array(title_off, image_off);
}
Participation.prototype = {
    style: function(status) {
        // Assign style according to passed status or toggle style 
        if (status != undefined) {
            if (status) {
                this.button.addClass(this.color);
            } else {
                this.button.removeClass(this.color);
            }
            var style = status ? this.style_on : this.style_off;
        } else {
            this.button.toggleClass(this.color);
            var style = this.button.hasClass(this.color) ? this.style_on : this.style_off;
        }
        this.button.find('.text').text(style[0]);
        this.button.find('img').attr('src', style[1]);
    }
}

part_obs_clicked = function(_this, url, type) {
    // Handle participation/observation button clicks
    if (type == 'part') {
        style = part;
        other_style = obs;
        color = 'orange';
    } else {
        style = obs;
        other_style = part;
        color = 'green';
    }
    style.style();
    if ($(_this).hasClass(color)) {
        other_style.style(false); }
        $.getJSON(url, function(data){
          $('#box-part-obs').html(data[1]);
          if ((data[0] == 'on' && !$(_this).hasClass(color)) ||
              (data[0] == 'off' && $(_this).hasClass(color))) {
              style.style();
              if ($(this).hasClass(color)) {
                  other_style.style(false); }
          }
        }, 'post');
    return false;
}

$(document).ready(function() {
    // Invitations
    $('#invite-button').click(function(){
        // TODO: check if user can invite,
        //       protect controller from spam: e.g. if N > 10 - captcha.
        var url = $('#invite-url').val();
        $('#invite-input').attr('disabled', true);
        // TODO: this should actually reload #box-invitees on succsess and
        //       #box-invite on failure (e.g. json status)
        $('#box-invitees').load(url, $.extend({invitee: $('#invite-input').val()},
                                              $('#invitation-form').toObject()), function(){
            // Clean field
            $('#invite-input').val('');
            $('#invite-input').attr('disabled', false);

            // Refresh concealable label
            Concealable.prototype.enable();

            // Rebind
            enable_autosuggest('invite-input', 'invite-suggest', '/util/invite_suggest', true);
        });
    });

    // Invitations autosuggest
    enable_autosuggest('invite-input', 'invite-suggest', '/util/invite_suggest', true);

    // Video inclusion button
    $('#video-upload-form').keypress(function(e) {
        if (e.keyCode == 27) {
            $('#video-upload-form').hide();
        } else if (e.keyCode == 13) {
            $('#video-upload-form button').click();
        }
    });
});

/*
 * Show invitation editing overlay
 */
function edit_invitation(id) {
    $('#overlay-content').load('/event/' + id + '/edit_invitation', $('#invitation-form').toObject(), function() {
        show_overlay('invite');
        enable_tinymce(['invitation-text']);
    });
}

/* 
 * Show message sending overlay
 *
 * url -- make an ajax call to this url; if it is false -- simply
 *        renew controls.
 */
function contact_interested(url) {
    function refresh() {
        // Refresh concealable labels and enable controls
        Concealable.prototype.enable();
        enable_selectboxes();
        enable_tinymce(['contact_text']);
    }

    if (url) {
        $('#overlay-content').load(url, function() {
            show_overlay('contact-interested');
            refresh();
        });
    } else {
        refresh();
    }
}
