//$(document).ready(function(){  });

/****************************/
/*Watch validator and hide if has no content*/
/****************************/
function WatchValidatorsAndSorrounding(e, me){
    var arroundLabels = $(me).closest("form").find('label.error');
    if(arroundLabels.length < 1){
        return false;
    }
    for (var index = 0; index < arroundLabels.length; index++) {
        const element = arroundLabels[index];
        var txt = $(element).text();
        var displayController = $(element).attr("display-controller");

        if(txt.length > 0){
            $(element).closest(displayController).removeClass("display-none");
        }else{
            $(element).closest(displayController).addClass("display-none");
        }
    }
}
function WatchValidators(e, me){
    var txt = $(me).text();
    var displayController = $(me).attr("display-controller");

    if(txt.length > 0){
        $(me).closest(displayController).removeClass("display-none");
    }else{
        $(me).closest(displayController).addClass("display-none");
    }
}
$(document).ready(function(){  
    $('label.error').bind('DOMSubtreeModified', function(event){
        WatchValidators(event, this);
    });
});

/****************************/
/*Calc Footer and Header height*/
/*to determine minimum Main Minimum Height*/
/****************************/
function CalcMainMinimumHeight(){
    var header = $(".header-container-placeholder");
    var footer = $(".footer-container-placeholder");
    var main = $(".main[role=main]");
    if(main.length < 1){
        return false;
    }
    var hheight = header.length > 0 ? header.height() : 0;
    var hfooter = footer.length > 0 ? footer.height() : 0;

    hheight = parseInt(hheight);
    hfooter = parseInt(hfooter);

    hheight = isNaN(hheight) ? 0 : hheight;
    hfooter = isNaN(hfooter) ? 0 : hfooter;
    var h = hheight + hfooter;

    var minHeight = "calc(100vh - "+h+"px)";
    main.css("min-height", minHeight);
}
$(document).ready(CalcMainMinimumHeight);
$(document).ready(function(){ $(window).resize(CalcMainMinimumHeight); });

/****************************/
/*Swich Classes*/
/****************************/
function DoSwichTheClass(theEl, theClass){
    
    var DoSwich = function(t){
        if(t.hasClass(theClass)){
            t.removeClass(theClass);
        }else{
            t.addClass(theClass);
        }
    }

    if(theEl.length == 1){
        DoSwich(theEl);
    }else if(theEl.length > 1){
        theEl.each(function(i,subEl){
            DoSwich($(subEl));
        });
    }
}

function SwichDisplay(e, me){
    var swichTarget = $(me.getAttribute("swich-target"));
    if(swichTarget.length > 0){
        DoSwichTheClass(swichTarget, "display-none")
    }
}

function SwichToDark(e, me){
    var swichTarget = $(me.getAttribute("dark-target"));
    if(swichTarget.length > 0){
        DoSwichTheClass(swichTarget, "dark")
    }
}

function SwichOverflowHidden(e, me){
    var swichTarget = $(me.getAttribute("overflow-hidden-target"));
    if(swichTarget.length > 0){
        DoSwichTheClass(swichTarget, "overflow-hidden")
    }
}

$.scrollbarWidth=function(){var a,b,c;if(c===undefined){a=$('<div style="width:50px;height:50px;overflow:auto"><div/></div>').appendTo('body');b=a.children();c=b.innerWidth()-b.height(99).innerWidth();a.remove()}return c};
$.scrollbarWidthSetPlaceholder=function(){ $(".scroll-bar-placeholder").width($.scrollbarWidth()); };
$(document).ready(function(){ $(".swich-display").on("click", function(event){ SwichDisplay(event, this); }); });
$(document).ready(function(){ $(".swich-to-dark").on("click", function(event){ SwichToDark(event, this); }); });
$(document).ready(function(){ $(".swich-overflow-hidden").on("click", function(event){ SwichOverflowHidden(event, this); }); });
//disable all autocomplete from forms
$(document).ready(function(){ $("form").attr("autocomplete","off"); });
$(document).ready(function(){ $.scrollbarWidthSetPlaceholder(); setTimeout($.scrollbarWidthSetPlaceholder, 5000); });