$(document).ready(function(){
   $('table.sortable').tablesorter(); 
   $('#calendar_id td').mouseenter(show_post_event);
   $('#calendar_id td').mouseleave(hide_post_event);
 });
 
 function show_post_event() {
    // adds a small link to post an event on a given date
    
    var href = location.pathname;
    
    href = href.replace('/sage/calendar/', '').replace('/', '');
    
    var months = ['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
    
    var thisid = this.id;
    var dateid = thisid.replace('calendar_id_cell_', '');
    var thisdate = dateid.split('-');
    var url = '/sage/post_event/?y=' + thisdate[0] + '&m=' + thisdate[1] + '&d=' + thisdate[2] + '&g=' + href;

    var newlink = $(document.createElement('a'));
    newlink.attr('id', thisid.replace('_cell_', '_innerdiv_'));
    newlink.attr('href', url);
    newlink.attr('title', 'Post a new event on '+months[thisdate[1]]+' '+thisdate[2]+', '+thisdate[0]);
    newlink.addClass('post_event_on_this_day');
    newlink.text('Post Here');
    
    $('#'+thisid+' div').append(newlink);
    
}

function hide_post_event() {
    // removes small link to post an event on a given date
    var thisid = this.id;
    var divid = thisid.replace('_cell_', '_innerdiv_');
        
    if ($('#' + divid + '').length > 0) {
        $('#' + divid + '').remove();
    }
}
