$(document).ready(function () {

    var drillshown = false;
   
    $(window).scroll(function () {  console.log(drillshown) });

    /* Alert notifications */
    var hulla = new hullabaloo();
    hulla.send("OneView loaded successfully", "success");
    setTimeout(function () {
        hulla.send("Please update you details", "info");
    }, 4000);

    setTimeout(function () {
        hulla.send("You have 15 new tickets", "warning");
    }, 8000);

    $('li.dropdown.notifications a').on('click', function (event) {
        $('li.dropdown.notifications').removeClass('show');
        $('li.dropdown.notifications .dropdown-menu').removeClass('show');
        $(this).parent().addClass('show');
        $(this).siblings(".dropdown-menu").addClass('show');
    });

    $('body').on('click', function (e) {
        if (!$('li.dropdown.notifications').is(e.target)
            && $('li.dropdown.notifications').has(e.target).length === 0
            && $('.open').has(e.target).length === 0
        ) {
            $('li.dropdown.notifications').removeClass('show');
            $('li.dropdown.notifications .dropdown-menu').removeClass('show');

        }
    });


    /* Expand/compress tags panel */

    $('#expandtags').click(function () {
        $('#tags, #graphwrapper').toggleClass("expanded");
    });

    /* change icon in filter input and clear on click*/

    $(".material-input input").focus(function () {
        $(this).siblings("i").addClass("icon-close");
        $(this).siblings("i").removeClass("icon-filter");
        $(this).siblings("i").removeClass("icon-search");
        $(this).siblings("i.icon-close").removeClass("disabled");

        $(this).siblings("i").click(function () {
            $(this).siblings("input").val('');
            $(this).addClass("disabled");
            $(this).addClass("icon-filter");
            $(this).removeClass("icon-close");
        });
    });

    $(".material-input input:not(.unitsearch)").focusout(function () {
        if ($(this).val() == "") {
            $(this).siblings("i.icon-close").addClass("disabled");
            $(this).siblings("i.icon-close").addClass("icon-filter");
            $(this).siblings("i.icon-close").removeClass("icon-close");

        }
    });

    $(".material-input .unitsearch").focusout(function () {
        if ($(this).val() == "") {
            $(this).siblings("i.icon-close").addClass("icon-search");
            $(this).siblings("i.icon-close").removeClass("icon-close");

        }
    });



    $("#newuserModal .material-input input").focus(function () {
        $(this).siblings(".icon-details").show();
        $(this).siblings(".validationIconStackWrapper").show();

        $(this).addClass("alertborder");
    });

    $("#newuserModal .material-input input").focusout(function () {
        $(this).siblings(".icon-details").hide();
        $(this).siblings(".validationIconStackWrapper").hide();

        $(this).removeClass("alertborder");
    });



    /* input Error demo */
    /*
   $(".material-input input").focus(function () {
       //$(this).siblings("error").slideDown();
       $(this).siblings("error").addClass("shown");
       $(this).closest("th").animate({
           height: 71
       }, 200);
   
       
   });
   
   $(".material-input input").focusout(function () {
       //$(this).siblings("error").slideUp();
       $(this).siblings("error").removeClass("shown");
       $(this).closest("th").animate({
           height: 61
       }, 200);
   
   });*/

    $("input").focus(function () {
        $(this).next("inlineerror").addClass("shown");
        /*$(this).closest("th").animate({
            height: 71
        }, 200);*/


    });




    /* Ticket Modal */
    $(".icon-createticket").click(function () {
        $('#ticketModal').modal({
            show: true
        });
    });

    $('#ticketModal').modal({
        show: true
    });

    /* Reports modal */
    $(".reportPeriodOk").click(function () {
        $('#reportModal').modal({
            show: true
        });
        $(".reportloading").delay("6000").fadeOut("500", function () {

            $(".reportloadsuccess").fadeIn("800");
            $("#reportModalLabel").text("Report loaded");
        });
    });

    $("#cancelReport").click(function () {
        $('#reportModal').modal("hide");
    });

    /* Loader icon */
    $(".loader").delay("1000").fadeOut("500", function () {
        $(".status h1").fadeIn("500", function () {
            $(".status h2").fadeIn("400");
            $(".status h6").fadeIn("400");

        });
        $(".hiddenwhileloading").fadeIn("800");
    });


    /* show all alarm types and recipients */


    $("#showallalarms").click(function () {
        $("#selectednotificationwrapper").toggleClass("fullheight");
        $("#notificationwrapper").toggleClass("fullheight");

        $("#showallalarms span").text(function (i, text) {
            return text === "Show all" ? "Show less" : "Show all";
        })
    });

    /* settings panel */
    $(".settings").click(function () {
        $("#settings").toggleClass("active");
    });


    /* Show drill container */
    $(".detailbtn").click(function () {
        drillshown = true;

        $(".detailbtn").removeClass("active");
        $(".detailbtn").addClass("inactive");
        $(".drill-drop").addClass("inactive");

        $(".datarow").removeClass("active");

        $(this).removeClass("inactive").addClass("active");
        $(this).closest(".datarow").addClass("active");

        $("#filterbuttons").fadeOut();
    });

    $(".detailbtn").click(function () {
        var visualheight = $(".drillcontainer").height();
        var xoffset = $("#titletd").width() + $("#actiontd").width() + 90;
        var drillwidth = $("#monitorwrapper").width() - xoffset;

        console.log(xoffset + '|' + visualheight + "|" + drillshown);

        $("#tablewrapper").stop().animate({
        }, 1000, function () {

            $(".closeDetail").fadeIn(500, function () {
            });

            $("#tablewrapper").stop().animate({
                width: 340
            }, 1000);

            //$('#filterbuttons').slideUp("fast");
            $('#tablewrapper').height(visualheight - 30);
            $("#tablewrapper").css(
                "overflow-y", "scroll"
            );
        });

        $(".drillcontainer").stop().animate({
            left: xoffset + 25,
            width: drillwidth
        }, 1000, function () {

            $("#monitorwrapper").stop().animate({
            }, 300);
        });
    });

    /* Hide Drillcontainer */

    $(".closeDetail").click(function () {
        drillshown = false;

        $(".detailbtn").removeClass("active");
        $(".detailbtn").removeClass("inactive");
        $(".datarow").removeClass("active");




        $(".drillcontainer").stop().animate({
            left: "100%"
        }, 400, function () {

        });

        $("#tablewrapper").stop().animate({
            width: "100%",
            height: "100%",


        }, 400, function () {

            $(".closeDetail").fadeOut(500, function () {
            });

            $("#tablewrapper").css(
                "overflow-y", "hidden"
            );

            $("#filterbuttons").fadeIn("slow");

        });

        $("#monitorwrapper").stop().animate({
            height: "auto",
            overflow: "hidden"
        }, 300);

    });

    /* Maximize drill container */
    function maximize() {
        $(".drillcontainer").stop().animate({
            left: 25,
            width: $("#monitorwrapper").width() + 5
        }, 400, function () {

            var visualheight = $(".drillcontainer").height();

            $("#monitorwrapper").stop().animate({
                height: visualheight + 50
            });
        });
    }

    /* Minimize drill container */
    function minimize() {
        var xoffset = $("#titletd").width() + $("#actiontd").width() + 90;
        var drillwidth = $("#monitorwrapper").width() - xoffset + 30;
        $(".drillcontainer").stop().animate({
            left: xoffset,
            width: drillwidth,
        }, 400, function () {

            var visualheight = $(".drillcontainer").height();

            $("#monitorwrapper").stop().animate({
                height: visualheight + 50
            });
        });
    }

    $(".maximize").click(function () {
        return (this.tog = !this.tog) ? maximize() : minimize();
    });


    /* Full screen drill container */
    $(function () {
        $('#supported').text('Supported/allowed: ' + !!screenfull.enabled);

        if (!screenfull.enabled) {
            return false;
        }

        $('#request').click(function () {
            screenfull.request($('#container')[0]);
        });

        $('#exit').click(function () {
            screenfull.exit();
        });

        function fullscreenchange() {
            var elem = screenfull.element;

            $('#status').text('Is fullscreen: ' + screenfull.isFullscreen);

            if (elem) {
                $('#element').text('Element: ' + elem.localName + (elem.id ? '#' + elem.id : ''));
            }

            if (!screenfull.isFullscreen) {
                $('#external-iframe').remove();
                document.body.style.overflow = 'auto';
            }
        }

        screenfull.on('change', fullscreenchange);

        fullscreenchange();
    });

    /* Switch color theme */

    $('#themeswitch').click(function () {
        $('body').toggleClass("theme-light theme-dark");
    });

    $('.tree-toggle').click(function () {
        $(this).parent().parent().children('ul.tree').toggle(200);
    });

    $(function () {
        $('.tree-toggle').parent().parent().children('ul.tree').toggle(200);
    })

/* Open configuration error modal on load with # */

$(function() {

    if(window.location.href.indexOf('#error') != -1) {
      $('#configerrorModal').modal('show');
    }
  
  });


    /* resize and scroll on unit details sidebar demo */

$(function () {
    $window = $(window);

    
        $window.scroll(function () {

            var distance = $('#monitorwrapper').offset().top - 66
            var top = $(window).scrollTop() - 146 + "px";

            console.log("hit |" + distance + "|" + $(window).scrollTop());

            if ($window.scrollTop() >= distance && (drillshown === true)) {

                $("#tablewrapper").css("transform", "translateY(" + top + ")");
                $("#tablewrapper").css("height", "calc(100vh - 146px)");
                var s1 = $('#monitorwrapper').height()+20;
                var s2 = $('#unitdetails').height()+20;

                if (s1 > s2)
                    $('#unitdetails').css('height', s1 + "px");
                else
                    $('#monitorwrapper').css('height', s2 + "px");
            } else {
                $("#tablewrapper").css("height", "auto");
                $("#unitdetails").css("height", "auto");
                $("#monitorwrapper").css("height", "auto");
            }
        });
  
});

});


/* set height of report sidebar*/
$(function () {
    var reportheight = $(".reportcontent .col-12").height();
    $(".reportsidebar").css("height", reportheight + 63);
});


/* Tooltip */

$(function () {
    $('[data-toggle="tooltip"]').tooltip()
});














/* Tooltip custom class */

$(function () {

    if (typeof $.fn.tooltip.Constructor === 'undefined') {
        throw new Error('Bootstrap Tooltip must be included first!');
    }

    var Tooltip = $.fn.tooltip.Constructor;

    // add customClass option to Bootstrap Tooltip
    $.extend(Tooltip.Default, {
        customClass: ''
    });

    var _show = Tooltip.prototype.show;

    Tooltip.prototype.show = function () {

        // invoke parent method
        _show.apply(this, Array.prototype.slice.apply(arguments));

        if (this.config.customClass) {
            var tip = this.getTipElement();
            $(tip).addClass(this.config.customClass);
        }

    };
});