jQuery(document).ready(function () {

    if ($.browser.msie && parseInt($.browser.version, 10) > 9) 
        $("html").addClass("ie10");
    if ($.browser.msie && parseInt($.browser.version, 10) <= 9)
        $("html").addClass("ie9");
    
    var url = window.location.toString();
    var host = window.location.host.toString();
   
    if (url.indexOf("browse-by-subjects") != -1) $('.main-menu a[href*="browse-by-subjects"]').parent().addClass("active");
    else if (url.indexOf("browse-by-publishers") != -1) $('.main-menu a[href*="browse-by-publishers"]').parent().addClass("active");
    else if (url.indexOf("browse-by-journals") != -1) $('.main-menu a[href*="browse-by-journals"]').parent().addClass("active");
    else if (url.indexOf("personal-user-account") != -1) $('.main-menu a[href*="personal-user-account"]').parent().addClass("active");
    else if (url.indexOf("help") != -1) $('.main-menu a[href*="help"]').parent().addClass("active");
    else if (url.indexOf("media") != -1) $('.main-menu a[href*="media"]').parent().addClass("active");
    else if (url.indexOf("contact-CEEOL") != -1) $('.main-menu a[href*="contact-CEEOL"]').parent().addClass("active");
    else if (url.indexOf("contact") != -1) $('.main-menu a[href*="contact"]').parent().addClass("active");
    
    else if (url.indexOf("CEEOL-for-libraries") != -1) $('.main-menu a[href*="CEEOL-for-libraries"]').parent().not('.menu-for-librarians').addClass("active");
    else if (url.indexOf("partners") != -1) $('.main-menu a[href*="partners"]').parent().addClass("active");
    else if (url.indexOf("downloads") != -1) $('.main-menu a[href*="downloads"]').parent().addClass("active");
    else if (url.indexOf("my-library-account") != -1) $('.main-menu a[href*="my-library-account"]').parent().addClass("active");
    else if (url.indexOf("order") != -1) $('.main-menu a[href*="order"]').parent().addClass("active");
    else if (url.indexOf("faq") != -1) $('.main-menu a[href*="faq"]').parent().addClass("active");
    
    else if (url.indexOf("CEEOL-for-publishers") != -1) $('.main-menu a[href*="CEEOL-for-publishers"]').parent().not('.menu-for-publishers').addClass("active");
    else if (url.indexOf("downloads") != -1) $('.main-menu a[href*="downloads"]').parent().addClass("active");
    else if (url.indexOf("my-publishers-account") != -1) $('.main-menu a[href*="my-publishers-account"]').parent().addClass("active");
    else if (url.indexOf("upload") != -1) $('.main-menu a[href*="upload"]').parent().addClass("active");

    else if (url.indexOf("journal-edit") != -1) {$('.page-sidebar-menu .sub-menu a[href*="journal-data"]').parent().addClass("active"); $('.page-sidebar-menu .sub-menu a[href*="journal-data"]').parent().parent().parent().addClass("active open"); }
    else if ((url.indexOf("ebook-edit") != -1) || (url.indexOf("ebook-view") != -1) || (url.indexOf("chapter-edit") != -1) || (url.indexOf("chapter-view") != -1)) {
        $('.page-sidebar-menu .sub-menu a[href*="content-management/ebooks"]').parent().addClass("active");
        $('.page-sidebar-menu .sub-menu a[href*="content-management/ebooks"]').parent().parent().parent().addClass("active open");
    }
    else if ((url.indexOf("issues") != -1) || (url.indexOf("issue-view") != -1) || (url.indexOf("issue-edit") != -1) || (url.indexOf("issue-articles") != -1) || (url.indexOf("article-edit") != -1) || (url.indexOf("article-view") != -1)) {
        $('.page-sidebar-menu .sub-menu a[href*="content-management/journal-issues"]').parent().addClass("active");
        $('.page-sidebar-menu .sub-menu a[href*="content-management/journal-issues"]').parent().parent().parent().addClass("active open");
    }
    else if ((url.indexOf("my-content-provider-data/author-edit") != -1)) {
        $('.page-sidebar-menu .sub-menu a[href*="my-content-provider-data/authors"]').parent().addClass("active");
        $('.page-sidebar-menu .sub-menu a[href*="my-content-provider-data/authors"]').parent().parent().parent().addClass("active open");
    }
    else if ((url.indexOf("gray-literature-edit") != -1) || (url.indexOf("gray-literature-view") != -1) ) {
        $('.page-sidebar-menu .sub-menu a[href*="content-management/gray-literature"]').parent().addClass("active");
        $('.page-sidebar-menu .sub-menu a[href*="content-management/gray-literature"]').parent().parent().parent().addClass("active open");
    }
    else if (url.indexOf("order-details") != -1) { $('.page-sidebar-menu .sub-menu a[href*="order/order-history"]').parent().addClass("active"); $('.page-sidebar-menu .sub-menu a[href*="order/order-history"]').parent().parent().parent().addClass("active open"); }
    else if (url.indexOf("ebook-view") != -1) { $('.page-sidebar-menu .sub-menu a[href*="content-management/ebooks"]').parent().addClass("active"); $('.page-sidebar-menu .sub-menu a[href*="content-management/ebooks"]').parent().parent().parent().addClass("active open"); }
    else if (url.indexOf("package-details") != -1) { $('.page-sidebar-menu .sub-menu a[href*="content-management/packages"]').parent().addClass("active"); $('.page-sidebar-menu .sub-menu a[href*="content-management/packages"]').parent().parent().parent().addClass("active open"); }


    else if (url.indexOf("my-publishers-account/ebook-edit") != -1) $('.secondary-menu a[href*="edit-ebooks"]').parent().addClass("active");
    //else if (host != -1) $('.main-menu li:first').addClass("active");

    var matchUrl = $(location).attr("pathname").toString();
    if (matchUrl != "/") {
        if (matchUrl != -1 ) {
            $(".page-sidebar-menu .sub-menu a[href='" + matchUrl + "']").parent().addClass("active");
            $(".page-sidebar-menu .sub-menu a[href='" + matchUrl + "']").parent().parent().parent().addClass("active open");
        } 
    }

   
    $('.main-search-input, #vkinput').mouseover(function () {
        this.holderMain = $(this).attr('placeholder');
        $(this).attr('placeholder', 'Using diacritics in the correct way improves your chances of finding what you are looking for.');
    });
    $('.main-search-input, #vkinput').mouseout(function () {
        $(this).attr('placeholder', this.holderMain);
    });
 
    
    $(".scrollup").hide();
    $(function () {
        $(window).scroll(function () {
            if ($(this).scrollTop() > 100) {
                $('.scrollup').fadeIn();
            } else {
                $('.scrollup').fadeOut();
            }
        });
        $('.scrollup').click(function () {
            $('body,html').animate({ scrollTop: 0 }, 800);
            return false;
        });
    });
    
    $('.flatrate button').click(function () {
        $('.flatrate-info').slideToggle(400);
    });

    $("#loginUserName").attr("autofocus", "autofocus");

    //if ($(window).width() <= 900) {
    //    $('.showhide').detach().insertAfter('.menu-for-publishers');
    //}
       
    $('.login-link-shibbolet').click(function () {
        $('.login-user').hide(300);
        $('.login-link-shibbolet').hide();
        $('.login-shibbolet').show(300);
        $('.login-link-user').show();
    });
    $('.login-link-user').click(function () {
        $('.login-shibbolet').hide(300);
        $('.login-link-user').hide();
        $('.login-user').show(300);
        $('.login-link-shibbolet').show();
    });
    $('.forgotten-pass').click(function () {
        $('.user-mail').toggle(300);
    });

    $('.toggle-actions').click(function () {
        $('.table-with-actions').toggleClass("collapsed");
    });  
  
   
   
    $(".search-result-filter h4 .fa-chevron-circle-down").parent().next().hide();
    $(".search-result-filter h4 i").click(function () {
        if ($(this).hasClass("fa-chevron-circle-up")) {
            $(this).parent().next().slideUp();
            $(this).removeClass("fa-chevron-circle-up");
            $(this).addClass("fa-chevron-circle-down");
        } else {
            $(this).parent().next().slideDown();
            $(this).removeClass("fa-chevron-circle-down");
            $(this).addClass("fa-chevron-circle-up");
        }
    });



    if ($(window).width() >= 768) {
        $('.carousel-inner .item').height($('.home-subejcts').height());
        $(window).resize(function () {
            $('.carousel-inner .item').height($('.home-subejcts').height());
        });
    }
    var filterToggler =  $(".search-result-filter h4 .fa");
    function searchResultFilterHide() {
        filterToggler.removeClass("fa-chevron-circle-up").addClass("fa-chevron-circle-down");
        filterToggler.parent().next().hide();
    }
    function searchResultFilterShow() {
        filterToggler.removeClass("fa-chevron-circle-down").addClass("fa-chevron-circle-up");
        filterToggler.parent().next().show();
    }
    function leftMenuCollapsed () {
        $('.secondary-menu').closest('.container').addClass("collapsed");
    }   
    function leftMenuExpanded() {
        $('.secondary-menu').closest('.container').removeClass("collapsed");
    }
    
    $(window).resize(function () {
        if ($(window).width() <= 992) {
            leftMenuCollapsed();
            searchResultFilterHide();
        }
        if ($(window).width() > 992) {
            leftMenuExpanded();
            searchResultFilterShow();
        }
        if ($(window).width() >= 992 && $(window).width() <= 1499) {
            $('.table-with-actions').addClass("collapsed");
        } else {
            $('.table-with-actions').removeClass("collapsed");
        }
    });
    if ($(window).width() <= 992) {
        leftMenuCollapsed();
        searchResultFilterHide();
    }
    if ($(window).width() > 992) {
        leftMenuExpanded();
        searchResultFilterShow();
    }
    if ($(window).width() >= 992 && $(window).width() <= 1499) {
        $('.table-with-actions').addClass("collapsed");
    } else {
        $('.table-with-actions').removeClass("collapsed");
    }


    $("#jetmenu").jetmenu();
    
    $('.secondary-menu .secondary-menu-toggle').click(function () {
        $(this).closest('.main-content').toggleClass("collapsed");
    });

    $(".btn-help").click(function () {
        $(".help-container").toggle();
    });
    
    var lastPopedPopover;
    $('.popovers').popover();
    $(document).on('click.bs.popover.data-api', function (e) {
        if (lastPopedPopover) {
            lastPopedPopover.popover('hide');
        }
    });
    
    $(".close[data-hide], .help-container .close").on("click", function () {
        $(this).parent().hide();
    });

    var winWidth = $(window).width();
    $(window).resize(function () {
        if (winWidth <= 767) {
            $('#slider').css({ 'width': '460px', 'height': '226px' });
            $('.slide-1 h2').css({ 'top': '40px', 'left': '40px' });
            $('.slide-1 p').css({ 'top': '80px', 'left': '40px' });
        }
    });
    if (winWidth <= 767) {
        $('#slider').css({ 'width': '460px', 'height': '226px' });
        $('.slide-1 h2').css({ 'top': '40px', 'left': '40px' });
        $('.slide-1 p').css({ 'top': '80px', 'left': '40px' });
    }
    $('.librarian .page-sidebar-menu > li:eq(1), .librarian .page-sidebar-menu > li:eq(2), .librarian .page-sidebar-menu > li:eq(3)').addClass("open");
    $('.librarian .page-sidebar-menu > li:eq(1) .arrow, .librarian .page-sidebar-menu > li:eq(2) .arrow, .librarian .page-sidebar-menu > li:eq(3) .arrow').addClass("open");

    $('.page-sidebar-menu').on('click', 'li > a .arrow', function (e) {
        if ($(this).parent().next().is(":visible")) {
            $(this).parent().next().slideUp(100);
            $(this).removeClass("open");
            $(this).parent().parent().removeClass("open");
        } else {
            $(this).parent().next().slideDown(100);
            $(this).addClass("open");
            $(this).parent().parent().addClass("open");;
        }
    });


});