$(document).ready(function() {
     //makes buttons buttons
     $("button").button();

    //makes datepickers
    $( ".datepicker" ).datepicker();
    //$('#newworkout').find('input.datepicker').datepicker();
    $('.datepicker').datepicker('setDate', new Date());
    //$('.datepicker').datepicker("hide");

     $("button#addExercise").click(function() {
        console.log("click!");
        var newElem = $('<li><label for="element">elements[0][exerciseID]<br /></label><input name="elements[][exerciseID]" class="element"><button type="button" class="addSet">Add Set</button><button type="button" class="delete">Delete Exercise</button><ul class="sets"></ul></li>');
        $(newElem).appendTo('#exercises');
        $("button").button();
        $('#newworkout').trigger('sortupdate');
        });

    $(document).on('click', '.addSet',(function() {
        console.log("click! set");
        var newElem = $('<li><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>');
        $(this).siblings('ul.sets').append(newElem);
        $("button").button();
        $('#newworkout').trigger('sortupdate');
        }));

     $(document).on('click', '.delete',function() {
           $(this).closest('li').remove();
           $('#newworkout').trigger('sortupdate');
     });



    $("#newworkout").bind('sortupdate', function(event, ui) {
        console.log ('sortupdate');
        $('#newworkout > ul > li').each(function(){
            var itemindex= $(this).index();
            var newname = $(this).find('input, select').attr('name').replace(/elements\[[0-9]*\]/,'elements[' + itemindex  + ']');

            $(this).children('label').html(newname);
            $(this).children('input').attr('name', newname);

                $(this).find('ul li').each(function(){
                    var itemindex2= $(this).index();
                    $(this).children('input').each(function(){
                        var newname2 = $(this).attr('name').replace(/\[splits\]\[[0-9]*\]/,'[splits][' + itemindex2  + ']');
                        newname2 = newname2.replace(/elements\[[0-9]*\]/,'elements[' + itemindex  + ']');
                        $(this).attr('name', newname2);
                      });

                     $(this).children('label').each(function(){
                         var newname2 = $(this).attr('name').replace(/\[splits\]\[[0-9]*\]/,'[splits][' + itemindex2  + ']');
                         newname2 = newname2.replace(/elements\[[0-9]*\]/,'elements[' + itemindex  + ']');
                         $(this).html(newname2);
                       });
                });



        });
        return false;
    });

        $('#newworkout').trigger('sortupdate');




jQuery.validator.addMethod("notEqual", function(value, element, param) {
  return this.optional(element) || value != param;
},"Please select a value");



});

