// Display calendar widget
$(function() {

    // Danish localization
    $.tools.dateinput.localize("da",  {
        months:        'januar,februar,marts,april,maj,juni,juli,august,september,oktober,november,december',
        shortDays:     'Søn,Man,Tirs,Ons,Tors,Fre,Lør',
    });

    var language = $("#calendar").attr("class");

    // initialize dateinput
    $("#datehelper").dateinput( {
        lang: language,
        firstDay: 1,
        value: 'Today',
        // closing is not possible
        onHide: function()  {
            return false; 
        },
    }).data("dateinput").show();

    // Remove absolute positioning
    $("#calroot").removeAttr("style");
    $("#calendar").replaceWith($("#calroot"));

    // Hide events and show on calendar navigation
    function showevents() {
        $(".teaser ol.vcalendar li").hide();
        var month = $("#caltitle").text();
        var events = $(".teaser ol.vcalendar li[title='" + month + "']");
        events.show();
        events.find("abbr").each(function() {
            var day = $(this).text().split('/')[0];
            $(".calweek a[href='#" + day + "'][class!='caloff']").addClass("hasevent");
        });
    };
    showevents();
    $('#calprev, #calnext').click(function() {
        showevents();
    });
});

