(function(w, $){
    $(function(){

        $('.brace-fancybox').fancybox();

        $('.brace-flickity').flickity({
            cellAlign:      'left',
            pageDots:       false,
            setGallerySize: false,
            wrapAround:     true
        });

        $('.brace-tooltip').tooltip();

        $('.brace-datepicker').datepicker({
            dateFormat: 'dd/mm/yy',
            changeMonth: true,
            changeYear: true,
            beforeShow: function(_, datepicker){
                console.log(datepicker.dpDiv.find('.ui-datepicker-month, .ui-datepicker-year').addClass('wwaoh'));
            }
        });
        
        $.fn.braceSocialShare = function(){
            return this.each(function(){
                
                var $a = $(this),
                    href = $a.prop('href'),
                    label = $a.data('share-label');

                $a.on('click', function(e){
                    e.preventDefault();

                    var width = 520,
                        height = 350,
                        x = ($(window).width() / 2) - (width / 2),
                        y = ($(window).height() / 2) - (height / 2);

                    window.open(href, 'sharer', 'top=' + y + ',left=' + x + ',toolbar=0,status=0,width=' + width + ',height=' + height);
                });
            });
        };

        $(function(){
            $('.brace-social-share').braceSocialShare();
        });
        
        var braceSlideIn = function($wrap){
            if(!$wrap || !$wrap.length){
                return;
            }
            
            this.SEL = {
                OPEN:  '.brace-slide-in-btn',
                CLOSE: '.brace-slide-close',
                INNER: '.brace-slide-in',

                CLOSED__CLASS: 'brace-slide-in-closed'
            };
            this.$el = {};

            this.$wrap = $wrap;
            this.init();
        };

        braceSlideIn.prototype = {
            init: function(){
                this.loadElems();
                this.sizeElems();
                this.attachListeners();

                this.isClosed = this.$wrap.hasClass(this.SEL.CLOSED__CLASS);

                this.triggerEvents();
            },
            toggle: function(){
                if(this.isClosed){
                    this.open();
                } else {
                    this.close();
                }
            },
            open: function(){
                this.$wrap.removeClass(this.SEL.CLOSED__CLASS);
                this.isClosed = false;
            },
            close: function(){
                this.$wrap.addClass(this.SEL.CLOSED__CLASS);
                this.isClosed = true;
            },
            loadElems: function(){
                this.$inner = this.$wrap.find(this.SEL.INNER);
                this.$open = this.$wrap.find(this.SEL.OPEN);
            },
            sizeElems: function(){
                this.$inner.width('');
                this.$wrap.width('');

                var width = this.$inner.outerWidth(),
                    btnHeight = this.$open.outerHeight();

                this.$wrap
                    .width(width + this.$open.outerHeight())
                    .css('padding-left', btnHeight);
            },
            affix: function(){
                var $w = $(window),
                    $affixedWith = $('h1').first(),
                    $adminBar = $('#wpadminbar');

                this.$wrap.removeClass('brace-slide-in-affix').css('top', '');

                if(!$affixedWith.length){
                    return;
                }
                
                if($affixedWith.offset().top - $w.scrollTop() >= this.$wrap.offset().top - $w.scrollTop()){
                    this.$wrap.addClass('brace-slide-in-affix')
                        .css('top', $affixedWith.offset().top - ($adminBar.length ? $adminBar.height() : 0));
                }
            },
            attachListeners: function(){
                this.$wrap.on('click', [this.SEL.OPEN, this.SEL.CLOSE].join(), $.proxy(this.triggerClick, this));
                $(window).on('scroll', $.proxy(this.affix, this));
                //$(window).on('resize', $.proxy(this.sizeElems, this));
            },
            triggerClick: function(e){
                this.toggle();
            },
            triggerEvents: function(){
                $(window).trigger('scroll');
            }
        };

        $.fn.braceSlideIn = function(){
            return new braceSlideIn(this);
        };

        $(function(){
            $('.brace-slide-in-wrap').braceSlideIn();
        });

        var braceFrame = function(selector){
            this.$w = $(w);
            this.$b = $(w.document.body);
            
            this.$frame = $(selector).first();

            if(!this.$frame.length){
                return;
            }

            this.SEL = {
                CURTAIN: '#brace-curtain',
                PAGE: '#page',
                NAVIGATION_OPEN_CLASS: 'brace-navigation-open',
                NAVIGATION: '#brace-navigation',
                NAVIGATION_TOGGLE: '.brace-navigation-toggle',
            };
            
            this.init();
        };

        
        braceFrame.prototype = {
            init: function(){
                this.initElems();
                this.attachListeners();
            },
            initElems: function(){
                this.$curtain = this.$frame.find(this.SEL.CURTAIN);
                this.$page = this.$frame.find(this.SEL.PAGE);
                this.$navigation = this.$frame.find(this.SEL.NAVIGATION);
                this.$navToggle = this.$frame.find(this.SEL.NAVIGATION_TOGGLE);
            },
            attachListeners: function(){
                this.$page.on('click', $.proxy(this.onPageClick, this));
                this.$navToggle.on('click', $.proxy(this.toggleNav, this));
            },
            toggleNav: function(e){
                var $target = $(e.target);
                
                if($target.closest(this.SEL.NAVIGATION_TOGGLE).length){
                    e.stopPropagation();
                }
    
                if(this.navOpen){
                    this.closeNav();
                } else {
                    this.openNav();
                }
            },
            onPageClick: function(){
                this.closeNav();
            },
            closeNav: function(){
                var _self = this;
    
                if(!_self.navOpen){
                    return;
                }
    
                _self.$b.removeClass(this.SEL.NAVIGATION_OPEN_CLASS);
                _self.navToggling = true;
    
                _self.$navigation.one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend', function(){
                    _self.navToggling = false;
                    _self.navOpen = false;
                });
                
            },
            openNav: function(){
                var _self = this;
    
                if(_self.navOpen){
                    return;
                }
    
                _self.$b.addClass(this.SEL.NAVIGATION_OPEN_CLASS);
                _self.navToggling = true;
    
                _self.$navigation.one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend', function(){
                    _self.navToggling = false;
                    _self.navOpen = true;
                });
                
            },
        };

        $(function(){
            new braceFrame('body'); 
        });
    });
}(window, jQuery));