
if(typeof arm.worktype == 'undefined') {
  arm.worktype = {};
}
arm.worktype.design_mode = function(t, target, division_id, profession_id){
  if($(t).hasClass('anywhere_icon__edit')){
    $(t).removeClass('anywhere_icon__edit');
    $(t).addClass('anywhere_icon__save');
    $(target).addClass('epe-arm-design_mode_off_on');
  } else {
    var checkboxes = [];
    $.each($(target).find('input:checked'), function(k, v){
      checkboxes.push($(v).val());
    });
    var sov = sOverlay($(target), 'more_index', $(target));
    $.post('/' + sf_app + '/work_type_division/save_all?division_id=' + division_id + '&profession_id=' + profession_id, {'work_type_division': checkboxes.join(';')}, function(){
      
      
      //sov.remove();
      $(t).addClass('anywhere_icon__edit');
      $(t).removeClass('anywhere_icon__save');
      $(target).removeClass('epe-arm-design_mode_off_on');
      callback_add_row_stuped('', sov);
    });
    
  }
}


if(typeof arm.worker == 'undefined') {
  arm.worker = {};
}


arm.worker.worker_only_course = function(type){
  var sov = sOverlay($('.for_calc_scroll'), 'more_index', $('.for_calc_scroll'));
  var _this = this;
  var is_new = type == 'new';
  $.get('/' + sf_app + '/worker_only_course/' + type, function(html){
    sov.addClass('no-bg');
    var ml = arm.modal_layer($('.ierch__layer__worker'), (type == 'new' ? 'Назначение курса' : 'Редактирование назначенного курса'), sov);
    $(ml).find('.ierch__layer__modal__wrapper').html($(html).find('.lui__scroller_wrapper'));
    $(ml).find('.lui__scroller_wrapper fieldset').after($(html).find('.sf_admin_actions'));
    
    
    var worker_id = $(ml).find('#worker_only_course_worker_id');
    worker_id.val($('.ierch__layer__worker').data('worker'));
    


    $(worker_id).css({
      'position': 'absolute',
      'visibility': 'hidden'
    });
    
    $(ml).css({
      'margin-left': -1 * Math.round($(ml).outerWidth() / 2) + 'px',
      'margin-top': -1 * Math.round($(ml).outerHeight() / 2) + ($('.ierch__layer__worker').scrollTop()) + 'px'
    });
    $(ml).find('.sf_admin_action_delete a').prop('onclick', '');
    $(ml).find('.sf_admin_action_delete a').click(function(event){
      event.preventDefault();
      if(confirm('Вы действительно хотитие отменить назначение?')){
        sov.removeClass('no-bg');
        $(ml).hide();
        $.ajax({
          url: $(this).attr('href'),
          type: 'POST',
          data: {'sf_method': 'delete'}
        }).done(function(resp){
          
          $('.ierch__layer__item__opened').last().click();
          
        });
      }
      return false;
    });
    
    $(ml).find('.sf_admin_action_save').click(function(){
      
      sov.removeClass('no-bg');

      var pseudo_form = $('<form action="/' + sf_app + '/worker_only_course' + (is_new ? '">' : '/' + type.replace('/edit', '') + '"><input type="hidden" name="sf_method" value="put" />') + '<input type="hidden" /></form>');
      pseudo_form.append($(ml));


      $.ajax({
        url: pseudo_form.prop('action'),
        type: 'POST',
        data: pseudo_form.serialize()
      }).done(function(resp){
         
         var erlis = $(resp).find('.error_list');
         if(erlis.length > 0){
           //TODO
         } else {
           sov.remove();
           $('.ierch__layer__item__opened').last().click();
         }
        
      });

      return false;
    });
    
    
    
  });
};

arm.worker.division_course = function(type){
  
  
  var sov = sOverlay($('.for_calc_scroll'), 'more_index', $('.for_calc_scroll'));
  var _this = this;
  var is_new = type == 'new';
  $.get('/' + sf_app + '/division_course/' + type, function(html){
    sov.addClass('no-bg');
    var ml = arm.modal_layer($('.ierch__layer__worker'), (type == 'new' ? 'Назначение курса' : 'Редактирование назначенного курса'), sov);
    $(ml).find('.ierch__layer__modal__wrapper').html($(html).find('.lui__scroller_wrapper'));
    
    var worker_id = $(ml).find('#worker_only_course_worker_id');
    worker_id.val($('.ierch__layer__worker').data('worker'));
    


    $(worker_id).css({
      'position': 'absolute',
      'visibility': 'hidden'
    });
    
    $(ml).css({
      'margin-left': -1 * Math.round($(ml).outerWidth() / 2) + 'px',
      'margin-top': -1 * Math.round($(ml).outerHeight() / 2) + ($('.ierch__layer__worker').scrollTop()) + 'px'
    });
    $(ml).find('.sf_admin_action_delete a').prop('onclick', '');
    $(ml).find('.sf_admin_action_delete a').click(function(event){
      event.preventDefault();
      if(confirm('Вы действительно хотитие отменить назначение?')){
        sov.removeClass('no-bg');
        $(ml).hide();
        $.ajax({
          url: $(this).attr('href'),
          type: 'POST',
          data: {'sf_method': 'delete'}
        }).done(function(resp){
          
          $('.ierch__layer__item__opened').last().click();
          
        });
      }
      return false;
    });
    
    $(ml).find('.sf_admin_action_save').click(function(){
      
      sov.removeClass('no-bg');

      var pseudo_form = $('<form action="/' + sf_app + '/worker_only_course' + (is_new ? '">' : '/' + type.replace('/edit', '') + '"><input type="hidden" name="sf_method" value="put" />') + '<input type="hidden" /></form>');
      pseudo_form.append($(ml));


      $.ajax({
        url: pseudo_form.prop('action'),
        type: 'POST',
        data: pseudo_form.serialize()
      }).done(function(resp){
         
         var erlis = $(resp).find('.error_list');
         if(erlis.length > 0){
           //TODO
         } else {
           sov.remove();
           $('.ierch__layer__item__opened').last().click();
         }
        
      });

      return false;
    });
    
    
    
  });
};



