// globale pour pouvoir l'appeller après
bind_splittedcheckboxes = function()
{
  // première liste
  jQuery('div.splittedcheckboxes-first input').live((jQuery.browser.msie ? "click" : "change"), function(event)
  {
    var real_id = jQuery(this).attr('id').replace(/^splittedcheckboxes_/, '');
    jQuery('div.splittedcheckboxes-secondary input#'+real_id).attr('checked', jQuery(this).attr('checked')).trigger('update-style');

    jQuery(this).trigger('update-style');
    // Formulaires ajax
    jQuery(this).parents('.mappable-form, #filter-frame-form').trigger('update');
  });

  // liste complète
  jQuery('div.splittedcheckboxes-secondary input').live((jQuery.browser.msie ? "click" : "change"), function(event)
  {
    var fake_id = 'splittedcheckboxes_'+jQuery(this).attr('id');
    jQuery('div.splittedcheckboxes-first input#'+fake_id).attr('checked', jQuery(this).attr('checked')).trigger('update-style');

    jQuery(this).trigger('update-style');
  });

  // change le style de la case cochée
  jQuery('div.splittedcheckboxes input').bind('update-style', function(event)
  {
    if (jQuery(this).attr('checked'))
    {
      jQuery(this).addClass('splittedchecked');
      jQuery("label[for=" + jQuery(this).attr("id") + "]").addClass('splittedchecked');
    }
    else
    {
      jQuery(this).removeClass('splittedchecked');
      jQuery("label[for=" + jQuery(this).attr("id") + "]").removeClass('splittedchecked');
    }
  }).trigger('update-style');

  var updateSplittedCheckboxesForm = function(hash)
  {
    // Formulaires ajax
    jQuery('.mappable-form, #filter-frame-form').trigger('update');
    hash.w.hide();
    hash.o.remove();
    hash.w.removeClass('active-popup');
  }
  jQuery('div.splittedcheckboxes-secondary').jqm({onHide: updateSplittedCheckboxesForm});

  // ouvre la liste complète
  jQuery('a.morecheckboxes').bind('click', function(event)
  {
    event.preventDefault();
    var popup = jQuery(this).parents('div.splittedcheckboxes').find('div.splittedcheckboxes-secondary');
    popup.jqmShow();
    popup.addClass('active-popup');
  });

  // ferme la liste complète
  jQuery('div.splittedcheckboxes-secondary button.jqmClose').live('click', function(event)
  {
    event.preventDefault();
    jQuery(this).parents('div.splittedcheckboxes').find('div.splittedcheckboxes-secondary').jqmHide();
  })

  // sélectionne/déselectionne tout
  jQuery('div.active-popup a.allcheckboxes').live('click', function(event)
  {
    event.preventDefault();
    var inputs = jQuery(this).parents('.splittedcheckboxes-secondary').find('input');
    var target = jQuery(event.target);

    if (target.data('selected'))
    {
      inputs.map(function(i, input)
      {
        jQuery(input).removeAttr('checked').trigger('update-style');
        target.data('selected', false);
      });
    }
    else
    {
      inputs.map(function(i, input)
      {
        jQuery(input).attr('checked', 'checked').trigger('update-style');
        target.data('selected', true);
      });
    }
  });
}

jQuery(document).ready(function()
{
  bind_splittedcheckboxes();

  jQuery('form a.morecheckboxes').bind('click', function(event)
  {
    event.preventDefault();
    var target = jQuery(event.target);
    var popup = target.parent().find('div.splittedcheckboxes-secondary');
        
    if (!popup.data('loaded'))
    {
      jQuery.ajax({
        url: target.attr('href'),
        dataType: 'html',
        success: function(data, status, xhr)
        {
          // rempli la popup d'objets
          popup.find('div.full-list').empty();
          popup.find('div.full-list').append(data);
          popup.data('loaded', true);
          popup.jqmShow();

          // coche les checkboxs de la méga liste cochés dans la petite liste
          var checkeds = target.parent().find('div.splittedcheckboxes-first input:checked');
          checkeds.each(function(i, el){
            popup.find('input#'+jQuery(el).attr('id')).attr('checked', 'checked');
          })

          // prefix les names, id des checkboxs visibles par 'splittedcheckboxes_'
          var inputs = target.parent().find('div.splittedcheckboxes-first input');
          inputs.each(function(i, input)
          {
            jQuery(input).attr('name', 'splittedcheckboxes_'+jQuery(input).attr('name'));
            jQuery(input).attr('id', 'splittedcheckboxes_'+jQuery(input).attr('id'));
            jQuery(input).next('label').attr('for', 'splittedcheckboxes_'+jQuery(input).next('label').attr('for'));
          });
        }
      });
    }
  })
});
