var calendar;

(function() {

// setup the calendar function
calendar = {
    // ==== init ==================================================================================
    // replace the calendar prev and next with ajaxed ones
    // ============================================================================================
    init: function()
    {
        // init vars
        var prev = $$('#previous a');
        var next = $$('#next a');
        
        // swap a tags with own elements
        if (prev[0]) this.swap_element(prev[0]);
        if (next[0]) this.swap_element(next[0]);
    },
    
    // ==== swap_element ==========================================================================
    // element - the element to turn off and replace with ajax
    // ============================================================================================
    swap_element: function(element)
    {
        // stop clicks on this element
        element.onclick = function() {return false;};
        
        // get the month offset
        var offset = element.firstChild.className;
        
        // set up the request for the new ajax
        element.request = new Request.HTML({url: '/whats_on_ajax.php?offset=' + offset, onSuccess: calendar.change_calendar});
        
        // set up a click on the a tag for ajaxing
        element.addEvent('click', calendar.change_month);
    },
    
    // ==== change_month ==========================================================================
    // e - the event caused by clicking on one of the change month arrows
    // ============================================================================================
    change_month: function(e)
    {
        // change the text to loading
        $('month').innerHTML = 'Loading...';
        $('previous').innerHTML = '';
        $('next').innerHTML = '';
        
        // send off the element request
        this.request.send();
    },
    
    // ==== change_calendar =======================================================================
    // html - the html of the new calendar to add
    // ============================================================================================
    change_calendar: function(html)
    {
        $('calendar').empty();
        $('calendar').adopt(html);
        calendar.init();
    }
};
})();