//compile 0a1a5a5d8ee6affa8ab79b4be4d87ea8
$(document).ready(function() {
  /* start loading */
  rcmail.gui_object('message', '#message');
  msgid = rcmail.set_busy(true,'loading');
  
  /* kill keep alive */
  rcmail.env.keep_alive = false;
  
  /* Window events */
  window.onunload = function(){
    opener.rcmail.env.calpopup = false;
  }
  
  /* print resize */
  $(window).resize(function(){
    var mwidth = parseInt($(window).width() / rcmail.env.cal_print_cols / 10) * 10;
    $('.print').each(function(){
      $(this).css('max-width', mwidth + 50 + 'px');
    });
  });

  /* set vars */
  rcmail.env.myevents = new Array();
  rcmail.env.myeventobjects = new Array();
  rcmail.env.calendar_print_curview = 'calendar';
    
  /* register commands */
  calendar_commands.init();
  
  /* get settings */
  rcmail.addEventListener('plugin.getSettings', getSettings);
  rcmail.http_post('plugin.getSettings', '');
  
  function getSettings(response) {
    rcmail.env.calsettings = response;
    rcmail.set_busy(false,'loading',msgid);
    var initial_date = new Date(queryString('_date') * 1000);
    var today = new Date();
    var year = today.getFullYear();
    var month = today.getMonth();
    var day = today.getDate();
    if(initial_date != 'false'){
      var ndate = $.fullCalendar.parseDate(new Date(queryString('_date') * 1000));
      var year = ndate.getFullYear();
      var month = ndate.getMonth();
      var day = ndate.getDate();
    }
    $('#calendar').fullCalendar({
      header: {
        left: '',
        center: 'title',
        right: ''
      }, 
      height : $(window).height() - 70,
      titleFormat: {
        month: response.settings['titleFormatMonth'],
        week: response.settings['titleFormatWeek'],
        day: response.settings['titleFormatDay']
      },      
      columnFormat: {
        month: response.settings['columnFormatMonth'],
        week: response.settings['columnFormatWeek'],
        day: response.settings['columnFormatDay']
      },    
      theme: response.settings['ui_theme_main'],
      editable: false,
      eventSources: calendar_common.eSources(response),
      //events: opener.$('#calendar').fullCalendar('clientEvents'),
      ignoreTimezone: false,
      date: day,
      month: month,
      year: year,
      monthNames : response.settings['months'],
      monthNamesShort : response.settings['months_short'],
      dayNames : response.settings['days'],
      dayNamesShort : response.settings['days_short'],
      firstDay : response.settings['first_day'],
      firstHour : response.settings['first_hour'],
      timeFormat: js_time_formats[rcmail.env.rc_time_format],
      axisFormat : js_time_formats[rcmail.env.rc_time_format],
      defaultView: queryString('_view'),
      allDayText: rcmail.gettext('all-day', 'calendar'),
      loading : function(isLoading) {
        if(isLoading) {
          msgid = rcmail.set_busy(true,'loading');
          rcmail.enable_command('plugin.calendar_do_print', false);
          rcmail.enable_command('plugin.calendar_toggle_view', false);
        }
        else {
          rcmail.set_busy(false,'loading', msgid);
          var mydate = new Date(queryString('_date') * 1000);
          $('#calendar').fullCalendar( 'gotoDate', $.fullCalendar.parseDate(mydate));
          rcmail.env.myevents = new Array();
          $(window).resize(function() {
            $('#print').width($(window).width()-20);
            $('#agendalist').width($(window).width()-20);
          });
          $('#caltoggleviewbut').trigger('click');
        }
      },
      eventRender: function(event, element, view) {
        rcmail.enable_command('plugin.calendar_do_print', true);
        rcmail.enable_command('plugin.calendar_toggle_view', true);
        if (event.description) {
          var mydescription = event.description;
          if(mydescription.length > 100)
            mydescription = mydescription.substring(0,100) + " ...";
          element.find('span.fc-event-title').after("<span class=\"fc-event-description\"><br />"+mydescription+"</span>");
        }
        if (event.className) {
          var disp = event.classNameDisp;
          if(!disp) disp = event.className;
          element.find('span.fc-event-title').after("<span class=\"fc-event-categories\"><br />"+disp+"</span>");
        }
        if(event.end && event.allDay != 1){
          var date = event.end;
          var mydate = date.format(js_time_formats[rcmail.env.rc_time_format]);
          element.find('.fc-event-title').html(" - " + mydate + " " + event.title);
        }
        else{
          calendar_common.modifyEvents(event, element, view, response);
        }
      }
    });
    $('#toolbar').show();
  }
});

  


