// Randomize func - http://stackoverflow.com/questions/1533910/randomize-a-sequence-of-div-elements-with-jquery
(function($) {
$.fn.randomize = function(childElem) {
  return this.each(function() {
      var $this = $(this);
      var elems = $this.children(childElem);
      elems.sort(function() { return (Math.round(Math.random())-0.5); });
      $this.remove(childElem);
      for(var i=0; i < elems.length; i++)
        $this.append(elems[i]);
  });
}
})(jQuery);

(function ($) {
  // Function to fade out top image place new image and loop
  function swapImage(container, target, duration) {
  	// Set our first image
  	var $currentImage = $(container + ' ' + target  + ':first');
  	// Set the new image
  	var $newImage = $currentImage.next(target);
  	// Make sure our current image is up high enough
  	$currentImage.css('z-index', 300);
  	// Place image behind it
  	$newImage.css('z-index', 250).show();
  	// Fade out top image, remove it and add to end of div
  	$currentImage
  		.fadeOut(2000, function() {
  			$(this)
  				.remove()
  				.css('z-index', 200)
  				.appendTo(container);
  		}
  	);
  	// Start loop
  	setTimeout(function() {
  	  swapImage(container, target, duration);
  	}, duration);
  }

  // Function to call to start carousel
  function startCarousel(container, target, duration) {
  	setTimeout(function() {
  	  swapImage(container, target, duration);
  	}, duration);
  }

  $(document).ready(function() {
    $(".pane-views-carousels-block-2 .field-content ul").randomize("li");
    $(".pane-views-carousels-block-2 .field-content ul li:first").css('z-index', 900).show();
    $('.pane-views-carousels-block-1 .view-content .views-row:first').show();
  	startCarousel('.pane-views-carousels-block-1 .view-content', '.views-row', 8000);
  	startCarousel('.pane-views-carousels-block-2 .field-content ul', 'li', 3500);
  });
})(jQuery);;

