var Remote = {

    scrollElement : 'html, body',

    init: function() {

        // Append Show/Hide-selector anchor

        $('#showhideselector').append('<a href="#" id="showhide" class="hideremote">Hide menu</a>');

        // ----------

        $('#showhide').click(function() {

            if($('#showhide').attr('class') == 'showremote')
            {
                $('#navigationwrapper').slideDown();
                $('#showhide').text('Hide menu');
                $('#showhide').attr('class', 'hideremote');

                $('.remoteenabledheader').each(function() {
                    
                    $(this).animate({ paddingTop: '150px' }, 1000);

                });
            }
            else
            {
                $('#navigationwrapper').slideUp();
                $('#showhide').text('Show menu');
                $('#showhide').attr('class', 'showremote');

                $('.remoteenabledheader').each(function() {

                    if($(Remote.scrollElement).attr('scrollTop') <= $(this).offset().top + 20 && $(Remote.scrollElement).attr('scrollTop') >= $(this).offset().top - 10 )
                    {
                        $(this).animate({ paddingTop: $('#extendmenucontainer').height() + 20 +'px' }, 1000);
                    }

                });
            }

            return false;

        });

        // Trigger remote

        $('#remotemenucontainer ul li a').each(function() {

            $(this).click(function() {

            if(getTarget($(this).attr('href')))
            {
                return false;
            }

            return true;

            });

        });

        function getTarget(_hash) {

            var target =  $(_hash);

            $(Remote.scrollElement).each(function () {

                var initScrollTop = $(this).attr('scrollTop');

                $(this).attr('scrollTop', initScrollTop + 1);

                if($(this).attr('scrollTop') == initScrollTop + 1)
                {
                    Remote.scrollElement = this.nodeName.toLowerCase();

                    $(this).attr('scrollTop', initScrollTop);

                    return false;
                }

                return true;

            });

            if(target.length)
            {
                var targetOffset = target.offset().top;

                $(Remote.scrollElement).animate(
                    {scrollTop: targetOffset},
                    800,
                    function() {
                        location.hash = _hash;
                    });

                return true;
            }

            return false;

        }

    },

    reinit: function() {

        // (Re)Trigger remote

        $('#remotemenucontainer ul li a.re').each(function() {

            $(this).click(function() {

            if(getTarget($(this).attr('href')))
            {
                return false;
            }

            return true;

            });

        });

        function getTarget(_hash) {

            var target =  $(_hash);

            $(Remote.scrollElement).each(function () {

                var initScrollTop = $(this).attr('scrollTop');

                $(this).attr('scrollTop', initScrollTop + 1);

                if($(this).attr('scrollTop') == initScrollTop + 1)
                {
                    Remote.scrollElement = this.nodeName.toLowerCase();

                    $(this).attr('scrollTop', initScrollTop);

                    return false;
                }

                return true;

            });

            if(target.length)
            {
                var targetOffset = target.offset().top;

                $(Remote.scrollElement).animate(
                    {scrollTop: targetOffset},
                    800,
                    function() {
                        location.hash = _hash;
                    });

                return true;
            }

            return false;

        }

    }

    // ...

}
