/**
 * Created by Marek on 16.12.16.
 */
$(document).ready(function() {
    // HOME SLICK SLIDER
    $('#mainSlider').slick({
        infinite: true,
        slidesToShow: 1,
        slidesToScroll: 1,
        arrows: false,
        dots: false,
        fade: true,
        speed: 400,
        autoplaySpeed: 4000,
        autoplay: false,
        pauseOnHover: false
    });



    var win = $(window),
        navbar = $('#navbar'),
        className = 'navbar-scroll';

    win.on('scroll',function() {
        if (win.scrollTop() >= 40 ) {
            navbar.addClass(className);
        } else {
            navbar.removeClass(className);
        }
    });


    // CLICKABLE IMAGES, CLASS SA PRIDAVA V TINYMCE
    var tinyMceImages = $('.tinymceClickable');
    tinyMceImages.each(function() {
        $(this).wrap('<a href="'+$(this).attr('src')+'" data-fancybox=""></a>');
    });


    // MODAL INTRODUCTROY MESSAGE
    var introMessageSwitch = $('footer').attr('data-introductoryMessage');
    if($.cookie(COOKIE_PREFIX + '_modal_introductory_message') == null) {
        var im = [];
    } else {
        var im = $.parseJSON($.cookie(COOKIE_PREFIX + '_modal_introductory_message'));
        im = $.map(im, function(n) {
            return (parseInt(n));
        });
    }

    if(introMessageSwitch != 0) {
        $.post(ROOT_DIR + 'api/article/getIntroductoryMessage')
            .done(function(result) {
                if(IsJsonString(result)) {
                    var res = $.parseJSON(result);
                    //console.log('result: ' + res.id);
                    //console.log('im: ' + im);
                    if(res.id != null) {
                        //console.log('index: ' + $.inArray(res.id, im));
                        if($.inArray(parseInt(res.id), im) === -1) {
                            // console.log('Opening message: ' + res.id);
                            $('body').append(res.data);
                            new bootstrap.Modal(document.getElementById('modalIntroductoryMessage'), {}).show();
                            // $('#modalIntroductoryMessage').modal({backdrop: 'static', keyboard: false});
                        } else {
                            // console.log('Message ' + res.id + ' has been read.');
                        }
                    } else {
                        // console.log(res.data);
                    }
                } else {
                    // console.log('Result is not JSON.');
                }
            })
            .fail(function(xhr, status, error) {
                console.log('error ajax.fail (api/article/getIntroductoryMessage)');
                console.log('xhr: ' + xhr);
                console.log('status: ' + status);
                console.log('error: ' + error);
            });

        $('body').on('hidden.bs.modal', '#modalIntroductoryMessage', function(e) {
            var date = new Date();
            date.setTime(date.getTime() + (2 * 60 * 60 * 1000));

            var actual = parseInt($(this).attr('data-message'));

            if($.inArray(actual, im) === -1)
                im.push(actual);
            $.cookie(COOKIE_PREFIX + '_modal_introductory_message', JSON.stringify(im), { expires: date });
        });
    }



    // Firing resize event only when resizing is finished
    var resizeId;
    $(window).on('resize', function() {
        clearTimeout(resizeId);
        resizeId = setTimeout(doneResizing, 250);
    }).trigger('resize');
});


function doneResizing() {
    // Push footer to bottom
    var wH = $(window).height();
    var hfH = $('body > header').outerHeight(true) + $('body > footer').outerHeight(true);
    var mH = $('body > main').outerHeight(true);
    if((hfH + mH) < wH) {
        $('body > main').css({'min-height': (wH - hfH) + 'px'})
    }
    // \Push footer to bottom
}

function createFormMessage($text, $type) {
    if($type == 'success') {var messageColor = '#20b516';}
    if($type == 'info') {var messageColor = '#02C6EB';}
    if($type == 'warning') {var messageColor = '#f1cc10';}
    if($type == 'error') {var messageColor = '#e4061a';}
    $('.form-messages').css('background-color', messageColor).text($text).slideToggle();

    setTimeout(function(){
        $('.form-messages').slideToggle();
    }, 5000);
}