//compile 0a1a5a5d8ee6affa8ab79b4be4d87ea8
/* calendar initialization */
$(document).ready(function() {
 
  /* selected style */
  if(rcmail.env.skin == 'larry'){
    $('#calendar_button').children().attr('style', 'color: #3CF; background-color: #2c2c2c;');
  }
  
  /* reload calendar */
  rcmail.addEventListener('plugin.reloadCalendar', reloadCalendar);
  
  /* unlock GUI */
  rcmail.addEventListener('plugin.calendar_unlockGUI', unlockGUI);
  
  /* Error Saving */
  rcmail.addEventListener('plugin.calendar_errorGUI', errorGUI);
  
  /* search Event */
  rcmail.addEventListener('plugin.calendar_searchEvents', searchEvents);
  
  /* trigger Search */
  rcmail.addEventListener('plugin.calendar_triggerSearch', triggerSearch);
  
  /* Reload Page */
  rcmail.addEventListener('plugin.calendar_refresh', refreshCalendar);
  
  function refreshCalendar(response) {
    $('#calendar').fullCalendar('removeEvents');
    $('#calendar').fullCalendar('refetchEvents');
    $('#calendaroverlay').hide();
    $('#cal_boxtitle').html(response[0].content);
    return;
  }
  
  function errorGUI(msgid) {
    rcmail.set_busy(false,'loading',msgid);
    $('#calendaroverlay').hide();
    rcmail.display_message(rcmail.gettext('calendar.errorsaving'), 'error');
    $('#calendar').fullCalendar('refetchEvents');
  }
  
  function reloadCalendar(msgid) {
    $('#calendar').fullCalendar('removeEvents');
    $('#calendar').fullCalendar('refetchEvents');
    rcmail.set_busy(false,'loading',msgid);
    $('#calendaroverlay').hide();
  }
  
  function triggerSearch(){
    calendar_commands.triggerSearch();
  }
  
  function searchEvents(response) {
    if(response.rows != 0){
      rcmail.env.cal_search_results = response.rows;
      $("#calsearch_table").html(response.rows);
      $("#calsearchdialog").dialog('open');
      var i=-1;
      if(response.events){
        $("#calsearch_table tr td").each(function(){
          i++;
          response.events[i].start = response.events[i].start * 1000;
          if(response.events[i].end)
            response.events[i].end = response.events[i].end * 1000;
          elem = calendar_callbacks.qtip(response.events[i], $(this), 'search');
        });
      }
      else{
        while($("#calsearchfilter").val().indexOf(' ') > -1){
          $("#calsearchfilter").val($("#calsearchfilter").val().replace(' ','*'));
          calendar_commands.triggerSearch();
          break;
        }
      }
      var maxheigth = $('#prefs-box').height() - 5;
      var tr = $('#calsearch_table tr').height();
      var fudge = 55;
      if(rcmail.env.skin == 'larry'){
        fudge = 85;
      }
      var approxheight = Math.max(50 + ((i+1) * tr), fudge);
      $("#calsearchdialog").dialog('option','height',Math.min(maxheigth,approxheight));
      if(response.filters.length > 0){
        $('#cal_search_field_categories').attr('checked',false);
      }
      $("#calsearchfilter").focus();
    }
  }
  
  function unlockGUI(msgid) {
    rcmail.set_busy(false,'loading',msgid);
    $('#calendaroverlay').hide();
    calendar_commands.triggerSearch();
  }
  
  /* get settings */
  rcmail.addEventListener('plugin.getSettings', getSettings);
  
  var tz_info = jzTimezoneDetector.determine_timezone();
  response_text = tz_info.key;
  if(typeof(tz_info.timezone) == 'undefined'){
     response_text = 'undefined'
  }
  else{
    response_text = tz_info.timezone['olson_tz']; 
  }

  rcmail.http_post('plugin.getSettings', '_tzname='+response_text+"&_init=1");

  function getSettings(response) {
    /* remember settings */
    rcmail.env.calsettings = response;
    
    /* Replicaten */
    if(response.settings['backend'] == 'caldav'){
      var delay = 1000;
      if(rcmail.env.noreplication){
        delay = 100;
      }
      window.setTimeout("calendar_replicate.init(rcmail.env.calsettings, 'calendar');", delay);
    }
    
    /* Template objects */
    $('#cal_boxtitle').html(response.settings['boxtitle'].content);
    $('#calusers').html(response.settings['usersselector'].content);
    $('#calfilterscontainer').html(response.settings['filtersselector'].content);
    $('#categories_html').html(response.settings['categorieshtml']);

    /* init Navigation Datepicker */
    calendar_gui.initNavDatepicker(response);

    /* clock pickers */
    calendar_gui.initClockPickers(response);
    
    /* init commands */
    calendar_commands.init();
    
    // first hour
    var firstHour = response.settings['first_hour'];
    if(firstHour == -1)
      firstHour = new Date().getHours() - 1;
      
    // fullcalendar
    $('#calendar').fullCalendar({
      readyState: function(){
        if(response.settings.backend != 'caldav'){
          rcmail.env.cal_env = 'calendar';
          calendar_replicate.done('_' + Math.round(new Date().getTime() / 1000));
        }
        //find me: address issue to fullcalendar dev: let some space to click on all day sections
        //$('.fc-agenda-allday').height($('.fc-agenda-allday').height(""));
        //setTimeout("$('.fc-agenda-allday').height($('.fc-agenda-allday').height() + 3);",1000)
        calendar_gui.init(response);
        $('#toolbar').show();
        $('#jqdatepicker').datepicker('setDate', $('#calendar').fullCalendar('getDate'));
        $('#upcoming').fullCalendar('addEventSource',$('#calendar').fullCalendar('clientEvents'));
        $('#upcoming').fullCalendar('gotoDate', $('#calendar').fullCalendar('getDate'));
      },
      header: {
        left: '',
        center: '',
        right: ''
      },
      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'],
      selectable: true,
      unselectAuto: false,
      height: $('#prefs-box').height(),
      lazyFetching: false,
      editable: true,
      ignoreTimezone: false,
      eventSources: calendar_common.eSources(response, true),
      monthNames : response.settings['months'],
      monthNamesShort : response.settings['months_short'],
      dayNames : response.settings['days'],
      dayNamesShort : response.settings['days_short'],
      firstDay : response.settings['first_day'],
      firstHour : firstHour,
      slotMinutes : 60/response.settings['timeslots'],
      timeFormat: js_time_formats[rcmail.env.rc_time_format],
      axisFormat : js_time_formats[rcmail.env.rc_time_format],
      defaultView: calendar_gui.getView(response),
      allDayText: rcmail.gettext('all-day', 'calendar'),
      height: $('#calendar-container').height() - 20,
      viewDisplay: function(view){
        calendar_callbacks.viewDisplay(view, response);
      },
      loading : function(isLoading){
        rcmail.env.calendar_msgid = calendar_callbacks.loading(isLoading, rcmail.env.calendar_msgid);
        calendar_callbacks.loading(isLoading, rcmail.env.calendar_msgid);
      },
      eventRender: function(event, element, view){
        if(rcmail.env.calreadonly)
          event.editable = false;
        calendar_callbacks.eventRender(event, element, view, response);
      },
      eventDragStart: function(event, jsEvent, ui, view){
        calendar_callbacks.eventDragStart(event, jsEvent, ui, view, response);
      },
      eventDrop: function(event,dayDelta,minuteDelta,allDay,revertFunc){
        calendar_callbacks.eventDrop(event, dayDelta, minuteDelta, allDay, revertFunc, response);
      },
      eventResize : function(event, delta) {
        calendar_callbacks.eventResize(event, delta, response);
      },
      eventResizeStop : function(event, jsEvent, ui, view){
        calendar_callbacks.eventResizeStop(event, jsEvent, ui, view);
      },
      select: function(startDate, endDate, allDay, jsEvent, view) {
        if(!rcmail.env.calreadonly){
          calendar_callbacks.select(startDate, endDate, allDay, jsEvent, view, response);
        }
      },
      dayClick: function(date, allDay, jsEvent, view) {
        if(!rcmail.env.calreadonly){
          calendar_callbacks.dayClick(date, allDay, jsEvent, view, response);
        }
      },
      eventClick : function(event) {
        if(!rcmail.env.calreadonly){
          calendar_callbacks.eventClick(event, response);
        }
      }
    });
    
    if(response.settings['date']){
      calendar_commands.gotoDate(response.settings['date']);
    }
  }

  /* disable toolbar button */
  $("#calendar_button").attr('href','#');
});