$(document).ready(function() {

    
    loadPosts( base+'/whiteboards/posts');
    
})

function loadPosts(url) {
    $.ajax({
        url: url,

        success: function(data) {
            $('#whiteboard').html(data);
            $('#WhiteboardUpdateForm').validate({
                rules: {
                    "data[Whiteboard][whiteboard_type_id]": {
                      required: function(element) {
                          var text = $('#WhiteboardData').val();
                          if(text != '') return true;
                          return false;
                      }

                    }
                },
                submitHandler: function(form) {
                    $(form).ajaxSubmit({
                        success: function() {
                            loadPosts( base+'/whiteboards/posts')
                        },
                        beforeSubmit: function() {
                            $("#WhiteboardSubmit").attr('disabled','true');
                            $("#WhiteboardSubmit").attr('value','Please wait');
                        }
                    });
                    
                }
                
            });
            $('.delete-post').click(function() {
                //alert($(this).attr('href'));
                loadPosts($(this).attr('href'));
                return false;
            });
            $('#WhiteboardWhiteboardTypeId').change(function() {
                var labels = new Array()
                labels[1] = 'Paste the video "embed code" into the box below';
                labels[2] = 'Put the link into the box below';
                labels[3] = 'Just type away';
                labels[4] = 'Place the url of the image below';
                var id = $(this).val();
                $('#WhiteboardData').prev('label').html(labels[id]);
            })
        }
    });
}
