$(document).ready(function(){
  rotateDIVs(1);
});

function rotateDIVs(currentDIV) {
  var numberOfDIVs = $('#hp_bg div').length;
  currentDIV = currentDIV % numberOfDIVs;
	
  $('#hp_bg div').eq(currentDIV).fadeOut(function() {
		// re-order the z-index
    $('#hp_bg div').each(function(i) {
      $(this).css(
        'zIndex', ((numberOfDIVs - i) + currentDIV) % numberOfDIVs
      );
    });
    $(this).show();
    setTimeout(function() {rotateDIVs(++currentDIV);}, 8000);
  });
}
