$(document).ready(function() {


    //this is the menu arrow function

    $('.menu_wrapper li').each(function(index, Element){

        $(Element).mouseover (function() {
            $(this).find('.menu_arrow').addClass('menu_arrow_2')
        });

        $(Element).mouseout (function(event) {
            if (index == 0) {
                var top = $(this).offset().top;
                var height = $(this).height();
                var width = $(this).width();
                var left = $(this).offset().left;
                var y = event.pageY;
                var x = event.pageX;
                var hide = true;
                if (x > left && x < left + width && y > top) hide = false;
                if (hide) $(this).find('.menu_arrow').removeClass('menu_arrow_2');
            } else {
                $(this).find('.menu_arrow').removeClass('menu_arrow_2');
            }

        });
    });


        //menu_home

    $('.submenu_wrapper').css('display', 'none');


    $('#menu_home').each(function(index, Element){
        $(Element).mouseover(function(){
            $('#home').show();
            $('.submenu_wrapper').css('display', 'block');
        });

        $(Element).mouseout(function(){
            $('#home').hide();
            $('.submenu_wrapper').css('display', 'none');
        });

    });

    $('.submenu_wrapper').each(function(index, Element){
        $(Element).mouseover(function(){
            $('#home').show();
            $('.menu_arrow', this).addClass('menu_arrow_2');
            $('.submenu_wrapper').css('display', 'block');
        });

        $(Element).mouseout(function(event){
            var top = $(this).offset().top;
            var height = $(this).height();
            var width = $(this).width();
            var left = $(this).offset().left;
            var y = event.pageY;
            var x = event.pageX;
            var hide = true;
            if (x > left && x < left + width && y > top && y < top + height) hide = false;
            if (hide) {
                $('#home').hide();
                $('.menu_arrow', this).removeClass('menu_arrow_2');
                $('.submenu_wrapper').css('display', 'none');
                $('.menu_wrapper li').find('.menu_arrow').removeClass('menu_arrow_2');
            }
        });
    });


    //mouseover standortkarte

    $('.pos_flag').css('display', 'none')

    $('.pos_dot').each(function(index, Element){

        $(Element).mouseover (function() {
            $('.pos_flag').show()
        });

        $(Element).mouseout (function() {
            $('.pos_flag').hide()
        });
    });

    $('.pos_flag').each(function(index, Element){

        $(Element).mouseover (function() {
            $('.pos_flag').show()
        });

        $(Element).mouseout (function() {
            $('.pos_flag').hide()
        });
    });


    //mouseover suche

    $('.search_input').css('display', 'none')

    $('.global_search').each(function(index, Element){

        $(Element).mouseover (function() {
            $('.search_input').show();
        });

        $(Element).parent().mouseout(function() {
            $('.search_input').hide();
        });


        $(Element).mouseout (function(event) {
            var parentPosition = $(this).offset();
            var parentWidth = $(this).width();
            var parentHeight = $(this).height();
            var left = event.pageX;
            var top = event.pageY;
            //console.debug("Px: " + parentPosition.left + ', Py: ' + parentPosition.top + 'Pw: ' + parentWidth + ', Ph: ' + parentHeight);
            //console.debug('Position x: ' + left + ', y: ' + top);
            var trigger = false;
            if (left < parentPosition.left || left > parentPosition.left + parentWidth) {
                //console.debug("OUT OF BOUNDS HORIZONTAL");
                trigger = true;
            }
            if (top < parentPosition.top || top > parentPosition.top + parentHeight) {
                //console.debug("OUT OF BOUNDS VERTICAL");
                trigger = true;
            }
            if (trigger) $('.search_input').hide();
        });

    });



    $('.btn_search').mouseover(function() {
        $(this).css('cursor', 'pointer');
    }).mouseout(function() {
        $(this).css('cursor', 'pointer');
    }).click(function(event) {
        event.preventDefault();
        var value = $('#search_text').val();
        var url = $(this).attr('href');
        if ('' !== value) {
            url += '/' + value;
            top.location = url;
        }
        
    });
});

