function init() {

  $('div.box').click(function(event) {
    $.foldUnfolded($(event.currentTarget));
    $.unfoldSample($(event.currentTarget));
  });
  
  $('div#unfolded-impressum > div.sample').click(function() {
    $.foldSample($(this).parent());
  });
  
  $('div.preload').each(function() {
    $('#unfolded-' + this.id).load('partials/' + this.id + '.html', {}, function(resp, data, xhr) {
      $('#' + this.id + ' > div.sample > div.clickignore').click(function(event) {
        event.stopPropagation();
      });
      
      $('#' + this.id + ' > div.sample').click(function(event) {
        $.foldSample($(event.currentTarget).parent());
      });

      $('#' + this.id + ' a').each(function() {
        if (this.href.indexOf('mailto') < 0) {
          this.target = '_blank';
        }
      });
    });
  });
}

jQuery.unfoldSample = function(element) {
  $(element).slideUp();
  var unfolded = 'div#unfolded-' + $(element).attr("id");
  $(unfolded).slideDown(function() {
    $.scrollTo($(unfolded), 800, {onAfter:function() {
      $.scrollTo({top:'-=7px'}, 200);
    }});
  });
}

jQuery.foldSample = function(element) {
  $(element).slideUp();
  $(element).prev(".box").slideDown();
}

jQuery.foldUnfolded = function(element) {
  $('div.unfoldedbox').each(function() {
    if ($(this).css('display') != 'none') {
      $.foldSample(this);
    }
  });
}

$(document).ready(function() {
  init();
});
