$(document).ready(function () {
    LoadMainMenu();
    LoadContents();
});

function LoadMainMenu() {
    var DoctorData = {
        dml_indicator: 'SD',
        doctor_id: $('#myHiddenVar').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Template/Help_Document_Ins_Upd_Del',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {
            if (jQuery.isEmptyObject((data))) {
            }
            else {
                debugger;
                var data1 = JSON.parse(data);
                $('#doc-menu').empty();
                var row = '';
                for (var i = 0; i < data1.Table.length; i++) {
                    row = row + '<a class="nav-link scrollto" href="#menu' + data1.Table[i].menu + '">' + data1.Table[i].doctor_module_name + '</a><ul id="stepID' + data1.Table[i].menu + '"></ul>'

                }
                $('#doc-menu').html(row);
            }
        }
    });
}

function LoadContents() {
    var DoctorData = {
        dml_indicator: 'SD',
        doctor_id: $('#myHiddenVar').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Template/Help_Document_Ins_Upd_Del',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {
            if (jQuery.isEmptyObject((data))) {
            }
            else {
                debugger;
                var data1 = JSON.parse(data);
                $('#content').empty();
                
                
               
                for (var i = 0; i < data1.Table.length; i++) {
                    debugger;
                    
                    var menuid=data1.Table[i].menu;
                    
                    var subdata = {
                        dml_indicator: 'S',
                        menu: menuid
                    }
                    $.ajax({
                        async: true,
                        type: 'POST',
                        url: '/Template/Help_Document_Ins_Upd_Del',
                        contentType: 'application/json; charset=UTF-8',
                        data: JSON.stringify(subdata),
                        success: function (data) {
                            if (jQuery.isEmptyObject((data))) {
                            }
                            else {
                                debugger;
                                var subdata1 = JSON.parse(data);
                                var mainMenu = '';
                                var submenu = '';
                                var sideMainMenu = '';
                                var menuID = '';
                                var document = '';
                                var url = $('#apiurl').val();
                                for (var k = 0; k < subdata1.Table.length; k++) {
                                    menuID = subdata1.Table[k].menu;
                                    sideMainMenu = sideMainMenu + '<li><a class="nav-link scrollto" href="#step' + subdata1.Table[k].sub_menu + '">'+ subdata1.Table[k].sub_menu +'</a></li>'
                                    mainMenu = '<section id="menu' + subdata1.Table[k].menu + '" class="doc-section"><h2 class="section-title">' + subdata1.Table[k].doctor_module_name + '</h2>'

                                    //attachement
                                    if (subdata1.Table[k].document != '' && subdata1.Table[k].document != "null" && subdata1.Table[k].document != null) {
                                        var documentArray = subdata1.Table[k].document.split(',');
                                        if (documentArray.length > 0) {
                                            for (var l = 0; l < documentArray.length; l++) {
                                                if (documentArray[l].split('.').pop() == "doc") {
                                                    document = document + '<div class="col-md-12"><iframe src="https://docs.google.com/viewer?url=' + url + "HelpDocuments/" + documentArray[l] + '&embedded=true" frameborder="0"></iframe></div></br>';
                                                }
                                                else if (documentArray[l].split('.').pop() == "pdf") {
                                                    document = document + '<div class="col-md-12"><iframe src=' + url + "HelpDocuments/" + documentArray[l] + '#toolbar=0></iframe></div></br>';
                                                }
                                                else {
                                                    document = document + '<div class="col-md-6"><img src="' + url + "HelpDocuments/" + documentArray[l] + '" id="imgStep' + subdata1.Table[k].menu + k + l + '" onmousemove=imageZoom1("imgStep' + subdata1.Table[k].menu + k + l + '","myresult' + subdata1.Table[k].menu + k + l + '")></div><div id="myresult' + subdata1.Table[k].menu + k + l + '" class="img-zoom-result col-md-6"></div></br>';
                                                }
                                                //document = document + '<embed src="' + url.replace('api/', "HelpDocuments/") + documentArray[l] + '" width="500" height="375"></br>';
                                            }
                                        }
                                    }
                                                                       
                                    submenu = submenu + '<div id="step' + subdata1.Table[k].sub_menu + '" class="section-block"><h3 class="block-title">' + (k + 1) + ') ' + subdata1.Table[k].sub_menu + '</h3><p>' + (subdata1.Table[k].content != null ? subdata1.Table[k].content : '') + '</p>' + document + '</div>'
                                   
                                    
                                }
                                
                                $('#stepID' + menuID).append(sideMainMenu);
                                //sectionHead = sectionHead + row + submenu;
                                $('#content').append( mainMenu + submenu + '</section>');
                            }
                        }

                    });
                    
                   
                }
                
                
            }
        }
    });
}

