{{!
    This file is part of Moodle - http://moodle.org/

    Moodle is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Moodle is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    Edwiser RemUI 
    @package    theme_remui
    @copyright  (c) 2018 WisdmLabs (https://wisdmlabs.com/)
    @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 
}}
</div>
{{{ output.standard_after_main_region_html }}}
    <!-- End Page -->

    {{> theme_remui/footer }}

{{#js}}
    {{#enabledictionary}}
        require(['jquery'], function (jQuery) {
            {{^editingenabled}}
            // Functions and code for Dictionary functionality

            // function getSelected data will give the selected text
            function getSelectedText() {
                if (window.getSelection) {
                    return window.getSelection().toString();
                } else if (document.selection) {
                    return document.selection.createRange().text;
                }
                return '';
            }

            // Event trigger for text selection
            jQuery('body').mouseup(function(e){
                var search = getSelectedText();
                if(search.trim() == ""){
                    return;
                }
                var obj = getSelectionCoords();
                if(obj.x != 'undefined' && obj.y != 'undefined'){
                    // var x = obj.x-152;
                    var x = obj.x-122;
                    x += 'px';
                    // var y = obj.y+12;
                    var y = obj.y+17;
                    y += 'px';
                    getWord(x,y,'headword',search);
                }
            });

            // Ajax to fetch the word meaning
            function getWord(x,y,token,search){
                searchword = token+'='+search;
                var url = "http://api.pearson.com/v2/dictionaries/entries?"+searchword;
                var settings = {
                    "async": true,
                    "crossDomain": true,
                    "url": url,
                    "method": "GET",
                    "success": function(response) {
                  
                        if(response != '' && response != null){
                            jQuery("#definition_layer").remove();
                        
                            var meaning = 'No definition found.';
                            var i=0;
                            for(i=0;i<response.results.length;i++){
                                if(response.results[i].senses[0].definition != null){
                                    meaning = response.results[i].senses[0].definition;
                                    break;
                                }
                            }
                            var data = "<div id='definition_layer' style='position:fixed; cursor:pointer;left:"+x+";top:"+y+";z-index:9999;'>";
                            data += '<div class="popover bs-popover-bottom w-400"><div class="arrow"></div><h3 class="popover-header">'+search;
                            data += '</h3><div class="popover-body"><p>' + meaning + '</p></div></div>';
                            data += '</div>';
                            jQuery("body").append(data);  
                            
                        }
                    },
                    "failure": function(error) {
                    
                    }
                }
                jQuery.ajax(settings);
            }

            // Close the Tooltip Event
            jQuery(window).scroll(function(){
               jQuery("#definition_layer").remove();
            });

            jQuery('body').click(function(){
                jQuery("#definition_layer").remove();
            });

            // Get location of selected text
            function getSelectionCoords(win) {
               win = win || window;
               var doc = win.document;
               var sel = doc.selection, range, rects, rect;
               var x = 0, y = 0;
               if (sel) {
                if (sel.toString().trim() == "" || sel.toString().trim() == "undefined") {
                      return false;
                   }
                   if (sel.type != "Control" && sel.toString != "") {
                       range = sel.createRange();
                       range.collapse(true);
                       x = range.boundingLeft;
                       y = range.boundingTop;
                   }
               } else if (win.getSelection) {
                   sel = win.getSelection();
                   console.log("SELECTED : "+sel.toString());
                   if (sel.toString().trim() == "" || sel.toString().trim() == "undefined") {
                      return false;
                   }
                   if (sel.rangeCount && sel.toString() != "") {
                       range = sel.getRangeAt(0).cloneRange();
                       if (range.getClientRects) {
                           range.collapse(true);
                           rects = range.getClientRects();
                           if (rects.length > 0) {
                               rect = rects[0];
                           }
                           x = rect.left;
                           y = rect.top;
                       }
                       // Fall back to inserting a temporary element
                       if (x == 0 && y == 0) {
                           var span = doc.createElement("span");
                           if (span.getClientRects) {
                               // Ensure span has dimensions and position by
                               // adding a zero-width space character
                               span.appendChild( doc.createTextNode("\u200b") );
                               range.insertNode(span);
                               rect = span.getClientRects()[0];
                               x = rect.left;
                               y = rect.top;
                               var spanParent = span.parentNode;
                               spanParent.removeChild(span);

                               // Glue any broken text nodes back together
                               spanParent.normalize();
                           }
                       }
                   }
               }
               return { x: x, y: y };
            }
            {{/editingenabled}}
        });

    {{/enabledictionary}}
{{/js}}
</body>
</html>