jQuery(document).ready(function()
{
    jQuery('div#sidebar_calendar').ajax_calendar();
});



jQuery.fn.ajax_calendar = function()
{
    var date = new Date();
    
    if (!jQuery(this).attr('id'))
    {
        jQuery(this).attr('id', 'calendar_' + date.getTime());
    }
    
    var id = jQuery(this).attr('id');
    
    jQuery(this).find('thead th a')
        .click(function()
        {
            var offset = jQuery('#' + id).offset();
            
            jQuery('#' + id + '_loader').remove();
            
            jQuery('<div></div>')
                .attr('id', id + '_loader')
                .addClass('loader')
                .css({
                    position: 'absolute',
                    backgroundImage: 'url("/midcom-static/midcom.helper.datamanager2/ajax-loading-black.gif")',
                    backgroundPosition: 'center center',
                    backgroundRepeat: 'no-repeat',
                    backgroundColor: '#000000',
                    width: (jQuery('#' + id).width() + 10) + 'px',
                    height: (jQuery('#' + id).height() + 10) + 'px',
                    marginLeft: '-5px',
                    marginTop: '-5px',
                    top: offset.top,
                    left: offset.left,
                    display: 'none',
                    color: '#ffffff'
                })
                .insertBefore('#' + id)
                .fadeIn('normal');
            
            jQuery('<span></span>')
                .addClass('loading')
                .css({
                    paddingTop: (((jQuery('#' + id).height() + 10) / 2) + 24) + 'px',
                    display: 'block',
                    textAlign: 'center',
                    color: '#ffffff'
                })
                .html('Loading...')
                .appendTo('#' + id + '_loader');
            
            jQuery('div#sidebar_calendar')
                .load(
                    // Location
                    jQuery(this).attr('href') + '?ajax',
                    // Parameters
                    {},
                    // Callback
                    function()
                    {
                        jQuery('#' + id).ajax_calendar();
                        jQuery('#' + id + '_loader').fadeOut('slow');
                    }
                );
            
            return false;
        });
    
    jQuery(this).find('tbody td.events')
        .mouseover(function()
        {
            jQuery(this).addClass('hover');
        })
        .mouseout(function()
        {
            jQuery(this).removeClass('hover');
        });
}


