Optimize “layering” in jQuery
Not if I understand what you're doing correctly.

So essentially each image has itself, and then some other element that should be before it as a buddy? Assuming they are all siblings, you should utilize some of jQuery's functions that support working with siblings.

Here's an example, though I'm sure it could still be better:

var arrangeLayers = function (e, direction) {
    var $target = $(e);
    var $targetBuddy = $target.prev();
    if (direction === 'down') {     
        if($targetBuddy.index() &gt; 1) {
            $target.insertBefore($targetBuddy.prev().prev());
        }
    } else {
        if ($target.index() !=$target.siblings().length) {
            $target.insertAfter($target.next().next());
        }
    }
    $targetBuddy.insertBefore($target);
}


Or if you don't care about index error checking (which in actual practice appears to not matter):

var arrangeLayers = function (e, direction) {
    var $target = $(e);
    var $targetBuddy = $target.prev();
    if (direction === 'down') {     
        $target.insertBefore($targetBuddy.prev().prev());
    } else {
        $target.insertAfter($target.next().next());
    }
    $targetBuddy.insertBefore($target);
}


Here's a jsfiddle without the checking: