var iOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;


$(document).ready(function () {

    var h_header = $( 'header' ).outerHeight();
    var h_footer = $( 'footer' ).outerHeight();



   parallax();

    $('.page_item > a:not(:has(b))').addClass('not_b');
    //$("#layout_1").remove();

    $(".button-menu-right").click(function (e) {
        e.preventDefault();
        $("body").toggleClass("toggled");
    });

    $(".title-controll").click(function (e) {
        e.preventDefault();
        $(this).toggleClass('open');
        $(this).closest('.sidebar-wrapper__admin').find("#_145_navAddControls").slideToggle(200);
    });

    $(".notification-header__dropdown .user-notification-link").click(function (e) {
        e.preventDefault();
        $(this).closest('#_2_WAR_notificationsportlet_userNotifications').find(".dockbar-user-notifications-container").toggleClass("open");
    });

    $("#_145_dockbarbodyContent .user-notification-link").click(function (e) {
        e.preventDefault();
        $(this).closest('#_2_WAR_notificationsportlet_userNotifications').find(".user-notifications-list").slideToggle(200);
    });


    var didScroll;
    var lastScrollTop = 0;
    var delta = 5;
    var navbarHeight = $('#banner').outerHeight();

    var prevScroll = 0;



    $(window).scroll(function () {
       /* check_if_in_view();*/

        var offset = $(this).scrollTop();
        if (offset > 200) {
            if (prevScroll < offset) {
                // down
                $('#banner').removeClass('nav-down').addClass('nav-up');
            } else {
                //up
                $('#banner').removeClass('nav-up').addClass('nav-down');
            }
        }
        prevScroll = offset;

        if( !iOS ) {
            $('.parallax').each(function () {
                var offsetTop = $(this).offset().top,
                    offsetBottom = offsetTop + $(this).height();
                if (isScrolledIntoView($(this))) {
                    var scaleBg = (offsetTop - $(window).scrollTop() ) / 4;
                    $(this).css('background-position-y', 'calc(50% + ' + scaleBg + 'px)');
                }
            })
        }
    });


    var clickEvent = false;
    $('#myCarousel').carousel({
        interval: 4000
    }).on('click', '.list-group li', function () {
        clickEvent = true;
        $('.list-group li').removeClass('active');
        $(this).addClass('active');
    }).on('slid.bs.carousel', function (e) {
        if (!clickEvent) {
            var count = $('.list-group').children().length - 1;
            var current = $('.list-group li.active');
            current.removeClass('active').next().addClass('active');
            var id = parseInt(current.data('slide-to'));
            if (count == id) {
                $('.list-group li').first().addClass('active');
            }
        }
        clickEvent = false;
    });

    $(".carousel-inner .item:first-child").addClass( "active" );

    resizeGridTitles($('.page_title_outer'))
});





window.onresize = function () {
    resizeList();
    resizeGridTitles($('.page_title_outer'))
}

/*$(".carousel-inner").load(function() {
    loadCarousel ();
})*/


function minHeightContainer() {

}

function parallax(){

    if( !iOS )
        $('.parallax').css('background-attachment', 'fixed');
    else
        $('.parallax').css('background-attachment', 'scroll');
}




function isScrolledIntoView(elem)
{
    var docViewTop = $(window).scrollTop();
    var docViewBottom = docViewTop + $(window).height();

    var elemTop = $(elem).offset().top;
    var elemBottom = elemTop + $(elem).height();

    return ((elemBottom <= docViewBottom + $(elem).height()) && (elemTop >= docViewTop - $(elem).height()));
}


function resizeList () {
    $(".carousel").each(function () {
        var boxheight = $(this).find('.carousel-inner').innerHeight();
        var itemlength =  $(this).find('.item').length;
        var triggerheight = Math.round(boxheight / itemlength + 1);

        $(this).find('.list-group-item').outerHeight(triggerheight);

    });
}


function loadCarousel () {
    $(".carousel").addClass("load");/*each(function () {
        console.log('loadCarousel: ');

        $(this).find('.carousel-inner').fadeTo(0.5, 1);
        $(this).find('.list-group').fadeTo(0.5, 1);
        $(this).find('.loading').hide();
    });*/
}

function resizeGridTitles(elements){
    var heights = elements.map(function ()
    {
        return $(this).height();
    }).get();

    var maxHeight = Math.max.apply(null, heights);
    elements.css('min-height', maxHeight);
}
/*


var $animation_elements = $('.parallax-container');
var $window = $(window);

function check_if_in_view() {
    var window_height = $window.height();
    var window_top_position = $window.scrollTop();
    var window_bottom_position = (window_top_position + window_height);

    $.each($animation_elements, function() {
        var $element = $(this);
        var element_height = $element.outerHeight();
        var element_top_position = $element.offset().top;
        var element_bottom_position = (element_top_position + element_height);

        //check to see if this current container is within viewport
        if ((element_bottom_position >= window_top_position) &&
            (element_top_position <= window_bottom_position)) {

            var h = element_top_position;

            var scrolltop = window.pageYOffset;
            $('.parallax').css('background-position-y', calc( 50% - scrolltop * 0.2) );
      /!*      $('.parallax').css('-webkit-transform', 'translateY(-' + scrolltop * 0.2 + 'px)');*!/

        } else {
            console.log('non presente');

        }
    });
}*/