jQuery(function ($) {

    "use strict";



    $(document).ready(function () { 


// $crisp.push(["do", "chat:open"]);

        
        var d = new Date();
        var n = d.getHours();
        var n2 = d.getMinutes();
        if(n<19 && n>9){
        function progress(timeleft, timetotal, $element) {
            var progressBarWidth = timeleft * $element.width() / timetotal;
            // $element.find('div').animate({ width: progressBarWidth }, 500).html(timeleft + " seconds to go");
            $element.find('div').animate({ width: progressBarWidth }, 500);
            if(timeleft > 0) {
                setTimeout(function() {
                    progress(timeleft - 1, timetotal, $element);
                }, 1000);
            }
        };
        // var d = new Date();
        // var n = d.getHours();
        // var n2 = d.getMinutes();
        // if(n<19 && n>9){
            console.log(n);
            console.log(n2);
            n = n*60;
            var ntotal = n+n2;
            console.log(ntotal);
            var oradeinceput = 9*60;
            console.log(oradeinceput);
            var ptrsecunde = ntotal -oradeinceput;
            console.log(ptrsecunde);
            var ntotalsecunde = ptrsecunde*60;

            console.log(ntotalsecunde);
            var vartimeleft = 36000-ntotalsecunde;
            console.log(vartimeleft);

        progress(vartimeleft, 36000, $('#progressBar'));
        }
        else {
            $('#progressBar').hide();

        }
    });




    /******************************************************* 
     *****  Gallerys  *****
     *********************************************************/


    $('.fancybox').fancybox();

    $(".fancybox-video,.big-view .fancybox , .popup-youtube").on("click", function () {
        $.fancybox({
            'padding': 0,
            'autoScale': false,
            'transitionIn': 'none',
            'transitionOut': 'none',
            'title': this.title,
            'width': 680,
            'height': 495,
            'href': this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
            'type': 'swf',
            'swf': {
                'wmode': 'transparent',
                'allowfullscreen': 'true'
            }
        });

        return false;
    });



    /******************************************************* 
     *****   Dropdown  *****
     *********************************************************/


    // $(function () {
    //     var $selects = $('.home-template select');

    //     $selects.easyDropDown({
    //         cutOff: 10,
    //         wrapperClass: 'tmp-select-dropdown',
    //         onChange: function (selected) {
    //             // do something
    //         }
    //     });
    // });


    // $(".home-template  #btc_calc").next().next("a").hide();







    /******************************************************* 
     *****  Carousel  *****
     *********************************************************/


    /****  Owl ****/


    $(".enable-owl-carousel").each(function (i) {
        var $owl = $(this);
        var navigationData = $owl.data('navigation');
        var paginationData = $owl.data('pagination');
        var singleItemData = $owl.data('single-item');
        var autoPlayData = $owl.data('autoplay');
        var transitionStyleData = $owl.data('transition-style');
        var mainSliderData = $owl.data('main-text-animation');
        var afterInitDelay = $owl.data('after-init-delay');
        var stopOnHoverData = $owl.data('stop-on-hover');
        var min600 = $owl.data('min600');
        var min800 = $owl.data('min800');
        var itemsData = $owl.data('items');
        var animateOutData = $owl.data('animate-out');
        var animateInData = $owl.data('animate-in');
        var min1200 = $owl.data('min1200');
        var responsiveItems = $owl.data('responsive-items');
        var navTextData = $owl.data('nav-text') == null ? ["<i class='fa fa-angle-left'></i>", "<i class='fa fa-angle-right'></i>"] : $owl.data('nav-text');
        $owl.owlCarousel({
            nav: navigationData,
            dots: paginationData,
            singleItem: singleItemData,
            autoHeight: false,
            autoplay: autoPlayData,
            transitionStyle: transitionStyleData,
            stopOnHover: stopOnHoverData,
            animateOut: animateOutData,
            animateIn: animateInData,
            items: itemsData,
            navText: navTextData,
            loop: true,

            responsive: {
                0: {
                    items: responsiveItems
                },
                767: {
                    items: itemsData
                }

            },
            itemsCustom: [
                [0, 1],
                [600, min600],
                [800, min800],
                [1200, min1200]
            ],
            afterInit: function (elem) {
                if (mainSliderData) {
                    setTimeout(function () {
                        $('.main-slider_zoomIn').css('visibility', 'visible').removeClass('zoomIn').addClass('zoomIn');
                        $('.main-slider_fadeInLeft').css('visibility', 'visible').removeClass('fadeInLeft').addClass('fadeInLeft');
                        $('.main-slider_fadeInLeftBig').css('visibility', 'visible').removeClass('fadeInLeftBig').addClass('fadeInLeftBig');
                        $('.main-slider_fadeInRightBig').css('visibility', 'visible').removeClass('fadeInRightBig').addClass('fadeInRightBig');
                    }, afterInitDelay);
                }
            },
            beforeMove: function (elem) {
                if (mainSliderData) {
                    $('.main-slider_zoomIn').css('visibility', 'hidden').removeClass('zoomIn');
                    $('.main-slider_slideInUp').css('visibility', 'hidden').removeClass('slideInUp');
                    $('.main-slider_fadeInLeft').css('visibility', 'hidden').removeClass('fadeInLeft');
                    $('.main-slider_fadeInRight').css('visibility', 'hidden').removeClass('fadeInRight');
                    $('.main-slider_fadeInLeftBig').css('visibility', 'hidden').removeClass('fadeInLeftBig');
                    $('.main-slider_fadeInRightBig').css('visibility', 'hidden').removeClass('fadeInRightBig');
                }
            },
            afterMove: sliderContentAnimate,
            afterUpdate: sliderContentAnimate,
        });
    });

    function sliderContentAnimate(elem) {
        var $elem = elem;
        var afterMoveDelay = $elem.data('after-move-delay');
        var mainSliderData = $elem.data('main-text-animation');
        if (mainSliderData) {
            setTimeout(function () {
                $('.main-slider_zoomIn').css('visibility', 'visible').addClass('zoomIn');
                $('.main-slider_slideInUp').css('visibility', 'visible').addClass('slideInUp');
                $('.main-slider_fadeInLeft').css('visibility', 'visible').addClass('fadeInLeft');
                $('.main-slider_fadeInRight').css('visibility', 'visible').addClass('fadeInRight');
                $('.main-slider_fadeInLeftBig').css('visibility', 'visible').addClass('fadeInLeftBig');
                $('.main-slider_fadeInRightBig').css('visibility', 'visible').addClass('fadeInRightBig');
            }, afterMoveDelay);
        }
    }



    function carouselStarts() {


        var owlCarouselBox = $('.b-single-gallery-carousel');
        if (owlCarouselBox && owlCarouselBox.length) {
            owlCarouselBox.each(function (i) {
                var $owl = $(this);

                var loopData = $owl.data('loop');
                var centerData = $owl.data('center');
                var autoWidthData = $owl.data('auto-width');
                var dotsData = $owl.data('dots');
                var navData = $owl.data('nav');
                var marginData = $owl.data('margin');
                var responsiveClassData = $owl.data('responsive-class');
                var responsiveData = $owl.data('responsive');
                var sliderNext = $owl.data('slider-next');
                var sliderPrev = $owl.data('slider-prev');

                $owl.owlCarousel({
                    loop: loopData,
                    center: centerData,
                    autoWidth: autoWidthData,
                    dots: dotsData,
                    nav: navData,
                    autoplay: true,
                    margin: marginData,
                    responsiveClass: responsiveClassData,
                    responsive: responsiveData
                });


                $(sliderNext).on("click", function () {
                    $owl.trigger('next.owl.carousel');
                });


                $(sliderPrev).on("click", function () {
                    $owl.trigger('prev.owl.carousel');
                });



            });
        }




    }

    setTimeout(carouselStarts, 100);


    /**** Bxslider ****/


    if ($('.bxslider-services').length > 0) {


        $('.bxslider-services').bxSlider({
            mode: 'horizontal',
            captions: true,
            pager: false,
            infiniteLoop: false,
            nextSelector: '#pager-services-next',
            prevSelector: '#pager-services-prev',
            nextText: '<i class="fa fa-angle-right" aria-hidden="true"></i>',
            prevText: '<i class="fa fa-angle-left" aria-hidden="true"></i>'
        });
    }




    //with custom pager
    if ($('.bxslider-services').length > 0) {









        $('.bxslider-services').bxSlider({
            pagerCustom: ".bx-pager-services",
            minSlides: 1,
            maxSlides: 1,
            nextSelector: '#pager-services-next',
            prevSelector: '#pager-services-prev',
            nextText: '<i class="fa fa-angle-right" aria-hidden="true"></i>',
            prevText: '<i class="fa fa-angle-left" aria-hidden="true"></i>'
        });





    }



    if ($('.bxslider-history').length > 0) {


        $('.bxslider-history').bxSlider({
            mode: 'horizontal',
            captions: true,
            pager: false,
            infiniteLoop: false,
            nextSelector: '#pager-history-next',
            prevSelector: '#pager-history-prev',
            nextText: '<i class="fa fa-angle-right" aria-hidden="true"></i>',
            prevText: '<i class="fa fa-angle-left" aria-hidden="true"></i>'
        });
    }


    //with custom pager
    if ($('.bxslider-history').length > 0) {

        $('.bxslider-history').bxSlider({
            pagerCustom: '.bx-pager-history',
            minSlides: 1,
            maxSlides: 1,
            infiniteLoop: false,
            nextSelector: '#pager-history-next',
            prevSelector: '#pager-history-prev',
            nextText: '<i class="fa fa-angle-right" aria-hidden="true"></i>',
            prevText: '<i class="fa fa-angle-left" aria-hidden="true"></i>'
        });
    }



    if ($('.bxslider-reviews').length > 0) {


        $('.bxslider-reviews').bxSlider({
            mode: 'horizontal',
            captions: true,
            pager: false,
            infiniteLoop: false,
            nextSelector: '#pager-reviews-next',
            prevSelector: '#pager-reviews-prev',
            nextText: '<i class="fa fa-angle-right" aria-hidden="true"></i>',
            prevText: '<i class="fa fa-angle-left" aria-hidden="true"></i>'
        });
    }


    //with custom pager
    if ($('.bxslider-reviews').length > 0) {


        var bxReviewPager = $('.bxslider-reviews .bx-pager');

        $('.bxslider-reviews').bxSlider({
            pagerCustom: '.bx-pager-reviews',
            minSlides: 1,
            maxSlides: 1,
            infiniteLoop: false,
            nextSelector: '#pager-reviews-next',
            prevSelector: '#pager-reviews-prev',
            nextText: '<i class="fa fa-angle-right" aria-hidden="true"></i>',
            prevText: '<i class="fa fa-angle-left" aria-hidden="true"></i>'
        });
    }




    $(".carousel-post").bxSlider({
        adaptiveHeight: true,
        auto: true,
        nextText: '<i class="fa fa-angle-right" aria-hidden="true"></i>',
        prevText: '<i class="fa fa-angle-left" aria-hidden="true"></i>'
    });


    /****  Footer fixed ****/



    if ($('.fixed-footer').length) {


        var fixedFooter = $(".fixed-footer").height();

        $(".layout-theme").css("marginBottom", fixedFooter);



    }






    /****  Slick slider ****/



    if ($('.b-home-slider').length > 0) {

        $('.b-home-slider').slick({
            prevArrow: $('#home-slider-prev'),
            nextArrow: $('#home-slider-next')
            // other settings can be set using the data-slick attribute on the slick element in the HTML markup
        });

    }

    if ($('.b-latest-carousel').length > 0) {

        $('.b-latest-carousel').slick({
            variableWidth: true,
            centerMode: true,
            centerPadding: '80px',
            slidesToShow: 1,
            prevArrow: $('#slick-slideshow-prev'),
            nextArrow: $('#slick-slideshow-next'),
            responsive: [{
                breakpoint: 1200,
                settings: {
                    slidesToShow: 1,
                    centerPadding: '20px',
                    arrows: true
                }
            }, {
                breakpoint: 991,
                settings: {
                    slidesToShow: 1,
                    centerPadding: '10px',
                    arrows: true
                }
            }, {
                breakpoint: 639,
                settings: {
                    slidesToShow: 1,
                    centerPadding: '0',
                    arrows: true,
                    variableWidth: false,
                    centerMode: false
                }
            }]
        });
    }

    if ($('.b-team-carousel').length > 0) {

        $('.b-team-carousel').slick({
            infinite: true,
            slidesToShow: 1,
            centerMode: true,
            variableWidth: true,
            prevArrow: $('#team-slideshow-prev'),
            nextArrow: $('#team-slideshow-next')
        });
    }

    if ($('.b-recent-carousel').length > 0) {

        $('.b-recent-carousel').slick({
            infinite: true,
            slidesToShow: 1,
            centerMode: true,
            centerPadding: '0px',
            variableWidth: true,
            prevArrow: $('#recent-slideshow-prev'),
            nextArrow: $('#recent-slideshow-next')
        });
    }



    /****  Sly slider  ****/


    if ($('.b-sly-slider').length > 0) {


        (function () {
            var $frame = $('#frame');
            var $wrap = $frame.parent();

            // Call Sly on frame
            $frame.sly({
                horizontal: 1,
                itemNav: 'basic',
                activateOn: 'click',
                mouseDragging: 1,
                touchDragging: 1,
                releaseSwing: 1,
                scrollBar: $wrap.find('.scrollbar'),
                scrollBy: 1,
                speed: 300,
                elasticBounds: 1,
                easing: 'easeOutExpo',
                dragHandle: 1,
                dynamicHandle: 0,
                clickBar: 1
            });

            // Sly to be reloaded when the user resizes the browser
            $(window).resize(function (e) {
                $frame.sly('reload');
            });

        }());
    }




    /******************************************************* 
     *****   Big adaptive title   ***
     *********************************************************/



    $(".b-upper-title").slabText({
        // Don't slabtext the headers if the viewport is under 380px
        "viewportBreakpoint": 380
    });



    /******************************************************* 
     *****   Full Width, Verticale Title ,  SVG Creator   ***
     *********************************************************/



    function fullWidthSection() {

        var windowWidth = $(window).width();

        var widthContainer = $('.home-template > .container, .portfolio-section  > .container , .page-content > .container').width();
        var widthContainerTitle = $('.home-template > .container, .portfolio-section  > .container , .page-content > .container').width();

        var fullWidth1 = windowWidth - widthContainer;
        var fullWidth2 = fullWidth1 / 2;

        var fullWidthTitle1 = windowWidth - widthContainerTitle;
        var fullWidthTitle2 = fullWidthTitle1 / 2;

        $('.js_stretch_anchor').css("width", windowWidth + 30);
        $('.js_stretch_anchor').css("margin-left", -fullWidth2 - 15);

        $(' .jarallax-full-width').css("min-width", windowWidth);
        $(' .jarallax-full-width').css("margin-left", -fullWidth2 + 15);


        var widthContainerFooter = $('.footer  > .container').width();


        var fullWidthFoot = windowWidth - widthContainerFooter;
        var fullWidthFoot2 = fullWidthFoot / 2;


        $(' .footer  .jarallax-full-width').css("min-width", windowWidth + 30);
        $(' .footer  .jarallax-full-width').css("margin-left", -fullWidthFoot2);


        $('.vertical-left').css("left", -fullWidthTitle2);
        $('.vertical-right').css("right", -fullWidthTitle2);




        $('circle.pix-decor-circle').each(function (index) {
            var hor = $(this).data('horizontal') * windowWidth / 100;
            $(this).attr('cx', hor);
        });

        if (windowWidth <= 768) {
            $('.section-decor-wrap').each(function (index) {
                var top, height;
                top = $(this).data('top');
                $(this).css('top', -(top / 2 - 1));
                height = $(this).data('height');
                $(this).css('height', height / 2);
            });
            $('.section-decor-wrap svg').each(function (index) {
                var height;
                height = $(this).data('height');
                $(this).height(height / 2);
            });
            $('.section-decor-wrap pattern').each(function (index) {
                var height;
                height = $(this).data('height');
                $(this).attr('height', height / 2 * 10);
            });
            $('.section-decor-wrap rect').each(function (index) {
                var height;
                height = $(this).data('height');
                $(this).attr('height', height / 2);
            });
            $('circle.pix-decor-circle').each(function (index) {
                var ver = $(this).data('vertical') / 2;
                $(this).attr('cy', ver);
            });
        } else {
            $('.section-decor-wrap').each(function (index) {
                $(this).css('top', -($(this).data('top') - 1));
                $(this).css('height', $(this).data('height'));
            });
            $('.section-decor-wrap svg').each(function (index) {
                $(this).height($(this).data('height'));
            });
            $('.section-decor-wrap pattern').each(function (index) {
                $(this).attr('height', $(this).data('height') * 10);
            });
            $('.section-decor-wrap rect').each(function (index) {
                $(this).attr('height', $(this).data('height'));
            });
            $('circle.pix-decor-circle').each(function (index) {
                $(this).attr('cy', $(this).data('vertical'));
            });
        }

    }

    fullWidthSection();
    $(window).resize(function () {
        fullWidthSection()
    });


    /******************************************************* 
     **********   jarallax   ***************
     *********************************************************/



    $(".vc_row").children().each(function (i) {

        var rowChecker = $(this);

        if (rowChecker.is(".jarallax")) {

            var rowPadTop = $(this).parent().css("padding-top");
            var rowPadBot = $(this).parent().css("padding-bottom");

            $(this).parent().addClass("jarallax-nopadding");

            $(this).css("padding-top", rowPadTop);
            $(this).css("padding-bottom", rowPadBot);

        }

    });





    /******************************************************* 
     *****   LOAD MORE PORTFOLIO   *****
     *********************************************************/


    function loadMore() {
        "use strict";

        $('.load-more a').on('click', function (e) {
            e.preventDefault();

            var current_page = $(this).parent().attr('data-current');
            var max_pages = $(this).parent().attr('data-max-pages');
            var wrapper_id = '#' + $(this).parents('.portfolio-list-section').attr('id');
            var link = $(this).attr('href');
            var $container = wrapper_id + ' .portfolio-masonry-holder';
            var container = $($container);
            var $anchor = wrapper_id + ' .portfolio-pagination .load-more a';
            var next_href = $(this).attr('href'); // Get URL for the next set of posts
            var btn = $(this);

            var load_more_holder = $(this).parents('.portfolio-pagination');
            var loading_holder = $(this).parents('.portfolio-pagination').next();

            load_more_holder.hide();
            loading_holder.show();

            $('.folio-isotop-filter li').find('.selected').removeClass('selected');
            $('.folio-isotop-filter ul li:first a').addClass('selected');

            container.isotope({
                filter: '*'
            });

            $.get(link + '', function (data) {

                console.log(wrapper_id);

                var new_content = $($container, data).wrapInner('').html(); // Grab just the content
                next_href = $($anchor, data).attr('href'); // Get the new href

                $(container, data).waitForImages(function () {

                    container.append(new_content);
                    // trigger isotope again after images have been loaded
                    container.imagesLoaded(function () {
                        container.isotope('reloadItems').isotope({
                            sortBy: 'original-order'
                        });
                    });

                    container.children().removeClass('wow');

                    current_page++;

                    if (max_pages > current_page) {
                        btn.attr('href', next_href); // Change the next URL
                    } else {
                        btn.parent().remove();
                    }

                    container.children('.portfolio-pagination:last').remove(); // Remove the original navigation

                    load_more_holder.show();
                    loading_holder.hide();

                    btn.parent().attr('data-current', current_page);
                });

            });
        });
    }
    loadMore();







    /******************************************************* 
     *****  SCROLL HREF  *****
     *********************************************************/




    $("a.rev-btn[href*='#'],  a[href*='#'].tp-caption").on("click", function (event) {
        event.preventDefault();

        var id = $(this).attr('href'),

            top = $(id).offset().top;

        $('body,html').animate({
            scrollTop: top
        }, 1500);
    });









    /******************************************************* 
     *****  Hover application  *****
     *********************************************************/




    $(".app-features").on('hover', function (e) {
        var value = $(this).attr('data-src');
        $(".service-application-img img").attr("src", value);
    });







    $(".app-features").on("click", "p", function () {
        $owl.trigger('next.owl.carousel');
    });



    /******************************************************* 
     *****  sticky sidebar  *****
     *********************************************************/



    var windowWidth = $(window).width();

    if (windowWidth > 1000) {


        if ($('#work-body-sticky').length > 0) {

            (function () {
                var a = document.querySelector('#work-body-sticky'),
                    b = null,
                    P = 0;
                window.addEventListener('scroll', Ascroll, false);
                document.body.addEventListener('scroll', Ascroll, false);

                function Ascroll() {
                    if (b == null) {
                        var Sa = getComputedStyle(a, ''),
                            s = '';
                        for (var i = 0; i < Sa.length; i++) {
                            if (Sa[i].indexOf('overflow') == 0 || Sa[i].indexOf('padding') == 0 || Sa[i].indexOf('border') == 0 || Sa[i].indexOf('outline') == 0 || Sa[i].indexOf('box-shadow') == 0 || Sa[i].indexOf('background') == 0) {
                                s += Sa[i] + ': ' + Sa.getPropertyValue(Sa[i]) + '; '
                            }
                        }
                        b = document.createElement('div');
                        b.style.cssText = s + ' box-sizing: border-box; width: ' + a.offsetWidth + 'px;';
                        a.insertBefore(b, a.firstChild);
                        var l = a.childNodes.length;
                        for (var i = 1; i < l; i++) {
                            b.appendChild(a.childNodes[1]);
                        }
                        a.style.height = b.getBoundingClientRect().height + 'px';
                        a.style.padding = '0';
                        a.style.border = '0';
                    }
                    var Ra = a.getBoundingClientRect(),
                        R = Math.round(Ra.top + b.getBoundingClientRect().height - document.querySelector('footer').getBoundingClientRect().top + 100);
                    if ((Ra.top - P) <= 0) {
                        if ((Ra.top - P) <= R) {
                            b.className = 'stop';
                            b.style.top = -R + 'px';
                        } else {
                            b.className = 'sticky';
                            b.style.top = P + 'px';
                        }
                    } else {
                        b.className = '';
                        b.style.top = '';
                    }
                    window.addEventListener('resize', function () {
                        a.children[0].style.width = getComputedStyle(a, '').width
                    }, false);
                }
            })()

        }


        var DelayLoad = function () {



            $(".sticky-bar").stick_in_parent();

        };

        setTimeout(DelayLoad, 2000);





    }









    /******************************************************* 
     *****  scrollie background color  *****
     *********************************************************/



    var wHeight = $(window).height();

    $('.scrollie')
        .height(wHeight)
        .scrollie({
            scrollOffset: -250,
            scrollingInView: function (elem) {
                var bgColor = elem.data('background');
                $('body').css('background-color', bgColor);

            }
        });




    /******************************************************* 
     *****  ISOTOPE FILTER  *****
     *********************************************************/






    var portfolio_container = $('.portfolio-masonry-holder');

    portfolio_container.imagesLoaded(function () {
        portfolio_container.isotope({
            // options
            itemSelector: '.item',
            layoutMode: 'masonry',
        });
    });





    $('.folio-isotop-filter a').on('click', function () {

        var container = $('.portfolio-masonry-holder');
        var filterValue = $(this).attr('data-filter');

        //don't proceed if already selected
        if ($(this).hasClass('selected')) {
            return false;
        }
        var $portfolio_optionSet = $(this).parents('.folio-option-set');
        $portfolio_optionSet.find('.selected').removeClass('selected');
        $(this).addClass('selected');

        filterValue = filterValue === 'false' ? false : filterValue;
        portfolio_container.isotope({
            filter: filterValue
        });
        return false;
    });









    var blog_container = $('.blog-masonry-holder');


    blog_container.isotope({
        // options
        itemSelector: '.item',
        layoutMode: 'masonry',
    });






    var $container = $('#gallery-items');

    $(window).load(function () {
        $container.isotope({
            //		    resizable: false, // disable normal resizing
            transitionDuration: '0.65s',
            masonry: {
                columnWidth: $container.find('.gallery-item:not(.wide)')[0]
            }
        });

        $(window).resize(function () {
            $container.isotope('layout');
        });
    });









    /////////////////////////////////////
    //  Chars Start
    /////////////////////////////////////

    if ($('body').length) {
        $(window).on('scroll', function () {
            var winH = $(window).scrollTop();

            $('.tmpl-stats').waypoint(function () {
                $('.js-chart').each(function () {
                    CharsStart();
                });
            }, {
                offset: '120%'
            });
        });
    }


    function CharsStart() {
        $('.js-chart').easyPieChart({
            barColor: false,
            trackColor: false,
            scaleColor: false,
            scaleLength: false,
            lineCap: false,
            lineWidth: false,
            size: false,
            animate: 7000,

            onStep: function (from, to, percent) {
                $(this.el).find('.js-percent').text(Math.round(percent));
            }
        });

    }



    /////////////////////////////////////////////////////////////////
    // Map/Form Switcher
    /////////////////////////////////////////////////////////////////

    $(".map-form-switcher .switcher-toggle").on('click', function (e) {
        e.preventDefault();
        $('.b-map-form-holder .b-contact-form').fadeToggle("300", "linear");
        $('.b-map-form-holder').toggleClass("map-active");
    });





    /////////////////////////////////////////////////////////////////
    // Map/Form Switcher
    /////////////////////////////////////////////////////////////////

    $(".panel-heading").on('click', function (e) {

        $(".panel-heading").removeClass("active");
        $(this).addClass("active");

    });





    /**
     * WooCommerce Variation Dynamic SubTotal
     */
    function addThousandSep(n, thousand_separator) {

        var rx = /(\d+)(\d{3})/;
        return String(n).replace(/^\d+/, function (w) {
            while (rx.test(w)) {
                w = w.replace(rx, '$1' + thousand_separator + '$2');
            }
            return w;
        });

    };

    function sub_total_change(price, count) {
        var currency, decimal_separator, thousand_separator, decimals, currency_pos;
        currency = $('.fixar_woo_currency').val();
        decimal_separator = $('.fixar_woo_decimal_separator').val();
        thousand_separator = $('.fixar_woo_thousand_separator').val();
        decimals = $('.fixar_woo_decimals').val();
        currency_pos = $('.fixar_woo_currency_pos').val();

        var totalPrice = parseFloat(price) * count,
            htmlPrice;

        totalPrice = totalPrice.toFixed(decimals);
        htmlPrice = totalPrice.toString().replace('.', decimal_separator);
        if (thousand_separator.length > 0) {
            htmlPrice = addThousandSep(htmlPrice, thousand_separator);
        }
        if (currency_pos == 'right') {
            htmlPrice = htmlPrice + currency;
        } else if (currency_pos == 'right_space') {
            htmlPrice = htmlPrice + ' ' + currency;
        } else if (currency_pos == 'left_space') {
            htmlPrice = currency + ' ' + htmlPrice;
        } else {
            htmlPrice = currency + htmlPrice;
        }

        $('.form-cart__price-total').html(htmlPrice);
    }


    $('.shopping_cart-quantity input').on('change', function () {
        var currency = $('.fixar_woo_currency').val();
        var sufix = '';
        $.each($('.tawcvs-swatches span.selected'), function () {
            sufix = sufix + '_' + $(this).data('value');
        });
        var price = $('.fixar_woo_price' + sufix).val();
        sub_total_change(price, $(this).val());
        //alert( "Handler for .change() called: "+ price );
    });

    $('.table-container .single_variation_wrap').on('bind', 'DOMSubtreeModified', function () {


        var currency = $('.fixar_woo_currency').val();
        var sufix = '';
        $.each($('.tawcvs-swatches span.selected'), function () {
            sufix = sufix + '_' + $(this).data('value');
        });
        var price = $('.fixar_woo_price' + sufix).val();
        var count = $('.shopping_cart-quantity input').val();
        if (price !== 'undefined' && price != '')
            sub_total_change(price, count);
    });
});

