

var Calendrier = {
    prevDate: '',
    nextDate: '',
    date: '',
    dateStr: '',
    cat_id: 0,
    isLoading: false,
    url: 'ajax/ef/calendrier.php',

    init: function() {
        this.applyTips();
        $('#calendrier-prev').click(function(){
            Calendrier.movePrev();
        });
        $('#calendrier-next').click(function(){
            Calendrier.moveNext();
        });
        $('#calendrier-today').click(function(){
            Calendrier.moveToday();
        });
        $('#calendrier-cat').change(function(){
            Calendrier.cat_id = $(this).val();
            Calendrier.refresh();
        });
    },

    print: function() {
        var win = window.open();
        var doc = win.document;
        doc.write('<html><head><title>Calendrier</title>');
        doc.write('<rel type="stylesheet" media="all" href="'+BASE_URL+'static/css/ef/style_calendrier.css" />');
        doc.write('<rel type="stylesheet" media="all" href="'+BASE_URL+'static/css/ef/style_calendrier_print.css" />');
        doc.write('</head><body>');
        doc.write('<div id="calendrier">');
        doc.write($('#calendrier').html());
        doc.write('</div></body></html>');

        //
        return;
        var $body = $doc.find('body'), $head = $doc.find('head');
        $('#calendrier').clone().appendTo($body);
        $head.append($('<rel type="stylesheet" media="all" href="'+BASE_URL+'static/css/style_calendrier.css" />'));
        $head.append($('<title>Calendrier</title>'));
    },

    refresh: function() {
        if (this.loading())
            return;
        jQuery.getJSON(this.url, 'date='+this.date+'&cat_id='+this.cat_id, function(data){
            Calendrier.onData(data);
        });
        this.loading(true);
    },

    moveToday: function() {
        if (this.loading())
            return;
        jQuery.getJSON(this.url, 'cat_id='+this.cat_id,  function(data){
            Calendrier.onData(data);
        });
        this.loading(true);
    },

    movePrev: function() {
        if (this.loading())
            return;
        jQuery.getJSON(this.url, 'date='+this.prevDate+'&cat_id='+this.cat_id,  function(data){
            Calendrier.onData(data);
        });
        this.loading(true);
    },

    moveTo: function(date) {
        if (this.loading())
            return;
        jQuery.getJSON(this.url, 'date='+date+'&cat_id='+this.cat_id,  function(data){
            Calendrier.onData(data);
        });
        this.loading(true);
    },

    moveNext: function() {
        if (this.loading())
            return;
        jQuery.getJSON(this.url, 'date='+this.nextDate+'&cat_id='+this.cat_id, function(data){
            Calendrier.onData(data);
        });
        this.loading(true);
    },

    loading: function(d) {
        if (typeof d != 'undefined')
            this.isLoading = !!d;
        return (this.isLoading);
    },

    onData: function(data) {
        this.loading(false);
        this.prevDate = data.prevDate;
        this.nextDate = data.nextDate;
        $('#calendrier-content').html(data.contents);
        this.applyTips();

        if (data.date && data.date != this.date) {
            this.date = data.date;
            this.dateStr = data.dateStr;

            if (0) {
                var $el = $('<span>'+this.dateStr+'<span>').hide();
                $('#calendrier-currentmonth span').slideUp('fast', function(){
                    $(this).remove();
                });
                $('#calendrier-currentmonth').append($el);
                $el.slideDown('fast');
            } else {
                // pas d'animation
                $('#calendrier-currentmonth').html(this.dateStr);
            }
        }
    },

    applyTips: function() {
        $('#calendrier .tip').cluetip({
            local: true,
            positionBy: 'bottomTop',
            clickThrough: true
        });
    }
}

