function init_month(an,mois){
    $('#block_activites').load("calendrier/index.php?action=display_month&year="+an+"&month="+mois+" .phpc-calendar", function(){
        // exécuté après le chargement du calendrier
        $('.phpc-calendar th:first-child').remove();
        $('.phpc-calendar th').each(function(){
            var day = $(this).text();
            switch(day){
                case 'Dimanche': day = 'D';
                    break;
                case 'Lundi': day = 'L';
                    break;
                case 'Mardi': day = 'M';
                    break;
                case 'Mercredi': day = 'M';
                    break;
                case 'Jeudi': day = 'J';
                    break;
                case 'Vendredi': day = 'V';
                    break;
                case 'Samedi': day = 'S';
                    break;
            }
            $(this).contents().replaceWith(day);
        });
        
        // gestion de la navigation entre les mois
        
        $('#block_activites caption').prepend('<a href="#" class="before">←</a>');
        $('#block_activites caption').append('<a href="#" class="after">→</a>');
        $('.phpc-add').remove();
        $('.phpc-calendar colgroup').remove();
        $('#block_activites .before').attr({
            'data-an':prev_year(mois,an),
            'data-mois':prev_month(mois)
        });
        $('#block_activites .after').attr({
            'data-an':next_year(mois,an),
            'data-mois':next_month(mois)
        });
        $('.before, .after').click(function(){
            an = parseFloat($(this).attr("data-an"));
            mois = parseFloat($(this).attr("data-mois"));
            init_month(an,mois);
            return false;
        });
        
        // gestion des hover
        
        $('.phpc-calendar td').each(function(){
            var evenement = $(this).children('ul').detach();
            $(evenement).addClass('tooltip');
            $(this).children('.phpc-date').append(evenement);
        });
        $('.phpc-date a').hover(function(){
            $(this).next('.tooltip').css({
                display: "block"
            })
        },function(){
            $(this).next('.tooltip').css({
                display: "none"
            })
        });
        $('.phpc-calendar .tooltip').prev('a').addClass('cal-active');
        
        // gestion des clicks et affichage de la pop-up
        $(".phpc-calendar td a").attr('rel','#overlay').overlay({
                                            onBeforeLoad: function(){
                                                $("#block_activites .phpc-future .cal-active, #block_activites .phpc-present .cal-active, #block_activites .phpc-past .cal-active").each(function(){  
                                                    $.ajax({ 
                                                        url: $(this).attr('href'),  
                                                        async:false, 
                                                        success: function(data){
                                                            $keep = $(data).children(".phpc-main");
                                                            $keep.children('thead').remove();
                                                            $keep.children('tbody').children('tr').each(function(){
                                                                $description = $(this).children("td:last-child").detach();
                                                                $description.attr("colspan","2");
                                                                $(this).after('<tr class="description"></tr>');
                                                                $(this).next("tr").append($description);
                                                            });
                                                            $keep.appendTo("#include-event");
                                                        }
                                                    });  
                                                });
                                                $("#block_activites .phpc-main").clone(true).prependTo("#include-cal");
                                            },
                                            onLoad: function(){
                                                $("#include-cal .tooltip").css("display","none");
                                                $("#include-cal .before, #include-cal .after").remove();
                                                $("#include-cal a").unbind("click").click(function(){return false;});
                                                $("#include-event a strong").unwrap();
                                            },
                                            onBeforeClose: function(){
                                                $("#include-cal").html("");
                                                $("#include-event").html("");
                                            },
                                            fixed: false
        });
        

    });
}

// Fonctions pour retourner le bon temps suivant

function prev_month(mois){
    if(mois == 1){
        return 12;
    }
    else{
        return parseFloat(mois)-1
    }
}
function next_month(mois){
    if(mois == 12){
        return 1;
    }
    else{
        return parseFloat(mois)+1
    }
}
function prev_year(mois,an){
    if(mois == 1){
        return an-1;
    }
    else{
        return an;
    }
}
function next_year(mois,an){
    if(mois == 12){
        return an+1;
    }
    else{
        return an;
    }
}

// À exécuter lors du chargement de la page

$(function(){
    
    var today = new Date();
    var an = today.getFullYear();
    var mois = today.getMonth()+1;
    
    init_month(an,mois);
})

