function Main() {
    var self = this;
    this.xmasData = [];
    this.mapData = [];

    this.cookieAccept = function () {
        $(".js-cookie-accept").click(function (e) {
            e.preventDefault();
            $(".js-cooke-layer").remove();
            $.ajax({
                'method': 'post',
                'data': 'ok=1',
                'url': '/cookie/accept'
            }).done(function (resp) {

            });
        });
    }
    this.getMoreNews = function () {
        $(".js-get-more-news").click(function (e) {
            e.preventDefault();
            let url = $(".js-get-more-news").data('url');
            let page = $(".js-get-more-news").data('page');
            let category_id = $(".js-get-more-news").data('category_id');
            $.ajax({
                'method': 'post',
                'data': 'ok=1&page=' + page + '&category_id=' + category_id,
                'dataType': 'json',
                'url': '/newsajax',
            }).done(function (resp) {
                if (resp.html) {
                    $(".js-ajax-news").append(resp.html);
                }
                if (resp.page) {
                    $(".js-get-more-news").data('page', resp.page);
                } else {
                    $(".js-get-more-news").remove();
                }
            });
        });
    }

    this.newsletter = function () {
        $(".js-accept-newsletter").click(function (e) {
            e.preventDefault();
            $(".js-error-msg").css('display', 'none');
            let email = $.trim($("#newsletter_email").val());
            if (self.isValidEmail(email)) {
                if ($(".js-newsletter-checkbox").prop("checked") == true) {
                    acceptTerms = 1;
                    let url = '/newsletterAdd';
                    $.ajax({
                        url: url,
                        type: "post", //send it through get method
                        data: {
                            email: email,
                        },
                        success: function (response) {

                        },
                        error: function (xhr) {
                            //Do Something to handle error
                        }
                    });

                    $(".js-newsletter-thx").css('display', 'block');
                    $(".js-accept-newsletter").remove();
                } else {
                    $("#js-newsletter-checkbox").focus();
                    $(".js-error-2").css('display', 'block')
                }
            } else {
                $("#newsletter_email").focus();
                $(".js-error-1").css('display', 'block')
            }
        });
    }

    this.isValidEmail = function (email) {
        const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
        return re.test(String(email).toLowerCase());
    }

    this.contactForm = function () {
        $(".js-contact-form-send-btn").click(function (e) {
            e.preventDefault();
            $(".js-form-message").css('display', 'none');
            let to = $.trim($("#to").val());
            let first_name = $.trim($("#first_name").val());
            let last_name = $.trim($("#last_name").val());
            let email = $.trim($("#contact-form-email").val());
            let message = $.trim($("#message").val());

            if (to && first_name && last_name && email && message && $(".js-contact-form-checkbox").prop("checked") == true && self.isValidEmail(email)) {
                $("#first_name").val("");
                $("#last_name").val("");
                $("#contact-form-email").val("");
                $("#message").val("");
                $.ajax({
                    'method': 'post',
                    'data': 'to=' + to + '&first_name=' + first_name + '&last_name=' + last_name + '&email=' + email + '&message=' + message,
                    'dataType': 'json',
                    'url': '/send/message',
                }).done(function (resp) {
                    if (resp.ok) {
                        $(".js-form-success").css('display', 'block');
                        $(".js-contact-form-send-btn").css('display', 'none');
                    }
                });

            } else {
                $(".js-form-error").css('display', 'block');
            }

        });

    }

    this.renderXmasSelect = function () {
        let $el = $("#xmastimeselector");
        $el.empty(); // remove old options
        $.each(self.xmasData, function (key, value) {
            $el.append($("<option></option>")
                .attr("value", value.interval).text(value.interval));
        });
    }

    this.xmas = function () {
        $("#xmasdayselector").change(function () {
            $("#js-error-message-1").css('display', 'none');
            $("#js-error-message-2").css('display', 'none');
            $("#js-error-message-3").css('display', 'none');
            $("#js-error-message-4").css('display', 'none');
            $("#js-error-message-5").css('display', 'none');
            $("#js-error-message-6").css('display', 'none');
            let date = this.value;
            let $el = $("#xmastimeselector");
            $el.empty(); // remove old options
            $.ajax({
                'method': 'post',
                'data': 'date=' + date,
                'dataType': 'json',
                'url': '/ajax/xmas-interval',
            }).done(function (resp) {
                if (resp.interval) {
                    self.xmasData = resp.interval;
                    self.renderXmasSelect();
                }
                if (resp.errorMessage) {
                    $("#js-error-message-5").css('display', 'list-item');
                    showPopup('errors');
                }
            });
        });
        $(".js-apply-for-xmax").click(function (e) {
            e.preventDefault();
            let xemail = $.trim($("#xemail").val());
            let xname = $.trim($("#xname").val());
            let day = $.trim($("#xmasdayselector").val());
            let time = $.trim($("#xmastimeselector").val());
            let edm = 0;
            if ($(".js-xmas-checkbox-2").prop("checked")) {
                edm = 1;
            }
            if ($(".js-xmas-checkbox-1").prop("checked") && self.isValidEmail(xemail) && xname != '') {
                $("#js-error-message-1").css('display', 'none');
                $("#js-error-message-2").css('display', 'none');
                $("#js-error-message-3").css('display', 'none');
                $("#js-error-message-4").css('display', 'none');
                $("#js-error-message-5").css('display', 'none');
                $("#js-error-message-6").css('display', 'none');
                $.ajax({
                    'method': 'post',
                    'data': 'name=' + xname + '&email=' + xemail + '&edm=' + edm + '&date=' + day + '&time=' + time,
                    'dataType': 'json',
                    'url': '/ajax/xmas-apply',
                }).done(function (resp) {
                    if (resp.interval) {
                        self.xmasData = resp.interval;
                        self.renderXmasSelect();
                    }
                    if (resp.ok) {
                        $("#xemail").val("");
                        $("#xname").val("");
                        $(".js-xmas-checkbox-1").prop("checked", false);
                        $(".js-xmas-checkbox-2").prop("checked", false);
                        showPopup('thx');
                    }
                    if (resp.error) {
                        showPopup('errors');
                    }

                    if (resp.busy) {
                        $("#js-error-message-6").css('display', 'list-item');
                        showPopup('errors');
                    }
                });
            } else {
                $("#js-error-message-1").css('display', 'none');
                $("#js-error-message-2").css('display', 'none');
                $("#js-error-message-3").css('display', 'none');
                $("#js-error-message-4").css('display', 'none');
                $("#js-error-message-5").css('display', 'none');
                $("#js-error-message-6").css('display', 'none');
                if (self.isValidEmail(xemail)) {
                    $("#js-error-message-2").css('display', 'none');
                } else {
                    $("#js-error-message-2").css('display', 'list-item');
                }
                if (xname) {
                    $("#js-error-message-1").css('display', 'none');
                } else {
                    $("#js-error-message-1").css('display', 'list-item');
                }
                if (day.length > 8 && time.length > 4) {
                    $("#js-error-message-3").css('display', 'none');
                } else {
                    $("#js-error-message-3").css('display', 'list-item');
                }
                if ($(".js-xmas-checkbox-1").prop("checked")) {
                    $("#js-error-message-3").css('display', 'none');
                } else {
                    $("#js-error-message-4").css('display', 'list-item');
                }
                showPopup('errors');
            }
        });
    };
    this.map = function () {
        if ($('.tc-map').length) {
            $('body').on('mousemove', '.floor-container g', function (e) {

                var parentOffset = $(this).parents('.tc-slider-item').offset(),
                    relX = e.pageX - parentOffset.left,
                    relY = e.pageY - parentOffset.top;

                if ($(e.target).parents('g').attr('id') != '') {
                    var shopName = $(e.target).parents('g').first().find('text').text();

                    if (shopName != '') {
                        if (shopName == 'U112') {
                            shopName = 'U112-2';
                        }
                        let shopsData = $('.js-map').data('shops');
                        let shopTitle = '';
                        $.each(shopsData, function (index, value) {
                            if (value.location == shopName) {
                                shopTitle = value.title;
                            }
                        });
                        if (shopTitle.length > 0) {
                            $(e.target).parents('.tc-slider-item').find('.map-tooltip').addClass('visible').css({
                                'left': relX,
                                'top': relY
                            }).text(shopTitle);
                        } else {
                            $(e.target).parents('.tc-slider-item').find('.map-tooltip').addClass('visible').css({
                                'left': relX,
                                'top': relY
                            }).text(shopName);
                        }
                    }
                }
            }).on('mouseleave', '.floor-container g', function (e) {
                $(e.target).parents('.tc-slider-item').find('.map-tooltip').removeClass('visible');
            }).on('click', '.floor-container g', function (e) {
                var shopID = $(e.target).parents('g').first();

                if (shopID[0].id == 'U112') {
                    shopID[0].id = 'U112-2';
                }
                $.ajax({
                    'method': 'post',
                    'data': 'location=' + shopID[0].id,
                    'dataType': 'json',
                    'url': '/ajax/shop',
                }).done(function (resp) {
                    if (resp.html) {
                        $(".js-map-infobox").html(resp.html);
                    }
                });
            });
        }

        if ($(".js-select-category").length) {
            $(".js-select-category").click(function (e) {
                e.preventDefault();
                $("g").removeClass('active');
                let category = $(this).data("category");
                $.ajax({
                    'method': 'post',
                    'data': 'category=' + category,
                    'dataType': 'json',
                    'url': '/ajax/shopcategory',
                }).done(function (resp) {
                    if (resp.shops) {
                        $.each(resp.shops, function (index, value) {
                            if (value.location != '') {
                                $("#" + value.location).addClass('active');
                            }
                        });
                    }
                });
            });
        }
    }
    this.momcard = function () {
        if ($('#momcard-form').length) {
            $('#momcard-form .js-send-btn').click(function (e) {
                e.preventDefault();
                let cardnumber = $("#card-number").val();
                var textWait = $(this).data('wait'),
                    textSend = $(this).data('send');
                if (cardnumber) {
                    $(this).text(textWait);
                    $('.services-item .services-layer').removeClass('sys');
                    $.ajax({
                        'method': 'get',
                        'dataType': 'json',
                        'url': '/api/1.0/momcard/' + cardnumber,
                    }).done(function (resp) {
                        if (resp.balance) {
                            $(".js-momcard-balance").html(resp.balance);
                        }
                        if (resp.temp_balance) {
                            $(".js-momcard-parking").html(resp.temp_balance);
                        }
                        if (resp.total) {
                            $(".js-momcard-total").html(resp.total);
                        }
                        if (resp.status) {
                            $('.services-layer-momcard').addClass('sys');
                        }
                    });

                } else {
                    $("#card-number").focus();
                }
            });

            $('.services-layer-close').click(function () {
                $(this).parents('#momcard-form').find('.services-layer').removeClass('sys');
            });
        }
    }

    this.search = function () {
        $("#js-search-box").keyup(function () {
            let searchText = $(this).val();
            if (searchText.length > 0) {
                $(".js-resut-list").css('display', 'none');
                $.ajax({
                    'method': 'get',
                    'data': 'search=' + searchText,
                    'dataType': 'json',
                    'url': '/search',
                }).done(function (resp) {
                    if (resp.items) {
                        $("#js-search-box-result-list").html("");
                        $(".js-resut-list").css('display', 'block');
                        $.each(resp.items, function (index, value) {
                            $("#js-search-box-result-list").append('<li><a href="' + value.url + '" title="">' + value.title + '</a></li>');
                        })
                    }
                });
            }
        });
    }


    this.cookieAccept();
    this.getMoreNews();
    this.newsletter();
    this.contactForm();
    this.xmas();
    this.map();
    this.momcard();
    this.search();
}

function momparkterkep() {
    if ($('.tc-map').length || $('.tc-shoppage').length) {
        $('svg').each(function (i, v) {
            if (!$(v).find('#map').hasClass('floor-container')) {
                $(v).find('#map').addClass('floor-container');
            }
        });
    }
}

$(document).ready(function () {
    new Main();
    momparkterkep();
});
