 $(document).ready(function() {

    $(document).on('click', '.addthisExercise',(function() {
    // $("button.addthisExercise").click(function() {
        console.log("This exercise name li" + JSON.stringify($(this).closest('li').data()));
        var newElem = $('<li class=ui-state-default ><label for="element">Name</label><input name="exercisename" class="element" value="' + $(this).closest('li').data('name') + '"><input name="elements[][exerciseID]" class="hiddenelement" value="' + $(this).closest('li').data('exid') + '"><input name="elements[][Modifier]" class="element" value="' + $(this).closest('li').data('musclearrayMod') + '"><button type="button" class="addSet">Add Set</button><button type="button" class="delete">Delete Exercise</button><ul class="sets"><li class="set"><label for="element" name="elements[0][splits][0][reps]">elements[0][splits][0][reps]<br /></label><input name="elements[0][splits][0][reps]" class="element"><label for="element" name="elements[0][splits][0][weight]"><br />elements[0][splits][0][weight]<br /></label><input name="elements[0][splits][0][weight]" class="element"><button type="button" class="delete">Delete Set</button></li></ul></li>');
        $(newElem).data('musclearray', $(this).closest('li').data('musclearray'));
        $(newElem).data('musclearrayMod', $(this).closest('li').data('musclearrayMod'));
        $(newElem).appendTo('ul#exercises');
        $( ".hiddenelement" ).hide();
        $("button").button();
        $( "ul#exercises" ).sortable( "refresh" );
        $( "ul#sets" ).sortable( "refresh" );
        $('#newworkout').trigger('sortupdate');
      })
    );

    //filteredExerciseArray = filterarray(originalExerciselist);
    //PopulateExerciseList(arr);

            $(document).on('click', '.filter',(function (){
              var keyREGEX= "";
              if ($(".filter:checked").length > 0)
                {
                    // any one is checked
                    var len = $('.filter').length;
                    $(".filter").each(function(index, element) {

                        if ($(this).is (':checked'))
                        {
                            // Do stuff
                            keyREGEX += '('+ $(this).val() +')';
                        }
                        });
                    //keyREGEX += "'";
                    keyREGEX = keyREGEX.replace(")(", ")|(");
                } else {
                   // none is checked
                   keyREGEX = '(none is checked)';
                }

                console.log("Regex= " + keyREGEX);
                filteredExerciseArray = filterarray(originalExerciselist, keyREGEX);
                PopulateExerciseList(filteredExerciseArray);
                console.timeEnd('Filter');
            }));


            function  filterarray(input, regex){
                console.log('passed regex' + regex);
                var keyRE=new RegExp(regex);
                var arr = jQuery.grep(input, function(n, i){
                            return keyRE.test(n.keywords);
                            // return $.grep(n.keywords, function (item, i) {
                            //     return keyRE.test(item);
                            // });
                });
                console.log("arr " +  arr.length);
                return arr;
            }

/////////////////////////////////
//  Pseudocode for  1RM calcs

// for each exercise in workout
//   get H1RM for exercise
//   C1RMsum = 0 
//   for each set in exercise
//     calculate C1RM and store in array
//   find highest C1RM in array
//   if C1RM > H1RM then
//     H1RM = C1RM
//     set comment to "yay"
//   for each value in array
//     set[index] = value/H1RM
//    C1RMsum += value/H1RM
//   Done


//  assume  sum sumarray * each in musclearray



////// Make below standalone funtion "updateExerciselistDIV"
function PopulateExerciseList(exerciselist) {
    console.time('PopulateExList');
     var newExerciselist  = $('<div></div>');
        $.each(exerciselist,  function (i, item) {
            //console.log("Exercisename = " + item.name);
             var newElem = $('<li>'+item.name + ' - ' + item.description + ' - ' + item.keywords +'<button type="button" class="addthisExercise">+</li>');
             $(newElem).data('name', item.name);
             $(newElem).data('exid', item._id);
             $(newElem).data('musclearray', item.musclearray);
             //$(newElem).data('musclearrayMod', item.musclearrayMod);
             $(newElem).data('datafields', item.datafields);
             newExerciselist.append(newElem);
        });
        console.log("newexlist outside= " + newExerciselist);
        $('#exerciselist').empty();
        $(newExerciselist).appendTo('#exerciselist');
        $("button").button();
        console.timeEnd('PopulateExList');
    }

    var originalExerciselist = $.getValues("/api/exerciselist");
    PopulateExerciseList(originalExerciselist);
});
