var sendinvite = true;

$(window).ready(function(){

    $('#filter_attendees').keyup(filterAttendees);
    $('#filter_attendees').focus(focusFilter).blur(focusBlur).each(focusBlur);

    $('#findPeople').keyup(findMatches).each(findMatches).focus(focusFind).blur(blurFind).each(blurFind);

    $('#possiblePeople li').mouseover(function(){ $(this).addClass('on') }).mouseout(function(){$(this).removeClass('on');});

    $('input:radio[name=response]').change(function(){
        if($('input:radio[name=response]:checked').val() == "Q") {
            $('#sendinvite').attr('disabled',true);
            $('#invitetext').attr('disabled',true);
            sendinvite = $('#sendinvite').attr('checked');
            $('#sendinvite').attr('checked', false);
        } else {
            $('#sendinvite').attr('disabled',false);
            $('#invitetext').attr('disabled',false);
            $('#sendinvite').attr('checked', sendinvite);
        }
    });

    $('#past').click(function(){
        $(this).hide();
        $('#pastSelect').show();
        return false;
    });

    $('.select').click(selectUsers);
    $('.show').click(showUsers);

    $("#attendees").tablesorter({sortList: [[1,0]], headers:{1:{sorter:'lastname'}, 5:{sorter:'status'}} }).bind("sortEnd",restripe);


});

    $.tablesorter.addParser({
        // set a unique id
        id: 'lastname',
        is: function(s) {
            // return false so this parser is not auto detected
            return false;
        },
        format: function(s) {
            // format your data for normalization
            return s.toLowerCase().substr(s.indexOf("</span>"));
            //return s.toLowerCase().replace(/good/,2).replace(/medium/,1).replace(/bad/,0);
        },
        // set type, either numeric or text
        type: 'text'
    });

    $.tablesorter.addParser({
        // set a unique id
        id: 'status',
        is: function(s) {
            // return false so this parser is not auto detected
            return false;
        },
        format: function(s) {
            // format your data for normalization

            r = s.replace(/Yes/,0).replace(/Maybe \(Y\)/,1).replace(/Maybe \(N\)/,2).replace(/None/, 4).replace(/No/, 3);

            if(r == '') r = 5;

            return r;
            //return s.toLowerCase().replace(/good/,2).replace(/medium/,1).replace(/bad/,0);
        },
        // set type, either numeric or text
        type: 'numeric'
    });

function restripe() {
    row = false;
    
    $('#attendees tr.row:visible').each(function(){

          $(this).removeClass('RowOne').removeClass('RowTwo');
          row = !row;
          $(this).addClass(row ? 'RowOne' : 'RowTwo' );
    });
}

function selectUsers() {
    find = $(this).attr('data-select');

    $('#attendees tr:visible input:checkbox').each(function(){

        status = $(this).attr('data-status').substr($(this).attr('data-status').length-1,1);

        $(this).attr('checked', status==find || find=="X");
    });

    return false;
}

function showUsers() {

    find = $(this).attr('data-select');
    $('.link_selected').removeClass('link_selected');
    $(this).addClass('link_selected');

    row = false;

    $('#attendees tr.row').each(function(){

        status = $(this).attr('data-status').substr($(this).attr('data-status').length-1,1);

        if(status==find || find=="X") {
          $(this).show();
          $(this).removeClass('RowOne').removeClass('RowTwo');
          row = !row;
          $(this).addClass(row ? 'RowOne' : 'RowTwo' );
        } else {
            $(this).hide();
            $(this).find('input:checkbox').attr('checked', false);
        }
    });

    return false;
}

function blurFind() {
    if($(this).val() == "Type A Name" || $(this).val() == "") {
        $(this).addClass('filterOff').val("Type A Name");
    }
}

function focusFind() {
    if($(this).val() == "Type A Name") {
        $(this).val("");
    }
    $(this).removeClass('filterOff');
}

function focusFilter() {
    if($(this).val() == "filter") {
        $(this).val("");
    }
    $(this).removeClass('filterOff');
}

function focusBlur() {
    if($(this).val() == "filter" || $(this).val() == "") {
        $(this).addClass('filterOff').val("filter");
    }
}

function filterAttendees() {

    search = $(this).val() == "filter" ? "" : $(this).val();

    $('#attendees tr').each(function(){
        hide = true;
        fields = $(this).find('.filter_field');

        if(fields.length == 0) {
            return;
        }

        $(fields).each(function(){
            if($(this).text().match(new RegExp(search,"ig"))) {
                hide = false;
            }
        });

        if(hide) {
            $(this).hide();
        } else {
            $(this).show();
        }
    });
}

function findMatches() {
    
    namesplit = $(this).val().split(" ");
    namestring = "";
    lastname = "";

    count = 0;

    $.each(namesplit,function(){
        if( count > 0 ) {
            lastname += " " + this;
        }
        if(this.length > 0) {
            namestring += "|"+this ;
            count++;
        }
    });

    $('#firstname').val(namesplit[0]);
    $('#lastname').val(lastname.substr(1));

    count = 0;

    if(namestring.length > 0) {
    $('#possiblePeople li').each(function(){
        
            if(count < 10 && $(this).text().match(new RegExp("(" + namestring.substr(1) + ")","ig"))) {
                $(this).show();
                count++;
            } else {
                $(this).hide();
            }
    });
    }

    if($(this).val().length == 0 || $(this).val() == "Type A Name") {
        $('#foundPeople').hide();
    } else {
        $('#foundPeople').show();
    }
    
}