I have these two carousels that randomly pick up pictures. [fiddle] Each of them picks up pictures and links from their own different inner array. It works fine as it is, but I was just wondering if there's any part that I can encapsulate/shorten. The only thing I can come up with that doesn't return any error is them sharing the same $(document).ready(function(){
[fiddle].
$('#carousel1').jsCarousel({ onthumbnailclick: function(src) { load(src); }, autoscroll: true, circular: true, masked: false, itemstodisplay: 3, orientation: 'h' });
$('#carouselu').jsCarousel({ onthumbnailclick: function(src) { load(src); }, autoscroll: true, circular: true, masked: false, itemstodisplay: 3, orientation: 'h' });
var theImages = [
['img1','url_1'],
['img2','url_2'],
['img3','url_3'],
['img4','url_4'],
['img5','url_5'], ];
var shuffled = [];
while (theImages.length) {
shuffled.push(theImages.splice(Math.random() * theImages.length, 1)[0]);
}
$(document).ready(function(){
var imgPath = "/pic/";
var urlPath = "/url/";
$("#carousel1 .1").each(function(index){
$(this).prepend('<a href="'+imgPath+shuffled[index][1]+'"><img src="'+urlPath+shuffled[index][0]+'"></a>');
});
});
/* Second rotator */
var theImagesabc = [
['imga','url_a'],
['imgb','url_b'],
['imgc','url_c'],
['imgd','url_d'],
['imge','url_f'] ];
var shuffledabc = [];
while (theImagesabc.length) {
shuffledabc.push(theImagesabc.splice(Math.random() * theImagesabc.length, 1)[0]);
}
$(document).ready(function(){
var imgPath = "/pic/";
var urlPath = "/url/";
$("#carouselu .u").each(function(index){
$(this).prepend('<a href="'+imgPath+shuffledabc[index][1]+'"><img src="'+urlPath+shuffledabc[index][0]+'"></a>');
});
});