
$(document).ready(function () {


    $('.icon-setpoint').click(function (e) {
         e.stopPropagation();

        $(this).next().find(".setpoint").slideDown( "fast", function() {
            // Animation complete.
          });
    });

    $('.icon-setpoint').next().find("button").click(function (e) {
       $(this).closest(".setpoint").slideUp( "fast", function() {
           // Animation complete.
         });
         e.stopPropagation();
     
   });


  /*  $(document).on('click', '.icon-setpoint', function (e) {

        e.stopPropagation();
    });*/


    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');

        }
    });

/* position pitch wings */

function poswing() {
    var yoffset = 75;

    if ($(window).width() > 3000) {
        yoffset = 80;
     }
    
    
    var wingwidth = $(".pitchgraph").width(); 
    var ypos = Math.floor(((wingwidth * 0.73)) - yoffset);
    $(".arrow").css("bottom", ypos + "px");
};

poswing();
   


    /* 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 + '| monitorwrapper width:' + $("#monitorwrapper").width() + '| offset:' + xoffset + '| width:' + drillwidth);

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

          
            enterdrill();

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

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

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

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

          
            enterdrill();

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

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

            //$('#filterbuttons').slideUp("fast");
        
            $("#tablewrapperScroll").css(
                "overflow-y", "scroll"
            );
        });

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


    $(".nav-tabs .nav-item").click(function () { 

        setTimeout(function(){
            var visualheight = $(".drillcontainer").height()+35;

            $("#monitorwrapper").animate({height: visualheight},100);

        }, 50);

    });



    /* Hide Drillcontainer */

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


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




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

        });

        $("#tablewrapper, #tablewrapperScroll").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);

    });


    $(window).on('resize', function(){
        if (drillshown){
        var wrapperheight = $(".drillcontainer").height()+35;
        var xoffset = $("#titletd").width() + $("#actiontd").width() + 90;
        var drillwidth = $("#monitorwrapper").width() - xoffset;
            console.log('Resized' + drillwidth + '| Offset:' + xoffset + '| monitorwrapper width:' + $("#monitorwrapper").width());

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

        $("#monitorwrapper").stop().animate({height: wrapperheight},100);

    }
    });
 
    



    /* Maximize drill container */
    function maximize() {
        $(".maximize").toggleClass("icon-max icon-minimize");
        $(".drillcontainer").addClass("maximized");
        
        $(".drillcontainer").stop().animate({
            left: 25,
            width: $("#monitorwrapper").width()
        }, 400, function () {

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

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

    /* Minimize drill container */
    function minimize() {
        $(".maximize").toggleClass("icon-max icon-minimize");
        $(".drillcontainer").removeClass("maximized");

        var xoffset = $("#titletd").width() + $("#actiontd").width() + 90;
        var drillwidth = $("#monitorwrapper").width() - xoffset + 25;
        $(".drillcontainer").stop().animate({
            left: xoffset,
            width: drillwidth,
        }, 400, function () {

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

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

        });
    }

    $(".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()
});







$(document).on('click', '.dropdown-menu.dropdownWithSelect', function (e) {
    e.stopPropagation();
  });

 


  $(function () {
  $("#budgetDropdown").css({
    'width': $("#budgetDropDownTrigger").width() + 42 + ('px')
  });
});




/* 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);
        }

    };
});


/* Value presentation tooltip */
$(function () {
    $('.valuepres').each(function(e) {
        $(this).append('<span class="valueprestooltip">' + $(this).data('tooltip') + '</span>');
    });
});



$(function () {


   /* $(document).bind('touchstart click', function (e) {
        $(".valuepres").removeClass('is-active');
    });*/



    $('.valuepres').bind('touchend click', function(e) {
        e.preventDefault();
       //$('.valuepres').removeClass('is-active');
        $('.valuepres').css('z-index','1');		

        if($(this).hasClass('is-active')) {
                $('.valuepres').removeClass('is-active');
                $(this).removeClass('is-active');
        } else {
            $('.valuepres').removeClass('is-active');
            $(this).addClass('is-active');
            $(this).css('z-index','99');		
        }
    });

    

});


/* Flow Unit details counters */

$(function () { 
   /*
    $("#unitdetails").width();

    $("#counterTab").click(function () {
        $("#unitdetails .counters").each(function() {
                console.log($(this).width() + "| ratio:" + $(this).width()/$("#unitdetails").width());
                var ratio = $(this).width()/$("#unitdetails").width();


                if (ratio < 0.3) {
                    $(this).closest(".frame").addClass("third");
                }

                else if (ratio < 0.5) {
                    $(this).closest(".frame").addClass("half");
                }

                else {
                $(this).closest(".frame").addClass("full");
                }

   
        
                setTimeout(function(){
                    var visualheight = $(".drillcontainer").height()+35;
        
                    $("#monitorwrapper").animate({height: visualheight},100);
                    
                }, 50);
    
        });
    
    });

*/
/*
var countersSet = true;


    $("#counterTab").click(
        function() {
            setTimeout(
                function() {
                    if(countersSet){
                    $("#unitdetails .counters").each(function() {
                        var ratio = $(this).width()/$("#unitdetails").width();
                       
                        if (ratio < 0.25) {
                            $(this).closest(".frame").addClass("narrow");
                        }
                       
                        else if (ratio < 0.3) {
                            $(this).closest(".frame").addClass("third");
                        }

                        else if (ratio < 0.5) {
                            $(this).closest(".frame").addClass("half");
                        }
        
                        else {
                        $(this).closest(".frame").addClass("full");
                        }

                        setTimeout(function(){
                            var visualheight = $(".drillcontainer").height()+35;
                
                            $("#monitorwrapper").animate({height: visualheight},100);
                            countersSet = false;
                        }, 50);
                    
                });
            }
                },
                100);
        });

*/

$(flowrow).click(function() {
    $('#counters .counters div').toggleClass( "columns" );
    $("#monitorwrapper").stop().animate({height: 1500},100);
  });

});



/*
$(window).resize(function() {
	$('.valuepres').removeClass('tooltip-message--left').removeClass('tooltip-message--right');
	var windowWidth = $(window).width();
	$('.valuepres').each(function() {
		var position = $(this).offset().left;
		if(position < 120) {
			$(this).addClass('tooltip-message--left');
		}
		if((windowWidth - position) < 120) {
			$(this).addClass('tooltip-message--right');
		}
	});
}).resize();

*/


/* Report generation progress bar
$(function () {
    $(".progress-bar").each(function(){
        each_bar_width = $(this).attr('aria-valuenow');
        $(this).width(each_bar_width + '%');
        $(this).html(each_bar_width + '%');
    });
});
 */


/* Reports Wizard in modal */
$(function () {
    var navListItems = $('div.setup-panel div a'),
            allWells = $('.setup-content'),
            allNextBtn = $('.nextBtn'),
              allPrevBtn = $('.prevBtn');
  
    allWells.hide();
  
    navListItems.click(function (e) {
        e.preventDefault();
        var $target = $($(this).attr('href')),
                $item = $(this);
  
        if (!$item.hasClass('disabled')) {
            navListItems.removeClass('primary').addClass('default');
            $item.addClass('primary');
            allWells.hide();
            $target.show();
            $target.find('input:eq(0)').focus();
        }
        if($('#step-4').is(':visible')) {

            $(function () {
                $( "#progressbar" ).animate({
                    width: '100%'
                  });




                  setTimeout(function() {
                    $("#reportGenerating").hide();
                      $("#reportDone").show();
                  }, 6000);

            });


           /* $('#progressbar').animate({width: '100%'});
            $('#progressbar').html($('#progressbar').width());*/

        }

    });
    
    allPrevBtn.click(function(){
        var curStep = $(this).closest(".setup-content"),
            curStepBtn = curStep.attr("id"),
            prevStepWizard = $('div.setup-panel div a[href="#' + curStepBtn + '"]').parent().prev().children("a");
  
            prevStepWizard.removeAttr('disabled').trigger('click');
    });
  
    allNextBtn.click(function(){
        var curStep = $(this).closest(".setup-content"),
            curStepBtn = curStep.attr("id"),
            nextStepWizard = $('div.setup-panel div a[href="#' + curStepBtn + '"]').parent().next().children("a"),
            curInputs = curStep.find("input[type='text'],input[type='url']"),
            isValid = true;
  
        $(".form-group").removeClass("has-error");
        for(var i=0; i<curInputs.length; i++){
            if (!curInputs[i].validity.valid){
                isValid = false;
                $(curInputs[i]).closest(".form-group").addClass("has-error");
            }
        }
  
        if (isValid)
            nextStepWizard.removeAttr('disabled').trigger('click');
    });
  
    $('div.setup-panel div a.primary').trigger('click');
});


