(function($) {
    $(document).ready(function() {
        var box = $('.top_geo');
        box.find('a,.down').on('click', function() {
            getMiniBox();
        });

        function getMiniBox() {
            if ($('.minibox, .bigbox').length) $('.minibox').remove();
            else {
                var minibox = $('<div class="minibox">').appendTo(box);
                $('<div class="mb_1">ВАШ РЕГИОН?</div>').appendTo(minibox);
                var mb2 = $('<div class="mb_2">').appendTo(minibox);
                $('<div class="mb_3 fa fa-search"><a href="javascript:void(0)">выбрать другой город</a></div>').appendTo(minibox).on('click', function() {
                    getBigBox();
                });
                $('<div class="mb_21">ДА</div>').appendTo(mb2).on('click', function() {
                    minibox.remove();
                });
                $('<div class="mb_22">НЕТ</div>').appendTo(mb2).on('click', function() {
                    getBigBox();
                });
            }
        }

        function getBigBox() {
            $('.minibox').remove();
            var over = $('<div class="cityoverlay">').appendTo('body');
            var bigbox = $('<div class="bigbox">').appendTo('body').css('opacity', 0);
            $('<div class="bclose">×</div>').appendTo(bigbox).add(over).on('click', function() {
                boxclose(bigbox,over);
            });
            $.ajax({
                url: '/index.php',
                data: {
                    option: "com_ajax",
                    module: "ru_cities",
                    format: 'raw',
                    mod: "bigbox"
                },
                success: function(html) {
                    if(html.indexOf('-w')>0) {
                        bigbox.append(html);
                        verticallycentr(bigbox);
                        activate(bigbox);
                        boxscroll(bigbox);
                    }
                }
            });

        }

        function verticallycentr(box) {
            var winh = $(window).height();
            if (box.height() < winh) {
                var h = (winh - box.height()) / 2;
                box.animate({
                    'opacity': 1,
                    'top': h
                }, 600);
            } else {
                box.animate({
                    'opacity': 1,
                    'top': 100
                }, 600);
            }
        }

        function activate(box) {
            var chooserbox = $('.bigboxchooserbox');
            $('.bigboxinput input').on('input', function() {
                if(this.value){
                    $.ajax({
                        url: '/index.php',
                        data: {
                            option: "com_ajax",
                            module: "ru_cities",
                            format: 'raw',
                            mod: "bigboxcities",
                            text: this.value
                        },
                        success: function(html) {
                            if(html) {
                                chooserbox.html(html);
                                box.css('background', 'rgba(255, 255, 255, 0.52)');
                                $('.bigboxvars').css('visibility','hidden');
                                $('.bigboxchooserbox a').on('click', function () {
                                    changeCity(this);
                                });
                            }
                            else {
                                back();
                            }
                        }
                    });
                }else{
                    back();
                }
            });
            box.on('click', function () {
                back();
            });
            $('.bigboxvars a').on('click', function () {
                changeCity(this);
            });
            function back() {
                chooserbox.html('');
                box.css('background', '#fff');
                $('.bigboxvars').css('visibility','visible');
            }
        }


        function changeCity(lnk) {
            $('.bigbox').animate({'opacity':0},600);
            var dat = $(lnk).data('city');
            $.ajax({
                url: '/index.php',
                data: {
                    option: "com_ajax",
                    module: "ru_cities",
                    format: 'raw',
                    mod: "bigboxcity",
                    city: dat
                },
                success: function (res) {
                    if(res=='y') {
                        $('.cityoverlay, .bigbox').remove();
                        $('.top_geo a').html(dat.slice(3));
                    }
                }
            });
        }




        var getBodyScrollTop = function(){return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop)};
        function boxscroll(box) {
            window.addEventListener('scroll', function() {
                var bh = box.height(),
                    wh = $(window).height(),
                    gbs = getBodyScrollTop(),
                    bot = box.offset().top;
                if (bh < wh) {
                    if ((bot - gbs) != (wh - bh) / 2) {
                        box.stop().animate({
                            'top': (wh - bh) / 2 + gbs
                        }, 300);
                    }
                } else {
                    if ((bot - gbs) > 0)
                        box.stop().animate({
                            'top': 50 + gbs
                        }, 300);
                    if ((bot - gbs + bh - wh) < 0)
                        box.stop().animate({
                            'top': gbs - bh + wh - 50
                        }, 300);
                }
            }, false);
        }
        function boxclose(box,over) {
            over.add(box).stop().animate({
                'opacity': 0
            }, 600, function() {
                $(this).remove();
            });
        }

    });
})(jQuery);
