jQuery(function ($) {
  var elements = $('.accordion'),
      contentHolder = $('#slide');
      
	elements.each(function (index) {
		var current = $(this).addClass('closed');     
        current.find('h3').click(function(){
        	if (current.hasClass('closed')) {
				current.find('.accordion-content').animate(
          {
            height: 'show',
            marginTop: 'show',
            marginBottom: 'show',
            paddingTop: 'show',
            paddingBottom: 'show'
          },
          {
            duration: 'slow',
            step: function (now, fx) {
              centerElement(contentHolder);
            }
          }
        );
        elements.not(current).filter(':not(.closed)').addClass('closed').find('.accordion-content').slideUp('slow'); // should be the same duration as used in the animate()
			}
			else {
				current.find('.accordion-content').animate(
          {
            height: 'hide',
            marginTop: 'hide',
            marginBottom: 'hide',
            paddingTop: 'hide',
            paddingBottom: 'hide'
          },
          {
            duration: 'slow',
            step: function (now, fx) {
              centerElement(contentHolder);
            }
          }
        );;
			}
			current.toggleClass('closed');
		});
	});
  
  
    var first_el = null;
    if(elements[0]) first_el = elements[0];
    
    //apertura primo accordion di default
    if($(first_el).hasClass('force_opened')) $(first_el).find('h3').click();
  
  
  
  function centerElement(elements) {
     //vertical middle
    var wHeight = $(window).height();
    var hHeight = $('#header').height();
    var cHeight = wHeight - hHeight;
    elements.each(function(){
      var bHeight = $(this).height();

      if ( cHeight > bHeight ){
        $(this).css('margin-top',  Math.floor(((cHeight) - (bHeight))/2) + 'px');
      }
      else {
        $(this).css('margin-top', 20 + 'px');
        $(this).css('margin-bottom', 20 + 'px');
      }
    })
  }
});
