// LBP - Admin
// Gestion des accordéons dans les formulaires
if (msiePatchAccordion==undefined) {
  var msiePatchAccordion = true;
}
if (lbpStatutAccordion==undefined) {
  var lbpStatutAccordion = false;
}
function LBP_disableAccordion() {
  $("div[id='lbpFormAccordion']").each(function(i) {
  	$(this).accordion('disable');
  });
}
$(function() {
  if (!lbpStatutAccordion) {
    $("div[id='lbpFormAccordion']").each(function(i) {
      if (!$.browser.msie) {
	      $(this).accordion({header: "h3", autoHeight: false, collapsible: true, clearStyle: true, active: (i>0)?-1:0});
	    } else {
	      $(this).accordion({header: "h3", autoHeight: false, collapsible: true, clearStyle: true});
      }
	    $(this).bind('accordionchange', function(event, ui) {
        var n = $("div[id='lbpFormAccordion']").length;
        var a = $('.ui-state-active').length;
        if (a<n) {
          $('#lbpFormExpand').html("<p><a><img src=\"../img/plus-16x16.png\"/>&nbsp;Tout d&eacute;plier</a></p>");
        } else {
          $('#lbpFormExpand').html("<p><a><img src=\"../img/minus-16x16.png\"/>&nbsp;Tout replier</a></p>");
        }
      });
    });
    $('#lbpFormExpand').click( function() {
      var a = 0;
      var expandHTML = $('#lbpFormExpand').html();
      if (expandHTML.indexOf("img/minus")>0) a = -1;
      $("div[id='lbpFormAccordion']").each(function(i) {
        var s = ($(this).children("h3").hasClass('ui-state-active'))?0:-1;
        if (s!=a) $(this).accordion("activate", a);
      });
    });
    if ($.browser.msie) {
      if (msiePatchAccordion==true) {
        $("div[id='lbpFormAccordion']").each(function(i) {
          if (i>0) {
            $(this).accordion("activate", -1);
          }
        });
      } else {
        $('#lbpFormExpand').html("<p><a><img src=\"../img/minus-16x16.png\"/>&nbsp;Tout replier</a></p>");
      }
    }
  } else {
    $("div[id='lbpFormAccordion']").each(function(i) {
      var n = $("div[id='lbpFormAccordion']").length;
      if (!$.browser.msie) {
	      $(this).accordion({header: "h3", autoHeight: false, collapsible: true, clearStyle: true, active: (i>0 && i<(n-1))?-1:0});
	      if (i<(n-1)) $(this).accordion('disable');
      } else {
	      $(this).accordion({header: "h3", autoHeight: false, collapsible: true, clearStyle: true});
      }
    });
    $('#lbpFormExpand').html("");
    if ($.browser.msie) {
      if (msiePatchAccordion==true) {
        $("div[id='lbpFormAccordion']").each(function(i) {
          var n = $("div[id='lbpFormAccordion']").length;
          if (i>0 && i<(n-1)) {
            $(this).accordion("activate", -1);
            $(this).accordion('disable');
          }
        });
      }
    }
  }
});

