//compile 0a1a5a5d8ee6affa8ab79b4be4d87ea8
// find me: make this as class to avoid conflicts

function show_caldav_dialog(category, elem){
  var $dialogContent = $("#caldav_dialog");
  if($dialogContent.dialog('isOpen') === true)
    return;
  var buttons = {};
  buttons[rcmail.gettext('cancel', 'calendar')] = function() {
    $dialogContent.dialog("close");
    elem.checked = false;
  }
  buttons[rcmail.gettext('remove', 'calendar')] = function() {
    $('#caldav_category').val(category);
    // post remove request
    rcmail.http_post('plugin.calendar_removeCalDAV', $('#caldav_form').serialize());
    $dialogContent.dialog("destroy");
    $dialogContent.hide();
    $('#edit_' + category).attr('style','visibility:hidden');
    $('#category_handler_' + category).attr('class', '');
    $('#category_handler_' + category).attr('onclick', 'removeRow(this.parentNode.parentNode)');
    $('#category_handler_' + category).attr('title', rcmail.gettext('remove_category','calendar'));
    elem.checked = false;
  }
  buttons[rcmail.gettext('save', 'calendar')] = function() {
    $('#caldav_category').val(category);
    // post save request
    if($('#caldav_user').val() != '' && $('#caldav_password').val() != '' && $('#caldav_url').val() != ''){
      rcmail.http_post('plugin.calendar_saveCalDAV', $('#caldav_form').serialize());
      $dialogContent.dialog("destroy");
      $dialogContent.hide();
      $('#edit_' + category).attr('style','visibility:visible');
      $('#category_handler_' + category).attr('class', 'protected_category');
      $('#category_handler_' + category).attr('onclick', 'calendar_toggle_caldav(this, \'' + category + '\')');
      $('#category_handler_' + category).attr('title', rcmail.gettext('unlink_caldav','calendar'));
      elem.checked = false;
    }
    else{
      $('#caldav_user').focus();
    }
  }
  $dialogContent.dialog({
       modal: false,
       width: 600,
       position: 'center',
       title: category,
       close: function() {
         $dialogContent.dialog("destroy");
         $dialogContent.hide();
         elem.checked = false;
       },
       buttons: buttons
  }).hide();
}

function calendar_toggle_caldav(elem, id){
  if(elem.value == 'X'){
     elem = document.getElementById('dialog_handler_' + id);
  }
  // fetch properties and run 'show_caldav_dialog' as callback
  rcmail.http_post('plugin.calendar_getCalDAVs', '_category=' + id);
  show_caldav_dialog(id, elem);
}

function calendar_getCalDAVs(response){
  var c = 0;
  $('.edit_caldav').each(function(){
    if($(this).attr('style') == 'visibility:visible'){
      c++;
    }
  });
  if(c >= response.max_caldavs){
    $('input:radio').each(function(){
      $(this).attr('disabled',true);
    });
  }
  else{
    $('input:radio').each(function(){
      $(this).attr('disabled',false);
    });
  }
  calendar_categories_gui(response);
}

function calendar_categories_gui(response){
  // set properties in dialog form and show dialog content
  $('#caldav_user').val(response.user);
  if(response.pass == 'ENCRYPTED' || response.pass == 'SESSION' || response.pass == 'GOOGLE'){
    $('#caldav_password').val(response.pass);
    $('#caldav_password').attr('title', rcmail.gettext('passwordisset','calendar'));
  }
  else{
    $('#caldav_password').val('');
    $('#caldav_password').attr('title', rcmail.gettext('passwordisnotset','calendar'));
  }
  if(response.url){
    $('#caldav_url').val(response.url);
  }
  else{
    $('#caldav_url').val(response.cat);
  }
  if(response.saved){
    $('#edit_' + response.category).attr('style','visibility:visible');
  }
  else{
    $('#edit_' + response.category).attr('style','visibility:hidden');
  }
  if(response.cal_dont_save_passwords){
    $('#caldav_password').attr('readonly', true);
  }
  if(response.extr == 'external' || response.extr === true){
    $('#caldav_extr').val('external');
  }
  else{
    $('#caldav_extr').val('internal');
  }
  if(response.auth == 'detect')
    $('#caldav_auth').val('detect');
  else
    $('#caldav_auth').val('basic');
  if(response.show)
    $("#caldav_dialog").show();
}

function addRowCategories(size) 
{ 
  var tbl = document.getElementsByTagName('table')[0];

  var rows_count = tbl.rows.length;
  var text = '&nbsp;';
  var category = '<input type="text" name="_categories[]" size="' + size + '" />';
  var color = '<input type="text" name="_colors[]" size="6" class="color" value="ffffff" />';   
  var remove= '<input type="button" value="X" onclick="removeRow(this.parentNode.parentNode)" title="' + rcmail.gettext('remove_category', 'calendar') + '" />'; 
   
  try { 
    var newRow = tbl.insertRow(rows_count); 
    var newCell = newRow.insertCell(0); 
    newCell.innerHTML = text;
    newCell.className = 'title';
    var newCell = newRow.insertCell(1); 
    newCell.innerHTML = remove + "&nbsp;" + category + "&nbsp;" + color;
    jscolor.init(); 
  }
  catch (ex) { 
 
  }
  jscolor_removeHexStrings();
}

function addRowCalFeeds(size) 
{ 
  var tbl = document.getElementsByTagName('table')[0];
  var rows_count = tbl.rows.length;
  var text = '&nbsp;';
  var feeds = '<input type="text" name="_calendarfeeds[]" size="' + size + '" />';
  var selector = '<select name="_feedscategories[]">';
  for (var category in categories) {
    selector += '<option value="' + category + '">' + category + '</option>';
  }
  selector += '</select>';
  var remove= '<input type="button" value="X" onclick="removeRow(this.parentNode.parentNode)" title="' + rcmail.gettext('remove_feed', 'calendar') + '" />'; 
   
  try { 
    var newRow = tbl.insertRow(rows_count); 
    var newCell = newRow.insertCell(0); 
    newCell.innerHTML = text;
    newCell.className = 'title'; 
    var newCell = newRow.insertCell(1); 
    newCell.innerHTML = remove + "&nbsp;" + feeds + "&nbsp;" + selector;
    jscolor.init(); 
  }
  catch (ex) { 
 
  }
}  

function removeRow(row) 
{ 
  var tbl = document.getElementsByTagName('table')[0];
  try { 
    tbl.deleteRow(row.rowIndex);
    document.forms.form.submit();
  }
  catch (ex) { 

  } 
}

$(document).ready(function() {
  rcmail.addEventListener('plugin.calendar_getCalDAVs', calendar_getCalDAVs);
});