new WOW().init();

//custom script

(function ($) {

    var teethForm = function () {
        var site_url = _cabinet.site_url;
        var ajax_url = _cabinet.ajax_url;

        //var selected_date = '';
        //var $hover_container = $('.hovered');
        var $mouthMap = $('#mouthMap');
        var clickedKey;
        var clickedTeeths = [];
        var $data = $('input[name="data"]');
        var visibleDates = { dates: [] };

        var emailData = {};

        $('div.login a.ajax').colorbox();
        $("input:text, select, input:radio").uniform();

        $('select#doctors').attr('disabled', true);
        $.uniform.update('#doctors');

        var $form_reservation = $('#form_reservation');

        $form_reservation.submit(function (e) {

            var isValid = $form_reservation.validationEngine('validate');
            if (!isValid)
                return false;

            e.preventDefault();

            emailData.clientInfo = '';
            emailData.clientInfo += '<b>Nume: </b>' + $('#step_two_form input[name="nume"]').val() + "\n";
            emailData.clientInfo += '<b>Varsta: </b>' + $('#step_two_form input[name="varsta"]').val() + "\n";
            emailData.clientInfo += '<b>Email: </b>' + $('#step_two_form input[name="email"]').val() + "\n";
            emailData.clientInfo += '<b>Telefon: </b>' + $('#step_two_form input[name="telefon"]').val() + "\n";

            emailData.scheduleInfo = '';
            emailData.scheduleInfo += '<b>Type: </b>' + $('input[name="jour"]:checked').val() + "\n";
            emailData.scheduleInfo += '<b>Hour: </b>' + (emailData.scheduleType == 'matines' ? $('.first-interval').val() : $('.last-interval').val()) + "\n";
            emailData.scheduleInfo += '<b>Date: </b>' + $('input[name="data"]').val() + "\n";

            emailData.clientEmail = $('#step_two_form input[name="email"]').val();

            $.ajax({
                url: ajax_url,
                method: 'POST',
                data: {
                    action: '_submit',
                    emailData: emailData
                }
            }).done(function (result) {
                console.log(result);
            });
        });

        var defaultMapsterOpts = {
            fillOpacity: 0.5,
            render_highlight: {
                fillColor: '2aff00',
                stroke: false
            },
            render_select: {
                stroke: false,
                altImage: site_url + '/images/general/hover.png'
            },
            fadeInterval: 50,
            mapKey: 'title',
            mapValue: 'alt',
            showToolTip: false,
            isSelectable: true,
            singleSelect: false
        };

        $mouthMap.mapster(defaultMapsterOpts);
        var state = !$mouthMap.mapster('get_options').isDeselectable;
        $mouthMap.mapster("set_options", { isDeselectable: state });

        var $teeth = $('#Map area'),
            $popup = $('#popup'),
            $step1_indicator = $('div#step_one').children('div#indicator');

        $problemList = $('#step_one_selection ul'),
            $problemListText = $('#step_one_info'),
            $problemList_container = $problemList.parent(),
            $emptyText = $('#step_one div.empty_text');
        scroller();
        $('.scrollbar.vertical').hide();

        $popup.delegate("input[name='dinte']", 'click', function (e) {
            var $questions_type_id = $(this).val();
            // $.ajax({
            //     url: siteurl + '/front/questions/'+$questions_type_id,
            //     success: function(result) {
            //         $('#popup #questions').html(result);
            //     },
            //     async: false
            // });

            $.ajax({
                url: ajax_url,
                method: 'GET',
                data: {
                    action: '_questions',
                    questions_type_id: $questions_type_id
                }
            }).done(function (result) {
                $('#popup #questions').html(result);
            });
        });

        $('body').delegate('#judet', 'change', function (e) {
            $.getJSON(siteurl + "/front/oras", { jid: $(this).val(), ajax: 'true' }, function (j) {
                var options = '';
                for (var i = 0; i < j.length; i++) {
                    options += '<option value="' + j[i].nume + '">' + j[i].nume + '</option>';
                }
                $("#oras").html(options);
                $.uniform.update("select#oras");
            })
        });

        $('body').delegate('#intervals', 'change', function (e) {
            resetDoctors();
        });

        $teeth.bind('click', function (e) {
            e.preventDefault();

            $mouthMap.mapster("set_options", { isSelectable: true });

            var $tooth = $(this);
            toothNumber = $tooth.attr('alt');
            if ($popup.is(':visible')) {
                $mouthMap.mapster("set_options", { singleSelect: true });
                $mouthMap.mapster("set_options", { isSelectable: false });
                return;
            }

            if ($.inArray(toothNumber, clickedTeeths) != -1) {
                return;
            }

            var rectcoords = $(this).attr('coords');
            coords = rectcoords.split(',');
            $x = coords[0];
            $y = coords[3];

            // $.getJSON(siteurl + '/front/popup',
            //         {t: toothNumber},
            //         function(result) {
            //             $popup.css({ top: '3px', left: $x + 20 }).show();
            //             $popup.find('input[type=hidden]:first').attr('name', 'teeth-'+result.o).val(result.d);
            //             $popup.find('input[type=radio]:first').attr('checked','checked').click();
            //             clickedKey = $tooth.attr('alt');
            //         }
            // );

            $.ajax({
                url: ajax_url,
                method: 'GET',
                dataType: 'json',
                data: {
                    action: '_popup',
                    t: toothNumber
                }
            }).done(function (result) {
                $popup.css({ top: '3px', left: $x + 20 }).show();
                $popup.find('input[type=hidden]:first').attr('name', 'teeth-' + result.ordinal).val(result.description);
                $popup.find('input[type=radio]:first').attr('checked', 'checked').click();
                clickedKey = $tooth.attr('alt');
            });

        });

        // on popup submit, add in item to the problem list and close the popup
        $popup.delegate('input[type=submit]', 'click', function (e) {
            e.preventDefault();
            var $this = $(this);
            var answers = [], ti = [];
            var incomplete = false;

            $popup.find('select').each(function (i, data) {
                var $this = $(this);
                var answer_info = [];
                var label = $this.attr('data-label');
                var answer = $this.find('option:selected').html();
                var aid = $this.find('option:selected').val();

                if (aid.length == 0) {
                    incomplete = true;
                    return;
                }

                answer_info['aid'] = aid;
                answer_info['qid'] = $this.attr('name');
                answer_info['answer'] = answer;
                answers[label] = answer_info;
            });

            if (incomplete == true) {
                alert('Alege un raspuns.');
                return false;
            }

            var $teeth_info = $popup.find('input[name^=teeth]');
            ti['o'] = $teeth_info.attr('name').split('-')[1];
            ti['d'] = $teeth_info.val();
            clickedTeeths.push(ti['o']);
            $problemList.append(toothTemplate(answers, ti));
            updateListText();
            $popup.hide();
            clickedKey = 0;
            $mouthMap.mapster("set_options", { singleSelect: false });
        });

        $popup.delegate('input[type=reset]', 'click', function (e) {
            e.preventDefault();
            $popup.hide();
            if (clickedKey != 0) {
                $mouthMap.mapster('set', false, clickedKey);
                clickedKey = 0;
            }
        });

        // clicking outside of the popup closes it
        $(document).bind('click', function (e) {
            if ($(e.target).is('#popup *, area'))
                return false;

            $popup.hide();
            if (clickedKey != 0) {
                $mouthMap.mapster('set', false, clickedKey);
                clickedKey = 0;
            }
        });

        $problemList.delegate('a.delete', 'click', function (e) {
            e.preventDefault();
            $(this).parent().remove();
            updateListText();
            var $test = $(this).siblings().find('span[class="ordinal"]');
            clickedKey = $test.text();
            clickedTeeths.splice($.inArray(clickedKey, clickedTeeths, 1));
            $mouthMap.mapster('set', false, clickedKey);
        });

        // $data.datepicker({
        //     firstDay: 1,
        //     dayNamesMin: ['Du','Lu','Ma','Mi','Jo','Vi','Sa'],
        //     monthNames: ['Ianuarie','Februarie','Martie','Aprilie','Mai','Iunie','Iulie','August','Septembrie','Octombrie','Noiembrie','Decembrie'],
        //     prevText: 'Inapoi',
        //     nextText: 'Inainte',
        //     showButtonPanel: true,
        //     defaultDate: +1,
        //     minDate: '0',
        //     currentText: 'Astazi',
        //     dateFormat: 'mm/dd/yy',
        //     onSelect: function(dateText, inst) {
        //         //selected_date = dateText;
        //         $.getJSON(siteurl + "/front/doctors",{i: $('#intervals').val(), d: dateText, ajax: 'true'}, function(j){
        //               var options = '';
        //            if (j !== null) {
        //               for (var i = 0; i < j.length; i++) {
        //                 options += '<option value="' + j[i].uid + '">' + j[i].name + '</option>';
        //               }
        //               $("#doctors").html(options);
        //               $('#doctors').removeAttr('disabled');
        //               $.uniform.update('select#doctors');
        //           } else {
        //               resetDoctors();
        //           }
        //         });

        //     },
        //     beforeShowDay: function(date) {

        //         if ($.inArray(date.valueOf(), visibleDates.dates) != -1) {
        //             return [1, 'enabled', 'Exista disponibilitate'];
        //         } else {
        //             return [0, 'disabled', 'Nu exista disponibilitate'];
        //         }

        //     },
        //     beforeShow: function(input, inst) {
        //         // reset dates array
        //         visibleDates.dates = [];
        //         resetDoctors();
        //         var $intervals = $('#intervals');
        //         if ($intervals.val() == 0) {
        //             alert('Va rugam alegeti un interval.');
        //             return;
        //         }
        //         var date = new Date();
        //         var m = date.getMonth(), d = date.getDate(), y = date.getFullYear();
        //         m = m + 1;
        //         if (m < 10) {
        //             m = '0' + m;
        //         }
        //         if (d < 10) {
        //             d = '0' + d;
        //         }
        //         var myDate = m + '/' + d + '/' + y;
        //         //console.info('beforeShow');

        //         $.ajax({
        //             url: siteurl + "/front/day",
        //             dataType: 'json',  
        //             cache: false,
        //             async: false,
        //             data: {d: myDate, ajax: 'true', i: $('#intervals').val()},
        //             success: function(j){
        //               if (j !== null) {


        //                 $.each(j, function(index, el) {
        //                     var thisDate = new Date(el);
        //                     visibleDates.dates.push(thisDate.valueOf());
        //                 });


        //               } else {
        //                 visibleDates.dates = [];
        //               }
        //             },
        //             complete: function(){
        //                $(inst).datepicker('refresh'); 
        //             }
        //         });  
        //     },
        //     onChangeMonthYear: function(year, month, inst) {
        //         visibleDates.dates = [];
        //         if (month < 10) {
        //             month  = '0' + month;
        //         }

        //         $.ajax({
        //             url: siteurl + "/front/monthYear",
        //             dataType: 'json',  
        //             cache: false,
        //             async: false,
        //             data: {m: month, y: year, ajax: 'true', i: $('#intervals').val()},
        //             success: function(p){
        //               if (p !== null) {

        //                 $.each(p, function(index, el) {
        //                     var thisDate2 = new Date(el);
        //                     console.info(thisDate2.toLocaleDateString());
        //                     visibleDates.dates.push(thisDate2.valueOf());
        //                 });

        //               } else {
        //                 visibleDates.dates = [];
        //               }
        //             },
        //             complete: function(){
        //                 $(inst).datepicker('refresh');
        //             }
        //         });  
        //     }
        // });

        $data.datepicker({
            firstDay: 1,
            defaultDate: +1,
            minDate: '0',
        });

        function fetchReservations(year, month, inst) {
            var reservation = [];
            $.ajax({
                url: siteurl + "/front/day",
                dataType: 'json',
                cache: false,
                async: false,
                data: { i: interval, ajax: 'true' },
                success: function (j) {

                    if (j != null) {
                        for (i = 0; i < j.length; i++) {
                            var myDate = j[i]['date'];
                            var dateArray = myDate.split('/');
                            if (date.getMonth() == dateArray[0] - 1
                                && date.getDate() == dateArray[1]
                                && date.getFullYear() == dateArray[2]) {

                                return [true, "enabled", "Exista disponibilitate."];
                            }
                        }
                    } else {
                        return [false, "disabled", "Nu exista disponibilitate."];
                    }
                },
                complete: function (e) {
                    $data.datepicker('refresh');
                }
            });
        }

        function resetDoctors() {
            $('#doctors').empty().append('<option selected="selected" value="">Doctor/Cabinet</option>');
            $('#doctors').attr('disabled', true);
            $.uniform.update('select#doctors');
        }

        function toothTemplate(answers, teeth) {
            var answerList = '', inputList = '';
            var problem_index = $problemList.children().size() + 1;
            emailData.problems = '';

            for (i in answers) {
                answerList += '<span class="question">' + i + ':</span>' + '<span class="answer">' + answers[i]['answer'] + '</span>' + '<br>';
                inputList += '<input type="hidden" name="teeth[' + teeth['o'] + '][' + answers[i]['qid'] + ']" value="' + answers[i]['aid'] + '" />';

                emailData.problems += '<b>' + i + ': </b>' + answers[i]['answer'] + "\n";
            }

            return '<li>' +
                inputList +
                '<table class="selection_number"><tr><td valign="middle">' + problem_index + '</td></tr></table>' +
                '<a href="#" class="delete" title="sterge">x</a>' +
                '<div class="selection_info">' +
                '<black><span class="ordinal">' + teeth['o'] + '</span> (' + teeth['d'] + ')</black><br>' +
                answerList +
                '</div>' +
                '</li>';
        }

        function reservation_success(formData, jqForm, options) {

            var sURL = unescape(window.location.pathname);

            var $content = $('#content');
            $content.empty();
            var $success_message = $('<div class="reservation success"><p class="message">Va multumim pentru timpul acordat! In cel mai scurt timp veti fi contactat de HEALTHIS  pentru confirmarea programarii completate.</p></div>');
            $content.html($success_message);
            setTimeout(function () {
                $success_message.fadeOut(300, function () {
                    window.location.href = sURL;
                });
            }, 3000);

        }


        function updateListText() {
            var text;
            var problemCount = $problemList.children().size();
            var problemList_height = $problemList_container.height();

            if (problemList_height > 440) {

                if ($('.scrollbar.vertical').not(':visible')) {
                    $('.scrollbar.vertical').show();
                }
            } else {
                $('.scrollbar.vertical').hide();
            }

            switch (problemCount) {
                case 0: text = '&nbsp;';
                    $emptyText.show();
                    $step1_indicator.removeClass('activ').addClass('inactiv');
                    break;
                case 1: text = 'Vous avez sélectionné une dent à problème';
                    $emptyText.hide();
                    $step1_indicator.removeClass('inactiv').addClass('activ');
                    break;
                default: text = 'Vous avez sélectionné   <strong>' + problemCount + '</strong> dents avec problèmes ';
                    $emptyText.hide();
                    $step1_indicator.removeClass('inactiv').addClass('activ');
            }
            $problemListText.html(text);
        }

        function scroller() {

            $("#step_one_selection").scrollbar({
                type: "scrollbar",
                height: 440,
                width: 250,
                scrollerEase: 7,
                dragVertical: true,
                dragHorizontal: false,
                barWidth: 3,
                draggerWidth: 12,
                draggerVerticalSize: "auto",
                draggerHorizontalSize: "auto",
                roundCorners: 5,
                distanceFromBar: 5,
                mouseWheel: true,
                mouseWheelOrientation: "vertical",
                mouseWheelSpeed: 13,
                draggerColor: "#666",
                draggerOverColor: "#99CC00",
                barColor: "#ADAFAE",
                barOverColor: "#ADAFAE"
            });
        }
    }

    // do some header stuff
    var headerAction = function () {
        setTimeout(function () {
            $('#home_featured').animate({ height: '150' }, 2000);
        }, 3000);
    }

    var formValidation = function () {
        $('#form_reservation').validationEngine();
    }

    var reloadCaptcha = function () {
        $('#captcha').bind('click', function () {
            var captchaSrc = window.location.protocol + '//' + window.location.hostname + '/assets/plugins/captcha/image.php';
            $(this).attr('src', captchaSrc + '?' + new Date());
        });
    }

    var registerAjaxLoad = function () {
        var $loading = $('#loading');
        var $content = $('#content');

        $loading.ajaxStart(function () {
            $content.fadeTo('fast', 0.5, function () {
                var offset = $('#content').offset();
                $loading.css('top', (offset.top + 100) + 'px').show();
            });
        }).ajaxStop(function () {
            $content.fadeTo('fast', 1, function () {
                $loading.hide();
            });
        });
    }

    var containedSticky = function () {
        //jQuery('').containedStickyScroll();
    }

    function step3() {
        var wantedSchedule = $('input[name="jour"]:checked').val();

        wantedSchedule == 'matines' ? $('.hour-interval .last-interval').parent().hide() : $('.hour-interval .first-interval').parent().hide();

        $('input[name="jour"]').on('change', function () {
            $('#intervals').val('');
            $('.hour-interval .first-interval, .hour-interval .last-interval').parent().show();
            $(this).val() == 'matines' ? $('.hour-interval .last-interval').parent().hide() : $('.hour-interval .first-interval').parent().hide();
        });
    };



    $(document).ready(function () {
        teethForm();
        step3();
        headerAction();
        formValidation('attach');
        reloadCaptcha();
        registerAjaxLoad();
        containedSticky();
    });
})(jQuery);