//compile 0a1a5a5d8ee6affa8ab79b4be4d87ea8
$(document).ready(function(){
  rcmail.env.event_imported = false;
  rcmail.env.event_previewed = new Array();
});

function calendar_icalattach(){
  this.save = function(mime_id, items){
    var ext = '&_items='+urlencode(items);
    c = 1;
    if(!items){
      try{
        c = rcmail.env.myevents.length;
      }
      catch(e){
        c = parent.rcmail.env.myevents.length;
      }
      ext = '';
    }
    if(confirm(rcmail.gettext('calendar.importconfirmation').replace('%s',c))){
      rcmail.http_post('plugin.saveical', '_uid='+rcmail.env.uid+'&_mbox='+urlencode(rcmail.env.mailbox)+'&_part='+urlencode(mime_id)+ext, true);
      rcmail.env.event_imported = true;
      setTimeout("calendar_icalattach.save_after()",1000);
    }
    return false;
  }
  
  this.save_after = function(){
    this.remove();
    try{
      parent.$('#upcoming').fullCalendar('refetchEvents');  
    }
    catch(e){
      $('#upcoming').fullCalendar('refetchEvents');
    }   
  }  
   
  this.preview = function(mydate){
    var date = new Date(mydate);
    var day = date.getDate();
    var month = date.getMonth();
    var year = date.getFullYear();
    try{
      var today = parent.$('#upcoming').fullCalendar('getDate');
    }
    catch(e){
      var today = $('#upcoming').fullCalendar('getDate');
    }
    if(today.getDate() != date || today.getMonth() != month || today.getFullYear() != year){
      try{
        parent.$('#upcoming').fullCalendar('gotoDate',year,month,day);
        parent.$('#upcoming').fullCalendar('refetchEvents');
      }
      catch(e){
        $('#upcoming').fullCalendar('gotoDate',year,month,day);
        $('#upcoming').fullCalendar('refetchEvents');
      }
      var date = new Date(mydate + (86400 * 1000));
      var day = date.getDate();
      var month = date.getMonth();
      var year = date.getFullYear();
      try{
        parent.$('#upcoming_1').fullCalendar('gotoDate',year,month,day);
      }
      catch(e){
        $('#upcoming_1').fullCalendar('gotoDate',year,month,day);
      }
      var date = new Date(mydate + (86400 * 1000 * 2));
      var day = date.getDate();
      var month = date.getMonth();
      var year = date.getFullYear();
      try{
        parent.$('#upcoming_2').fullCalendar('gotoDate',year,month,day);
      }
      catch(e){
        $('#upcoming_2').fullCalendar('gotoDate',year,month,day);
      }
    }
  }
    
  this.remove = function(){
    try{
      parent.rcmail.env.myevents = new Array();
      parent.$('#upcoming').fullCalendar('removeEvents','preview');
      parent.$('#upcoming-container').scrollTop(0);
    }
    catch(e){
      rcmail.env.myevents = new Array();
      $('#upcoming').fullCalendar('removeEvents','preview');
      $('#upcoming-container').scrollTop(0);    
    }
  }      
}
var calendar_icalattach = new calendar_icalattach();