<%! from django.utils.translation import ugettext as _ %>

<div class="annotatable-wrapper">
    <div class="annotatable-header">
        % if display_name is not UNDEFINED and display_name is not None:
            <div class="annotatable-title">${display_name}</div>
        % endif
    </div>
    % if instructions_html is not UNDEFINED and instructions_html is not None:
    <div class="annotatable-section shaded">
        <div class="annotatable-section-title">
            ${_('Instructions')}
            <a class="annotatable-toggle annotatable-toggle-instructions expanded" href="javascript:void(0)">${_('Collapse Instructions')}</a>
        </div>
        <div class="annotatable-section-body annotatable-instructions">
            ${instructions_html}
        </div>
    </div>
    % endif
    <div class="annotatable-section">
        <div class="annotatable-content">
            <div id="textHolder">${content_html}</div>
            <div id="sourceCitation">${_('Source:')} ${source}</div>
            <div id="catchDIV">
                <div class="annotationListContainer">${_('You do not have any notes.')}</div>
            </div>
        </div>
    </div>
</div>


<script>
    function onClickHideInstructions(){
        //Reset function if there is more than one event handler
        $(this).off();
        $(this).on('click',onClickHideInstructions);
        var hide = $(this).html()=='Collapse Instructions'?true:false,
            cls, txt,slideMethod;
        txt = (hide ? 'Expand' : 'Collapse') + ' Instructions';
        cls = (hide ? ['expanded', 'collapsed'] : ['collapsed', 'expanded']);
        slideMethod = (hide ? 'slideUp' : 'slideDown');
        $(this).text(txt).removeClass(cls[0]).addClass(cls[1]);
        $(this).parents('.annotatable-section:first').find('.annotatable-instructions')[slideMethod]();
    }
    $('.annotatable-toggle-instructions').on('click', onClickHideInstructions);
    
    //Grab uri of the course
    var parts = window.location.href.split("/"), 
        uri = '';
    for (var index = 0; index <= 9; index += 1) uri += parts[index]+"/"; //Get the unit url
    //Change uri in cms
    var lms_location = $('.sidebar .preview-button').attr('href');
    if (typeof lms_location!='undefined'){
        uri = window.location.protocol;
        for (var index = 0; index <= 9; index += 1) uri += lms_location.split("/")[index]+"/"; //Get the unit url
    }
    var unit_id = $('#sequence-list').find('.active').attr("data-element");
    uri += unit_id;
    var pagination = 100,
        is_staff = !('${user.is_staff}'=='False'),
        options = {
        optionsAnnotator: {
            permissions:{
                user: {
                    id:"${user.email}", 
                    name:"${user.username}"
                },
                userString: function (user) {
                    if (user && user.name) 
                        return user.name;
                    return user;
                },
                userId: function (user) {
                    if (user && user.id) 
                        return user.id;
                    return user;
                },
                permissions: {
                        'read':   [],
                        'update': ["${user.email}"],
                        'delete': ["${user.email}"],
                        'admin':  ["${user.email}"]
                },
                showViewPermissionsCheckbox: true,
                showEditPermissionsCheckbox: false,
                userAuthorize: function(action, annotation, user) {
                    var token, tokens, _i, _len;
                    if (annotation.permissions) {
                      tokens = annotation.permissions[action] || [];
                      if (is_staff){
                          return true;
                      }
                      if (tokens.length === 0) {
                        return true;
                      }
                      for (_i = 0, _len = tokens.length; _i < _len; _i++) {
                        token = tokens[_i];
                        
                        if (this.userId(user) === token) {

                          return true;
                        }
                      }

                      return false;
                    } else if (annotation.user) {
                      if (user) {
                        return this.userId(user) === this.userId(annotation.user);
                      } else {
                        return false;
                      }
                    }
                    return true;
                  },
            },
            auth: {
                token: "${token}"
            },
            store: {
                // The endpoint of the store on your server.
                prefix: "${annotation_storage}",

                annotationData: {
                    uri: uri,
                    citation: "${source}"
                },

                urls: {
                    // These are the default URLs.
                    create:  '/create',
                    read:    '/read/:id',
                    update:  '/update/:id',
                    destroy: '/delete/:id',
                    search:  '/search'
                },

                loadFromSearch:{
                    limit:pagination,
                    offset:0,
                    uri:uri,
                    media:'text',
                    userid:'${user.email}',
                }
            },
            highlightTags:{
                tag: "${tag}",
            },
            diacriticMarks:{
                diacritics: "${diacritic_marks}"
            }
        },
        optionsVideoJS: {techOrder: ["html5","flash","youtube"]},
        optionsRS: {},
        optionsOVA: {posBigNew:'none'},
        optionsRichText: {
            tinymce:{
                selector: "li.annotator-item textarea",
                plugins: "media image insertdatetime link code",
                menubar: false,
                toolbar_items_size: 'small',
                extended_valid_elements : "iframe[src|frameborder|style|scrolling|class|width|height|name|align|id]",
                toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image media rubric | code ",
            }
        },
    };

    var imgURLRoot = "${settings.STATIC_URL}" + "js/vendor/ova/catch/img/";
    tinyMCE.baseURL = "${settings.STATIC_URL}" + "js/vendor/ova";
    
    //remove old instances
    if (Annotator._instances.length !== 0) {
      $('#textHolder').annotator("destroy");
    }
    delete ova;
    //Load the plugin Video/Text Annotation
    var ova = new OpenVideoAnnotation.Annotator($('#textHolder'),options);
    
    //Catch
    var annotator = ova.annotator,
        catchOptions = {
            media:'text',
            externalLink:false,
            imageUrlRoot:imgURLRoot,
            showMediaSelector: false,
            showPublicPrivate: true,
            userId:'${user.email}',
            pagination:pagination,//Number of Annotations per load in the pagination,
            flags:is_staff
        },
        Catch = new CatchAnnotation($('#catchDIV'),catchOptions);
</script>
