/**
 *  Cuban Council
 *  Footer JS
 *
 *  Dependencies
 *  - jQuery
 *
 */

(function($) {

    var isMobile = (navigator.userAgent.match(/(?:iPhone|iPad|iPod|Android)/i) != null);
    var footExpanded = false;
    var footExpandedClick = false;
    var footAnimating = false;
    var footContent = $('#cc-footer .footer-content');
    var footContact = $('#cc-footer .footer-contact');

    var CubanFooter = {

        init: function() {

            // reformat footer on mobile devices
            if (isMobile) {
                CubanFooter.mobileFormat();
            }

            // open footer (contact)
            $('.foot-contact').click(function(e) {

                e.preventDefault();
                if (isMobile) return;
                var targetContactHeight = Math.max(0, 71 - ($(document).height() - ($(window).height() + $(window).scrollTop())));

                if (!footExpandedClick) {
                    footExpandedClick = true;
                    footContact.css({ 'position': 'fixed', 'bottom': '18px', 'top':'auto', 'height': targetContactHeight + 'px' });
                    footContent.css({ 'position': 'fixed', 'bottom': targetContactHeight + 18 + 'px' });
                    footContact.animate({'height': '71px'}, 200);
                    footContent.animate({'bottom': '89px'}, 200);
                } else {
                    footExpandedClick = false;
                    footContact.animate({'height': targetContactHeight + 'px'}, 200, function() {
                        footContact.css({ 'position': 'absolute', 'bottom': 'auto', 'top': '-18px', 'height': '71px' });
                    });
                    footContent.animate({'bottom': targetContactHeight + 18 + 'px'}, 200, function() {
                        if (targetContactHeight > 0) {
                            footContent.css({ 'position': 'absolute', 'bottom': '89px' });
                        } else {
                            footContent.css({ 'position': 'fixed', 'bottom': '18px' });
                        }
                    });
                }

            });

            // more initialization
            CubanFooter.checkDocumentSize();
            CubanFooter.checkFooterExpanded();

        },

        mobileFormat: function() {
            $('#cc-footer').css('padding-bottom', '89px');
            $('#cc-footer .footer-botwood').css('position', 'absolute');
            footContact.css({ 'position': 'absolute', 'top':'0' });
            footContent.css({ 'position': 'absolute', 'bottom': '89px' });
        },

        checkFooterExpanded: function() {
            if(isMobile) return;
            if (!footExpandedClick && !footAnimating) {
                var scrollTopOffset = $(document).height() - $(window).height();
                if ($(window).scrollTop() >= scrollTopOffset - 71) {
                    if (!footExpanded) {
                        footContent.css({ 'position': 'absolute', 'bottom': '89px' });
                        footExpanded = true;
                    }
                } else {
                    if (footExpanded) {
                        footContent.css({ 'position': 'fixed', 'bottom': '18px' });
                    }
                    footExpanded = false;
                }
            }
        },

        checkDocumentSize: function() {
            $('#cc-wrap').css('height', 'auto');
            if ($('#cc-wrap').height() < $(window).height()) {
                if (isMobile) {
                    $('#cc-wrap').css('minHeight', $(window).height()-75);
                } else {
                    $('#cc-wrap').css('minHeight', $(window).height());
                }
            }
        }

    };

    window.CubanFooter = CubanFooter;

    $(document).ready(function() {

        CubanFooter.init();

        $(window).resize(function() {
            CubanFooter.checkDocumentSize();
            CubanFooter.checkFooterExpanded();
        });

        $(window).scroll(function() {
            CubanFooter.checkFooterExpanded();
        });

    });

})(jQuery);
