// 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]);

        // Show plan sharing box
        if (this.button.hasClass(this.color)) {
            $('#share-plans').show();                
            setTimeout(function() {$('#share-plans').hide(500)}, 6000);
        }
    }
}

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').live('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);

        $.post(url, $.extend({invitee: $('#invite-input').val()},
                                       $('#invitation-form').toObject()), function(data) {
            if (data[0] == 'error') {
                // Load input box
                $('#box-invite').html(data[1]);
            } else {
                // Load invitee list
                $('#box-invitees').html(data[1]);

                // Clean field
                $('#invite-input').val('');
                $('#box-invite .error .notice').hide();
            }

            // Refresh some js-related stuff
            $('#invite-input').attr('disabled', false);
            Concealable.prototype.enable();
            enable_autosuggest('invite-input', 'invite-suggest', '/util/invite_suggest', true);
        }, "json");
    });

    // 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();
    }
}
