$("document").ready( function() {
  var max = 5;
  var i = 1 + Math.floor(Math.random() * max); // 1 .. max
  var next = (i == max) ? 1 : i+1;
  
  $("#header").prepend("<div id=\"headerbg\"></div>");
  $("#headerbg").css("backgroundImage", "url('images/header"+i+".jpg')");
  
  setInterval( function() {
    $("#headerbg").fadeOut(2500, function() {
      $(this).css("backgroundImage", "url('images/header"+i+".jpg')")   
      $(this).show();
    });
    $("#header").css("backgroundImage", "url('images/header"+next+".jpg')");
    i = (i == max) ? 1 : i+1;
    next = (i == max) ? 1 : i+1;
  }, 6000);
});
