$(function(){
    //for styling purposes, add a js class to content
    $('#content').addClass('js');
    //add a new attendee form row
    $('#content .addattendee a').click(function(){
        var thiseventid = $(this).attr('id').split('-');
        if (thiseventid.length > 1) { thiseventid = thiseventid[1]; } else { return false; }
        var spacesleft = Number($('#spacesleft-' + thiseventid).html());
        $('#spacesleft-' + thiseventid).text(spacesleft - 1);
        if (spacesleft > 0) {
            if (spacesleft == 1) { $(this).parent().hide(); }
            var attendeeform = $('#event-' + thiseventid + '-1').html();
            var attendeelength = $('#event-' + thiseventid + ' .attendee').length;
            attendeelength++;
            $(this).parent().before('<div class="attendee" id="event-'+thiseventid+'-'+attendeelength+'">'+attendeeform+'</div>');
            $('#event-'+thiseventid+'-'+attendeelength+' .rowtitle span').text(attendeelength);
            $(
                '#event-'+thiseventid+'-'+attendeelength+' .row.name label, ' +
                '#event-'+thiseventid+'-'+attendeelength+' .row.email label, ' +
                '#event-'+thiseventid+'-'+attendeelength+' .row.phone label'
             ).each(function(){
                $(this).attr('for', $(this).attr('class') + '-'+thiseventid+'-'+attendeelength);
            });
            $(
                '#event-'+thiseventid+'-'+attendeelength+' .row.name input, ' +
                '#event-'+thiseventid+'-'+attendeelength+' .row.email input, ' +
                '#event-'+thiseventid+'-'+attendeelength+' .row.phone input'
             ).each(function(){
                $(this).attr('name', $(this).attr('class') + '-'+thiseventid+'-'+attendeelength);
            });
            $(
                '#event-'+thiseventid+'-'+attendeelength+' .row.name input, ' +
                '#event-'+thiseventid+'-'+attendeelength+' .row.email input, ' +
                '#event-'+thiseventid+'-'+attendeelength+' .row.phone input'
             ).each(function(){
                $(this).attr('id', $(this).attr('class') + '-'+thiseventid+'-'+attendeelength);
            });
            
            $('#event-'+thiseventid+'-'+attendeelength+' .row.email input').val($('#event-'+thiseventid+'-1 .row.email input').val());
            $('#event-'+thiseventid+'-'+attendeelength+' .row.phone input').val($('#event-'+thiseventid+'-1 .row.phone input').val());
            $('#event-'+thiseventid+'-'+attendeelength).append('<div class="row deleteattendee"><a href="#" title="Delete this attendee">X</a>');
            $('#content .deleteattendee a').unbind();
            $('#content .deleteattendee a').click(function(){
                var thisattendeeid = $(this).parent().parent().attr('id').split('-');
                if (thisattendeeid.length > 2) { thisattendeeid = thisattendeeid[2]; } else { return false; }
                $('#event-' + thiseventid + '-' + thisattendeeid).remove();
                $('#spacesleft-' + thiseventid).text(Number($('#spacesleft-' + thiseventid).text()) + 1);
                $('#event-' + thiseventid + ' .attendee').each(function(){
                    var otherattendeeid = $(this).attr('id').split('-');
                    if (otherattendeeid.length > 2) { otherattendeeid = Number(otherattendeeid[2]); }
                    if (otherattendeeid > thisattendeeid) {
                        $('#event-'+thiseventid+'-'+otherattendeeid+' .rowtitle span').text(otherattendeeid - 1);
                        $(
                            '#event-'+thiseventid+'-'+otherattendeeid+' .row.name label, ' +
                            '#event-'+thiseventid+'-'+otherattendeeid+' .row.email label, ' +
                            '#event-'+thiseventid+'-'+otherattendeeid+' .row.phone label'
                         ).each(function(){
                            $(this).attr('for', $(this).attr('class') + '-'+thiseventid+'-'+(otherattendeeid - 1));
                        });
                        $(
                            '#event-'+thiseventid+'-'+otherattendeeid+' .row.name input, ' +
                            '#event-'+thiseventid+'-'+otherattendeeid+' .row.email input, ' +
                            '#event-'+thiseventid+'-'+otherattendeeid+' .row.phone input'
                         ).each(function(){
                            $(this).attr('name', $(this).attr('class') + '-'+thiseventid+'-'+(otherattendeeid - 1));
                        });
                        $(
                            '#event-'+thiseventid+'-'+otherattendeeid+' .row.name input, ' +
                            '#event-'+thiseventid+'-'+otherattendeeid+' .row.email input, ' +
                            '#event-'+thiseventid+'-'+otherattendeeid+' .row.phone input'
                         ).each(function(){
                            $(this).attr('id', $(this).attr('class') + '-'+thiseventid+'-'+(otherattendeeid - 1));
                        });
                        $('#event-' + thiseventid + '-' + otherattendeeid).attr('id', ('event-' + thiseventid + '-' + (otherattendeeid - 1)));
                    }
                });
                return false;
            });
        }
        return false;
    });
    //add signup link
    $('.signup .open').each(function(){
        $(this).append('<a href="#">sign up now</a>');
    });
    //open a signup form
    $('#content .open a').click(function (){
        var thiseventid = $(this).parent().parent().parent().attr('id').split('-');
        if (thiseventid.length > 1) { thiseventid = thiseventid[1]; } else { return false; }
        var spacesleft = Number($('#spacesleft-' + thiseventid).html());
        var attendeelength = $('#event-' + thiseventid + ' .attendee').length;
        $('#spacesleft-' + thiseventid).text(spacesleft - attendeelength);
        $('#event-' + thiseventid + ' .signupform').show();
        $(this).hide();
        return false;
    });
    //close a signup form
    $('#content .cancelbutton').click(function(){
        var thiseventid = $(this).parent().parent().parent().parent().parent().attr('id').split('-');
        if (thiseventid.length > 1) { thiseventid = thiseventid[1]; } else { return false; }
        var resetspaces = $('#spacesleft-' + thiseventid).attr('class').split('-');
        if (resetspaces.length > 1) { resetspaces = resetspaces[1]; } else { return false; }
        $('#spacesleft-' + thiseventid).text(resetspaces);
        $('#event-' + thiseventid + ' .signupform').hide();
        $('#event-' + thiseventid + ' .open a').show();
        return false;
    });
    //form validation
    $('#content .signupform').submit(function(){
        var form = $(this).attr('id');
        var emptyname = 0;
        var emptyemailphone = 0;
        $('#' + form + ' input.name').each(function(){
            if ($(this).val() == '') {
                emptyname++;
            }
            var idparts = $(this).attr('id').split('-');
            if (idparts.length > 2) {
                if ($('#email-' + idparts[1] + '-' + idparts[2]).val() == '' &&
                    $('#phone-' + idparts[1] + '-' + idparts[2]).val() == '') {
                    emptyemailphone++;
                }
            }
            
        });
        if (emptyname > 0) {
            alert('You have left ' + emptyname + ' attendee name field(s) empty. Please fill in the correct information and then resubmit the form.');
            return false;
        }
        if (emptyemailphone > 0) {
            alert('You have entered ' + emptyemailphone + ' attendee(s) who do(es) not have any contact information. Please provide at least a phone number or email address for each attendee and then resubmit the form.');
            return false;
        }
        
        
        
        return true;
    });

});