/*This file is loaded at the end of the page body */
$(document).foundation({
    offcanvas: {
        // Sets method in which offcanvas opens.
        // [ move | overlap_single | overlap ]
        open_method: 'move',
        close_on_click: true
    },
    reveal: {
        animation: 'fadeAndPop',
        animation_speed: 250,
        close_on_background_click: true,
        close_on_esc: true,
        dismiss_modal_class: 'close-reveal-modal',
        bg_class: 'reveal-modal-bg',
        open: function (e) {
            
            //var modal = $(this);

            //Foundation.libs.abide.validate(modal.find('input, select, textarea'), { type: '' })
            
            
            // Find the child form
            //var abideForm = $(modal).find('form');
            //// Clear the error classes on divs and labels
            //$(abideForm).children('div').removeClass('error');
            //$(abideForm).children('label').removeClass('error');
            //// Reset form abide validation
            //$(abideForm)[0].reset();


            //var model = $(this);
            //modal.find("div").removeClass("error");
            //modal.find("label").removeClass("error");
        },
        opened: function () {
            
        },
        close: function () { },
        closed: function () { $('html').attr('style', 'overflow:auto;') },
        bg: $('.reveal-modal-bg'),
        css: {
            open: {
                'opacity': 0,
                'visibility': 'visible',
                'display': 'block'
            },
            close: {
                'opacity': 1,
                'visibility': 'hidden',
                'display': 'none'
            }
        }
    },
    equalizer: {
        equalize_on_stack: false
    },
    abide: {
        live_validate: true,
        focus_on_invalid: true,
        error_labels: true,
        validators: {
            diceRoll: function(el, required, parent) {
                var possibilities = [true, false];
                return possibilities[Math.round(Math.random())];
            },
            isAllowed: function (el, required, parent) {                
                var possibilities = ['a@zurb.com', 'b.zurb.com'];
                return possibilities.indexOf(el.value) > -1;
            },
            intRange: function (el, required, parent) {
                var min = parseInt(el.getAttribute("data-minvalue"));
                var max = parseInt(el.getAttribute("data-maxvalue"))
                var val = parseInt(el.value);
                if (isNaN(min) || isNaN(max) || isNaN(val)) return false;
                return ((val >= min) && (val <= max));
            }
        }
      
    }, accordion: {
        // specify the class used for active (or open) accordion panels
        active_class: 'active',
        // allow multiple accordion panels to be active at the same time
        multi_expand: true,
        // allow accordion panels to be closed by clicking on their headers
        // setting to false only closes accordion panels when another is opened
        toggleable: true
    }
});

var menu = $('.left-off-canvas-menu');

$(window).smartresize(function () {
    menu.height($(this).height());
});

// Initialize height
$(document).trigger('resize');

$(document)
    .on('open.fndtn.offcanvas', '[data-offcanvas]', function () {
        $('html').css('overflow', 'hidden');
    })
    .on('close.fndtn.offcanvas', '[data-offcanvas]', function () {
        $('html').css('overflow', 'auto');
    })


// Fix for drop downs on small screen
Foundation.libs.dropdown.css = function (dropdown, target) {

    var left_offset = Math.max((target.width() - dropdown.width()) / 2, 8);

    this.clear_idx();
    var settings = target.data(this.attr_name(true) + '-init') || this.settings;
    this.style(dropdown, target, settings);

    $(dropdown)[0].style.bottom = "4em"
    $(dropdown)[0].style.top = "auto"

    var marginOffset = 10;

    var difference = (dropdown.position().left + dropdown.width() + marginOffset) - $(document).width();
    if (difference > 7) {

        $(dropdown)[0].style.left = "auto"
        $(dropdown)[0].style.right = "20px";
        $(dropdown).addClass("right-offset");

    }
    return dropdown;
}

$(document).on('opened', '[data-reveal]', function () {
    $(this).appendTo($("form"));
});
