$(document).ready(function() {
    // F. IE region selector round corners
    $('.show-more-title .hr').each(function () {
        this.style.height = this.parentNode.offsetHeight - 22 + 'px';
    });
    $('.show-more-title .vr').each(function () {
        this.style.width = this.parentNode.offsetWidth - 22 + 'px';
    });
     $("#menu .show-more").width($("#menu .show-more").width());

    // Init in-place rounded popups like "More ▼"
    $('.show-more .show-more-title').click(function (e) {
        e.preventDefault();

        var container = $(this).parents('.show-more');
        var content = container.children('.show-more-content');
        if (container.hasClass('show-more-open')) {
            container.removeClass('show-more-open');
        }
        else {
            container.addClass('show-more-open');
            // F. IE regions list round corners
            content.children('.hr').each(function () {
                this.style.height = this.parentNode.offsetHeight - 22 + 'px';
                this.style.width = this.parentNode.offsetWidth - 2 + 'px';
            });
            content.children('.vr').each(function () {
                this.style.height = this.parentNode.offsetHeight - 2 + 'px';
                this.style.width = this.parentNode.offsetWidth - 22 + 'px';
            });
        }
    });

    // Init close buttons for in-place rounded popups like "More ▼"
    $('.show-more .btn-close').click(function (e) {
        e.preventDefault();
        e.stopPropagation();
        var $this = $(this);
        var element = $this.parents('.show-more');
        var content = element.children('.show-more-content');
        element.removeClass('show-more-open');
    });

    // Init menu lines with slider
    $('.slider > li a').click(function(e) {
        e.preventDefault();

        // Get affected elements
        var $this = $(this);
        var switcher = $(this).parents('ul');
        var target = $this.parents('li');
        var selected = switcher.children('.selected');

        if (!selected.length) {
            // moved from some other position, just select and go
            target.addClass('selected').addClass('rc13').addClass('rc-shape');
            window.location = $this.attr('href');
            return;
        }

        // Calculate initial and final position
        var from_x = selected.get(0).offsetLeft;
        var from_y = selected.get(0).offsetTop;
        var from_w = selected.innerWidth();
        var to_x = target.get(0).offsetLeft;
        var to_y = target.get(0).offsetTop;
        var to_w = target.innerWidth();

        // Init slider
        var slider = switcher.children('li.slider');
        if (!slider.length) {
            switcher.append('<li class="slider rc13 rc-shape"></li>');
            slider = switcher.children('li.slider');
            if (typeof rocon != 'undefined') {
                rocon.update(slider.get(0));
            }
        }
        slider.css({
            'top' : from_y,
            'left' : from_x,
            'width' : from_w,
            'height' : selected.innerHeight()
        });
        slider.show();

        selected.removeClass('selected').addClass('slide-from');
        target.addClass('slide-to');

        var time = function (from, to) {
            var step = switcher.children().get(0).offsetLeft
                - switcher.children().get(1).offsetLeft;
            var raw = Math.abs(150 * (from - to) / step);
            return raw < 80 ? 80 : raw;
        }

        if (from_y == to_y) {
            slider.
                animate({left: to_x}, time(from_x, to_x)).
                animate(
                    {width: to_w},
                    {
                        duration : time(from_w, to_w),
                        complete : function() {
                            window.location = $this.attr('href');
                        }
                    }
                );
        }
        else {
            var from_end = switcher.children().get(0).offsetLeft;
            var to_end = switcher.innerWidth() - slider.innerWidth();
            slider.
                animate(
                    {left: to_end},
                    {
                        duration : time(from_x, to_end),
                        complete : function() {
                            slider.css({top: to_y, left: from_end});
                        }
                    }
                ).
                animate({left: to_x}, time(from_end, to_x)).
                animate(
                    {width: to_w},
                    {
                        duration : time(from_w, to_w),
                        complete : function() {
                            window.location = $this.attr('href');
                        }
                    }
                );
        }
    });
});
