// defaults
var time_ms_fade_between  = 10000; 
var time_ms_fade_duration = 3.0;

var hpm_container_id      = 'headerphotos';
var hpm_container         = null;
var hpm_images            = Array();

var hpm_image_current     = 0;
var hpm_image_previous    = 0;

function hpm_init() {
  hpm_container = $(hpm_container_id);
  hpm_images    = hpm_container.select('img');

  // fade in the first photo
  hpm_images[hpm_image_current].show();

  setTimeout('fade_image()', time_ms_fade_between);
}

function fade_image() {
  hpm_image_current++;

  // to the firts picture
  if (hpm_image_current >= hpm_images.length) {
    hpm_image_current = 0;
  }

  // calculate previous number
  if (hpm_image_current == 0) {
    hpm_image_previous = hpm_images.length - 1;
  } else {
    hpm_image_previous = hpm_image_current - 1;
  }

  // hide previous and appear current
  hpm_images[hpm_image_current].appear( { duration: time_ms_fade_duration, from: 0, to: 1 } );
  setTimeout(function() { hpm_images[hpm_image_previous].fade( { duration: time_ms_fade_duration, from: 1, to: 0 } ); }, 350);

  setTimeout('fade_image()', time_ms_fade_between);
}