/**
 * Handles RSVP
 */

(function(){
    if(!window.cs) {
        return;
    }
    
    var rsvp = window.cs.rsvp = {
        // default options/settings
        empty: null,
        rsvpCount: 0
    };
    
    rsvp.init = function() {
        // initialize default actions
        rsvp.rsvpCount = parseInt($('#gatheringsCount').html());
        
        /* Upcoming Gatherings RSVP Button */
        $('.rsvp a.accept').click(function(){
            if (!$('#isVisitorLoggedIn').val()) {
                return cs.lightbox.loginRegister();
            }
            var gatheringID = $(this).parent().children('#gatheringID')[0].value;
            if($(this).parent().hasClass("attending")) {
                if (cs.forms.doGatheringRSVP(gatheringID, 0)) {
                    $('.gathering-'+gatheringID+' .rsvp').removeClass("attending");
                    rsvp.rsvpCount++;
                    rsvp.updateCount(true);
                }
            } else {
                if (cs.forms.doGatheringRSVP(gatheringID, 1)) {
                    $('.gathering-'+gatheringID+' .rsvp').removeClass("notAttending");
                    $('.gathering-'+gatheringID+' .rsvp').addClass("attending");
                    rsvp.rsvpCount--;
                    rsvp.updateCount(false);
                }
            }
            return false;
        });
        $('.rsvp a.reject').click(function(){
            if (!$('#isVisitorLoggedIn').val()) {
                return cs.lightbox.loginRegister();
            }
            var gatheringID = $(this).parent().children('#gatheringID')[0].value;
            if($(this).parent().hasClass("notAttending")) {
                if (cs.forms.doGatheringRSVP(gatheringID, 0)) {
                    $('.gathering-'+gatheringID+' .rsvp').removeClass("notAttending");
                    rsvp.rsvpCount++;
                    rsvp.updateCount(true);
                }
            } else {
                if (cs.forms.doGatheringRSVP(gatheringID, -1)) {
                    $('.gathering-'+gatheringID+' .rsvp').removeClass("attending");
                    $('.gathering-'+gatheringID+' .rsvp').addClass("notAttending");
                    rsvp.rsvpCount--;
                    rsvp.updateCount(false);
                }
            }
            return false;
        });
        
        /* Gathering RSVP Buttons */
        $('#rsvp_attending').click(function(){
            $('#didVisitorRSVP').val(1);
            if (!$('#isVisitorLoggedIn').val()) {
                return cs.lightbox.loginRegister();
            }
            var gatheringID = $('#rsvpWrapper').children('#gatheringID')[0].value;
            if (cs.forms.doGatheringRSVP(gatheringID, 1)) {
                $('#rsvpWrapper h3').hide();
                $('#rsvpWrapper #rsvp_attending').hide();
                $('#rsvpWrapper #rsvp_notAttending').hide();
                $('#rsvpWrapper #rsvp_status').attr("class","attending").html("Attending");
                $('#rsvpWrapper #rsvp_change').show();
                rsvp.rsvpCount--;
                rsvp.updateCount(false);
            }
            return false;        
        });
        $('#rsvp_notAttending').click(function(){
            $('#didVisitorRSVP').val(2);
            if (!$('#isVisitorLoggedIn').val()) {
                return cs.lightbox.loginRegister();
            }
            var gatheringID = $('#rsvpWrapper').children('#gatheringID')[0].value;
            if (cs.forms.doGatheringRSVP(gatheringID, -1)) {
                $('#rsvpWrapper h3').hide();
                $('#rsvpWrapper #rsvp_attending').hide();
                $('#rsvpWrapper #rsvp_notAttending').hide();
                $('#rsvpWrapper #rsvp_status').attr("class","notAttending").html("Not Attending");
                $('#rsvpWrapper #rsvp_change').show();
                rsvp.rsvpCount--;
                rsvp.updateCount(false);
            }
            return false;        
        });
        $('#rsvp_change').click(function(){
            $('#didVisitorRSVP').val(3);
            if (!$('#isVisitorLoggedIn').val()) {
                return cs.lightbox.loginRegister();
            }
            var gatheringID = $('#rsvpWrapper').children('#gatheringID')[0].value;
            if (cs.forms.doGatheringRSVP(gatheringID, 0)) {
                $('#rsvpWrapper h3').show();
                $('#rsvpWrapper #rsvp_attending').show();
                $('#rsvpWrapper #rsvp_notAttending').show();
                $('#rsvpWrapper #rsvp_status').attr("class","").html("");
                $('#rsvpWrapper #rsvp_change').hide();
                rsvp.rsvpCount++;
                rsvp.updateCount(true);
            }
            return false;        
        });
    };

    rsvp.updateCount = function(param) {
        if (rsvp.rsvpCount < 0) {
            rsvp.rsvpCount = 0;
        }
        if (rsvp.rsvpCount == 0 && !param) {
            $('#gatheringsCount').hide();
        }
        if (rsvp.rsvpCount == 1 && param) {
            $('#gatheringsCount').show();
        }
        if (rsvp.rsvpCount >= 10 && !$('#gatheringsCount').hasClass('double')) {
            $('#gatheringsCount').addClass('double');
        }
        if (rsvp.rsvpCount < 10 && $('#gatheringsCount').hasClass('double')) {
            $('#gatheringsCount').removeClass('double');
        }
        $('#gatheringsCount').html(rsvp.rsvpCount);
    };
                                      

})();
