/* Minification failed. Returning unminified contents.
(1,1): run-time error CSS1019: Unexpected token, found '$'
(1,2): run-time error CSS1019: Unexpected token, found '('
(1,12): run-time error CSS1031: Expected selector, found '('
(1,12): run-time error CSS1025: Expected comma or open brace, found '('
(164,2): run-time error CSS1019: Unexpected token, found ')'
 */
$(function () {
    
    // Date editor 
    $(".js-dateeditor").on('change', function () {
        var hiddenDateId = $(this).data("dateid");
        var date = new Date($('#' + hiddenDateId + '_Year').val(), $('#' + hiddenDateId + '_Month').val(), $('#' + hiddenDateId + '_Day').val());
        //console.log('date : ', date);
        //console.log('dateLocal : ', date.toLocaleDateString());
        $('#' + hiddenDateId).val(date.toJSON());
    });

    // Sticky sidebar
    var stickySidebar = $('.js-sticky');

    if (stickySidebar.length > 0) {
        var stickyHeight = stickySidebar.height(),
            sidebarTop = stickySidebar.offset().top;
    }
    

    // on scroll move the sidebar
    $(window).scroll(function () {
        if (stickySidebar.length > 0) {
            var scrollTop = $(window).scrollTop();

            if (sidebarTop < scrollTop) {
                stickySidebar.css('top', scrollTop - sidebarTop);

                // stop the sticky sidebar at the footer to avoid overlapping
                var sidebarBottom = stickySidebar.offset().top + stickyHeight,
                    stickyStop = $('.js-stickycontainer').offset().top + $('.js-stickycontainer').height();
                if (stickyStop < sidebarBottom) {
                    var stopPosition = $('.js-stickycontainer').height() - stickyHeight;
                    stickySidebar.css('top', stopPosition);
                }
            }
            else {
                stickySidebar.css('top', '0');
            }
        }
    });

    $(window).resize(function () {
        if (stickySidebar.length > 0) {
            stickyHeight = stickySidebar.height();
        }
    });

    // Date format to jj-mm-yyyy
    if ($.validator) {
        $.validator.addMethod('date', function (value, element) {
            var d = new Date();
            return this.optional(element) || !/Invalid|NaN/.test(new Date(d.toLocaleDateString(value)));
        });
    }
    
    
    $('.js-richselect').foundationSelect();

    $(document).foundation();

    

    // js-clickback
    $(".js-clickback").bind("click", function (e) {
        window.location.href = document.referrer;
        return false;
    });

    $(".card-speciality").bind("click", function (e) {
        $(".card-speciality").removeClass("active");
        $(this).addClass("active");
        //$("#speciality-bg").attr("data-speciality", "");
        
        var expertisearea = $(this).data("expertisearea");
        //$("#speciality-bg").attr("data-speciality", expertisearea);
        var prevHeight = $(".expertisearea.shown").height() || 0;
        prevHeight += 20;
        $(".expertisearea.shown").removeClass('shown');
        var $elem = $(".expertisearea." + expertisearea);
        $elem.css('height', 'auto');
        var height = $elem.height() + 20;
        $elem.css('height', prevHeight + 'px');
        $elem.css('opacity', '0');
        $(".expertisearea." + expertisearea).addClass('shown').animate({ opacity: 1, height: height }, 200);
    });
    
    // First visit on osa => show popup
    if (!Cookies.get('osa-firstvisit')) {
        $("#modal-beta-disclaimer").foundation('reveal', 'open');
        $(".js-close").on('click', function () {
            $("#modal-beta-disclaimer").foundation('reveal', 'close');
        });
        Cookies.set('osa-firstvisit', 'true', {expires: 365*10});
    }

    // Show cookies disclaimer
    if (!Cookies.get('osa-cookies')) {
        $("#js-cookies-disclaimer").addClass('shown');
    }
    $("#js-cookies-disclaimer-accepts").on('click', function () {
        $("#js-cookies-disclaimer").removeClass('shown');
        Cookies.set('osa-cookies', 'true', { expires: 365 * 10 });
    });

    // Dates
    $('.osa-datepicker').fdatepicker({
        format: 'dd-mm-yyyy',
        disableDblClickSelection: true,
        startView: 'year',
        language: 'fr'
    });

    // Filter law topics on search page
    var filterLawTopics = function () {
        var lawField = parseInt($(".js-lawfield-select").val());
        var $customDropdown = $(".js-lawfield-subselect").next(".custom-dropdown-area");
        console.log('lawField ', lawField);
        if (!lawField) {
            $(".js-lawfield-column").hide('fast');
            $(".js-lawmain-column").addClass("large-offset-3");
        } else {
            $(".js-lawmain-column").removeClass("large-offset-3");
            $(".js-lawfield-column").show('fast');
            
        }
        $(".js-lawfield-subselect option").map(function (element) {
            var $element = $(this),
                childrenLawField = $element.data('lawfield'),
                chilrenValue = $element.val();
            var customElement = $customDropdown.find("li.value_" + chilrenValue);
            console.log(childrenLawField, 'custom : ', customElement);
            if (childrenLawField === lawField) {
                customElement.show();
            } else {
                customElement.hide();
            }
        });
    };

    // Auto submit on home
    //$(".js-homesearch select").on('change', function () {
    //    $(this).parents("form").submit();
    //});


    // Search lawfield and subselect
    $(".js-lawfield-select").on('change', function () {
        $(".js-lawfield-subselect option:checked").removeAttr("selected");
        var toReset = $(".js-lawfield-subselect").next(".custom-dropdown-area");
        resetCustomDropdown(toReset);
        filterLawTopics();
    });
    filterLawTopics();

    //$("[data-val-required]").prev("label").after("<div class='input-required-mark'></div>");
    //$("[data-val-required]").on('focus', function(){
       
    //});
    //$("[data-val-required]").on('blur', function () {
    //    var next = $(this).next(".input-required-mark");
    //    if (next) next.remove();
    //});
});

