var start = false;
var slideWidth = 3;
var animationTime = 40;

$(function () {
    $(document).tooltip(
        {
            tooltipClass: 'custom-tooltip'
        });
});

$(document).ready(function () {
    /* Subnavigation: Fix für überbreite Navigation */
    var outerWidth = $('#navi_sub').innerWidth();
    var width = 0;
    var mainElem = $('ul', '#navi_sub');

    $('li', mainElem).each(function (key, elem) {
        width += $(elem).innerWidth() + 1;
    });

    if (width > outerWidth) {
        mainElem.innerWidth(width);
        $('<div class="sliderLeft grid_3col"><div class="sliderLeft_inner grid_1col"/></div>').insertBefore(mainElem);
        $('<div class="sliderRight grid_3col"><div class="sliderRight_inner grid_1col"/></div>').insertBefore(mainElem);
        mainElem.css({
            'margin-left': $('.sliderLeft', '#navi_sub').innerWidth(),
            'margin-right': $('.sliderRight', '#navi_sub').innerWidth()
        });
    }

    $('body').on('mouseenter', '.sliderRight_inner', function () {
        start = true;
        fnc_startAnimation($('#navi_sub'), mainElem, 'left');
    });
    $('body').on('mouseleave', '.sliderRight', function () {
        start = false;
    });
    $('body').on('mouseenter', '.sliderLeft_inner', function () {
        start = true;
        fnc_startAnimation($('#navi_sub'), mainElem, 'right');
    });
    $('body').on('mouseleave', '.sliderLeft', function () {
        start = false;
    });

    if (typeof(editor) === 'undefined' && $('#galeryslider').length !== 0) {
        $('.keyvisual').css({cursor: 'pointer'});
        $('.keyvisual').on('click', 'img', function () {
            $('#galeryslider').show();
        });
    }

    /* Scroll - Event für Navigationselemente: Position fixed */
    var display = false;
    var sub_outer = $('#navi_sub_outer').clone();
    var last_outer = $('#navi_last_outer').clone();
    sub_outer.css({'position': 'fixed', 'top': parseInt($('#navi_main_outer').css('height')), 'z-index': 3, 'display': 'none'});
    last_outer.css({
        'position': 'fixed',
        'top': parseInt($('#navi_main_outer').css('height')) + parseInt($('#navi_sub_outer').css('height')),
        'z-index': 3,
        'display': 'none'
    });
    sub_outer.appendTo('body');
    last_outer.appendTo('body');

    $(window).on('scroll', function () {
        var offsetBody = $(window).scrollTop() + parseInt($('#navi_main_outer').css('height'));
        var offsetNavi = ( $('#navi_sub_outer').position() )
            ? $('#navi_sub_outer').position().top
            : 0;
        if (display === false && offsetBody >= offsetNavi) {
            display = true;
            sub_outer.show();
            last_outer.show();
        }
        else if (display === true && offsetBody <= offsetNavi) {
            display = false;
            sub_outer.hide();
            last_outer.hide();
        }
    });
});

function fnc_startAnimation(outerElem, innerElem, direction) {
    if (start === true) {
        outerElemWidth = outerElem.innerWidth();
        innerElemWidth = innerElem.innerWidth() + parseFloat(innerElem.css('margin-left')) + parseFloat(innerElem.css('margin-right'));
        if (direction === 'left') {
            if (outerElemWidth < innerElemWidth) {
                innerElem.css({'margin-left': parseFloat(innerElem.css('margin-left')) - slideWidth});
                setTimeout(function () {
                    fnc_startAnimation(outerElem, innerElem, direction);
                }, animationTime);
            }
            else {
                start = false;
            }
        }
        if (direction === 'right') {
            maxWidth = innerElem.innerWidth() + $('.sliderLeft', outerElem).innerWidth() + $('.sliderRight', outerElem).innerWidth();
            if (maxWidth > innerElemWidth) {
                innerElem.css({'margin-left': parseFloat(innerElem.css('margin-left')) + slideWidth});
                setTimeout(function () {
                    fnc_startAnimation(outerElem, innerElem, direction);
                }, animationTime);
            }
            else {
                start = false;
            }
        }
    }
}