$.fn.reorder = function() {

  // random array sort from
  // http://javascript.about.com/library/blsort2.htm
  function randOrd() { return(Math.round(Math.random())-0.5); }

  return($(this).each(function() {
    var $this = $(this);
    var $children = $this.children();
    var childCount = $children.length;

    if (childCount > 1) {
      $children.remove();

      var indices = new Array();
      for (i=0;i<childCount;i++) { indices[indices.length] = i; }
      indices = indices.sort(randOrd);
      $.each(indices,function(j,k) { $this.append($children.eq(k)); });

    }
  }));
}

$(function() {
    $("#slideshow").css({
        'position': 'relative',
        'height': $("#illustration-1").outerHeight()
    });

    $("#slideshow").reorder();

    $("#slideshow > div").css('position', 'absolute');
    $("#slideshow > div:first").show().siblings().hide();

    var interval = null;
    var index = 0;

    var callback = function() {
        if($("#slideshow > div:animated").length == 0) {
            var current = $("#slideshow > div:not(:hidden)");
            var next = null;

            if(current.next().length) {
                next = current.next();
            } else {
                next = current.siblings(":first");
            }

            next.css('z-index', index).fadeIn(2000, function() {
                current.hide();
            });

            index++;
        }
    };

    var intervalCallback = function() {
        if($("body").hasClass('index')) {
            interval = setInterval(callback, 7000);
        }
    };

    $("#slideshow").mouseover(function() {
        clearInterval(interval);
    }).mouseout(function() {
        intervalCallback();
    });

    $("#slideshow > div").click(callback);
    intervalCallback();
});

