$(document).ready(function() {

    // Change the "Closed" text
    $('.data-text').each(function(i){
        $(this).text($(this).attr('aria-label'));
    });

    var time_now = new Date();
    var t_year = time_now.getFullYear(),
        t_month = time_now.getMonth(),
        t_day = time_now.getDate(),
        t_weekday = time_now.getDay();

    var basic_date = moment().startOf("day").unix();
    function show_opening_hours() {
        var t_now = moment().unix();
        $('.opening-hours').each(function (el) {
            var $el = $(this),
                location_id = $el.data('location'),
                $btn = $('#oh-status-' + location_id),
                $ul = $('ul', $el)[0],
                $open_status = $('#for-' + location_id);

            $('li', $ul).each(function (i) {
                var $li = $(this),
                    weekday = $li.data("weekday");

                if (weekday === t_weekday) {
                    $btn.html($li.html());
                    $li.addClass('active');

                    if ($('.oh-hours', $li).length > 0) {
                        var open_text = 'CLOSED',
                            open_class = 'closed';
                        $('.oh-hours', $li).each(function (i) {
                            var $ohint = $(this);
                            var start = basic_date + parseInt($ohint.data('start')),
                                end = basic_date + parseInt($ohint.data('end'));


                            if (t_now >= start &&
                                t_now < end) {
                                open_text = 'OPEN';
                                open_class = 'open';
                            }
                        });
                        if($open_status.hasClass(open_class)) {

                        }
                        else {
                            if(open_class === "open") $open_status.removeClass("closed");
                            if(open_class === "closed") $open_status.removeClass("open");
                            $open_status.addClass(open_class).text(open_text);
                        }

                    }
                }

            });


        });
    }
    show_opening_hours();
    window.setInterval(show_opening_hours, 20000);

    $('.btn-call').each(function(i){
        var $btn = $(this);
        $btn.attr('href', $btn.data('href'));
    });

});