function imageZoom1(imgID, resultID) {

    var img, lens, result, cx, cy;
    img = document.getElementById(imgID);
    result = document.getElementById(resultID);
    /* Create lens: */
    lens = document.createElement("DIV");
    lens.setAttribute("class", "img-zoom-lens");
    /* Insert lens: */
    img.parentElement.insertBefore(lens, img);
    /* Calculate the ratio between result DIV and lens: */
    cx = result.offsetWidth / lens.offsetWidth;
    cy = result.offsetHeight / lens.offsetHeight;
    /* Set background properties for the result DIV */
    result.style.backgroundImage = "url('" + img.src + "')";
    result.style.backgroundSize = (img.width * cx) + "px " + (img.height * cy) + "px";
    /* Execute a function when someone moves the cursor over the image, or the lens: */
    lens.addEventListener("mousemove", moveLens);
    img.addEventListener("mousemove", moveLens);
    /* And also for touch screens: */
    lens.addEventListener("touchmove", moveLens);
    img.addEventListener("touchmove", moveLens);
    
    function moveLens(e) {
        var pos, x, y;
        /* Prevent any other actions that may occur when moving over the image */
        e.preventDefault();
        /* Get the cursor's x and y positions: */
        pos = getCursorPos(e);
        /* Calculate the position of the lens: */
        x = pos.x - (lens.offsetWidth / 2);
        y = pos.y - (lens.offsetHeight / 2);
        /* Prevent the lens from being positioned outside the image: */
        if (x > img.width - lens.offsetWidth) { x = img.width - lens.offsetWidth; }
        if (x < 0) { x = 0; }
        if (y > img.height - lens.offsetHeight) { y = img.height - lens.offsetHeight; }
        if (y < 0) { y = 0; }
        /* Set the position of the lens: */
        lens.style.left = x + "px";
        lens.style.top = y + "px";
        /* Display what the lens "sees": */
        result.style.backgroundPosition = "-" + (x * cx) + "px -" + (y * cy) + "px";
    }
    function getCursorPos(e) {
        var a, x = 0, y = 0;
        e = e || window.event;
        /* Get the x and y positions of the image: */
        a = img.getBoundingClientRect();
        /* Calculate the cursor's x and y coordinates, relative to the image: */
        x = e.pageX - a.left;
        y = e.pageY - a.top;
        /* Consider any page scrolling: */
        x = x - window.pageXOffset;
        y = y - window.pageYOffset;
        return { x: x, y: y };
    }
}

function ViewDocument(document, id) {
    debugger;
    var url = $('#apiurl').val();
    if (document != "null" && document != '') {
        //$('a#idDocument' + id).attr({
        //    target: '_self',
        //    href: url.replace('api/', "HelpDocuments/") + document
        //});
        var source = url + "HelpDocuments/" + document;
        var link = '<iframe id="fred" style="border:1px solid #666CCC" title="PDF in an i-Frame" src="' + source + '" frameborder="1" scrolling="auto" height="1100" width="850" ></iframe>'
        $('#iframeDiv').append(link);
    }
    else {
        $('a#idDocument').attr({
            href: "javascript:void(0);"
        });
    }
}

$('#btnSearch').click(function () {
    $('#divSearchSection').show();
    $('#divMainSection').hide();
    SearchContent();
});

function SearchContent() {
    var subdata = {
        dml_indicator: 'AS',
        sub_menu: $('#txtSearch').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Template/Help_Document_Ins_Upd_Del',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(subdata),
        success: function (data) {
            if (jQuery.isEmptyObject((data))) {
            }
            else {
                debugger;
                var subdata1 = JSON.parse(data);
                $('#searchContent').empty();
                var sideMainMenu = '';
                for (var k = 0; k < subdata1.Table.length; k++) {
                    sideMainMenu = sideMainMenu + '<li><a class="nav-link scrollto" href="#" id=idSearchResult onclick=\'ViewSearchResults(\"' + subdata1.Table[k].sub_menu + '\")\'>' + subdata1.Table[k].sub_menu + '</a></li>'
                   

                }
                $('#searchContent').append('<h3>Search Results<h3>'+ sideMainMenu);
            }
        }
    });
}

function ViewSearchResults(sub_menu) {
    $('#divSearchSection').hide();
    $('#divMainSection').show();
    $('a#idSearchResult').attr({
        href: "#step"+sub_menu
    });
    
}