
var p51labs_calendar_events;

(function($)
{
  $(document).ready(function() 
  {
    $('#fullcalendar').fullCalendar({
      eventSources: p51labs_calendar_events
      ,eventRender: function(event, element, view)
      {
        if (typeof p51labs_calendar_events_data[event.title] != 'undefined')
        {
          var content = $('#' + p51labs_calendar_events_data[event.title]).html();
          var c = content.split("\n");
          content = c[2];
        }
        else
        {
          var c = event.description.split("\n");
          var d = '';
          var e = c[2].split(' ');
          for (var i = 0; i < e.length; i++)
          {
            if (e[i].indexOf('http') === -1 && e[i].substr(0, 1) != '\/')
            {
              d += e[i] + ' ';
            }
            else
            {
              $('a[href="' + event.url + '"]').attr('href', e[i]);
            }
          }
          var content = '<h3>' + event.title + '</h3>' + d;
          
          if (c[3])
          {
            content += c[3];
          }
        }
                
        element.qtip({ 
          content: content
          ,style: { 
            padding: 5
            ,color: 'black'
            ,border: {
              width: 1
              ,color: '#eeeeee'
            }
          }
          ,position: {
            corner: {
              target: 'bottomleft'
              ,tooltip: 'topLeft'
            }
          }
        });
      }
    });
    
    $('.calendar-list-view').click(function(event)
    {
      event.preventDefault();
      
      $('#calendar-grid-view').hide();
      $('#calendar-list-view').show();
    });
    
    $('.calendar-grid-view').click(function(event)
    {
      event.preventDefault();
      
      $('#calendar-grid-view').show();
      $('#calendar-list-view').hide();
    });
    
    $('.calendar-details-link').click(function(event)
    {
      event.preventDefault();
      
      var title = $(this).html();
      
      $(this).html(title == 'Show Details' ? 'Hide Details' : 'Show Details');
      
      $(this).parent().next('div').slideToggle('fast');
    });
  });
})(jQuery);