if(typeof arm.bid == 'undefined') {
  arm.bid = {};
}

arm.bid.check_virtual = function(checkbox){
  if($(checkbox).is(':checked')){
    $('#' + $(checkbox).val()).removeAttr('disabled');
  } else {
    $('#' + $(checkbox).val()).attr('disabled', 'disabled');
  }
}
arm.bid.change_date = function(ref, course_type){
  var url = $.url($('.ierch__layer__shadowed__wrapper form').data('r'));
  var getvars = {};

  $.each($('.arm-widget_date_range select,.arm-widget_date_range input[type="text"],.arm-widget_date_range input[type="hidden"],.arm-widget_date_range input:checked'), function(k, v){
    getvars[$(v).attr('name')] = $(v).val();
  });
  

  
  if(ref == 'p'){
    var typess = '';
    
    $.each(getvars, function(k, v){
      typess += '&' + k + '=' + v;
    });
    
    $('.arm_p2_right_panel__b__cont').html('');
    $('.arm_p2_right_panel__b__cont').css({
      'background-image' : 'url(/i/arm/282-p.gif)',
      'background-repeat' : 'no-repeat',
      'background-position' : 'center center'
    });
    $.get(sf_prefix + '/report/learning/' + current_division + '?ref=p' + typess, function(html){
      $('.arm_p2_right_panel__b__cont').css({'background-image' : 'url(/i/n.gif)'});
      $('.arm_p2_right_panel__b__cont').html(html);
    });
  } else {
    var o = sOverlay($('.ierch__layer__shadowed__wrapper'), '')
    $.post(url.attr('path'), getvars, function(html){
      $('.ierch__layer__shadowed__wrapper').html(html);
      o.remove();
    });
  }

}

arm.bid.calc_selected = function(){
  var summ = 0;
  $('.worker_checkbox:checked').each(function(k, checkbox){
    summ += $(checkbox).data('price');
  })
  $('.epe-arm-bid_create_item__cost__val_selected').text(number_format(summ, 0, '', ' '));
  
}
arm.bid.calc_summ_total = function(){
  var summ = 0;
  $('.epe-arm-bid_create_item__cost__val').each(function(k, v){
    if($(v).text() != '?'){
      summ += parseInt($(v).text().replace(/ /g, ''));
    }
    if($(v).text() == '0'){
      $(v).parent().addClass('hidden_element_width_pseudo');
    }
  });
  if(summ == 0){
    $('.epe-arm-bid_create_item__cost__val_total').parent().addClass('hidden_element_width_pseudo');
  }
  $('.epe-arm-bid_create_item__cost__val_total').text(number_format(summ, 0, '', ' '));
}

function check(elem, s){
  if($(elem).length > 0){
    $(elem).prop('checked' , s);
  }
  if($('.worker_checkbox:checked, .course_checkbox:checked').length > 0){
    $('#bid_create_td').css('visibility', 'visible');
  }else{
    $('#bid_create_td').css('visibility', 'hidden');
  }
  arm.bid.calc_selected();
};

function check_worker(worker){
  var wo = $(worker);
  $('.worker_checkbox__' + wo.data('wid')).each(function(k, v){
    if($(v).prop('id') != wo.prop('id')){
      if(wo.is(':checked') && $(v).data('deadline') != wo.data('deadline')){
        $(v).prop('disabled', true);
      } else {
        $(v).prop('disabled', false);
      }
    }
  });
}

$(document).on('mousedown', '.course_checkbox', function(){
  $('html').addClass('cursor_wait');
});
$(document).on('click', '.course_checkbox', function(){
  
  $(this).parent().parent().find('.worker_checkbox:enabled').each(function(k, v){
    $(v).click();
  });
  $('html').removeClass('cursor_wait');
  /*
  var state = $(this).is(':checked') ? 'true' : false;
          p = $(this).parents('.standart__table');

  check($(this).parent().parent().find('.worker_checkbox:enabled'), state);
  check(p.find('.course_all_checkbox'), p.find('.worker_checkbox:checked').length ? 'true' : false);
  */

});
$(document).on('click', '.worker_checkbox:enabled', function(){
  check_worker($(this));
  var state = false,
      p = $(this).parents('.standart__table');

  state = $(this).parents('.standart__table_tr').find('.worker_checkbox:enabled').is(':checked') ? 'true' : false;
  check($(this).parents('.standart__table_tr').find('.course_checkbox'), state);
  check(p.find('.course_all_checkbox'), p.find('.course_checkbox:checked').length ? 'true' : false);
});