I am building an web application that is going to work in a similar way like photoshop or illustrator regarding layers. Here are my code that is searching the ".case" div for images, and then put these images id's to an array. When that is done we are looping the array to find out if the received image is the same as the one we loop through from the array.
When finding this image we wan't to move it one step up or down in the hierarchy. But this image have a div with controllers associated, so we have to move this in the same way.
So do you think this is the best possible code we can create to get this work? It does work, I only want to find out if there are any better solutions for it.
var arrangeLayers = function(e,direction) {
var timestampId = e.attr('id');
timestampId = timestampId.replace('name', '');
var elem = [];
$('.case img').each(function(){
elem.push($(this).attr('id'));
});
for (i=0;i<elem.length;i++) {
if (timestampId == elem[i] && direction == 'down') {
if (i != 0) {
$('#' + timestampId).insertBefore($('#' + elem[(i-1)]));
$('#name' + timestampId).insertBefore($('#' + timestampId));
}
} else if (timestampId == elem[i] && direction == 'up') {
if (i != (elem.length) -1) {
$('#' + timestampId).insertAfter($('#' + elem[(i+1)]));
$('#name' + timestampId).insertBefore($('#' + timestampId));
}
}
}
}