var count = 0;

$( function (){

  if ( $('#home-images').length == 0 ) {
    $( '<div id="home-images"></div>').appendTo('#masthead');
  }

  $.getJSON( "/home_images/data.json", function(data) {

    if ( ! data.items ) {
      return false;
    }

    for ( var i in data.items ) {
      var item = data.items[i];

      s = '<div class="home-image" style="background:url(' + item.url + ') center no-repeat;"></div>';
      $('#home-images').append(s);
    }
    
    $('#home-images .home-image').hide();
    $('#home-images .home-image:first-child').show();
    $('#home-images .home-image:first-child').addClass('home-image-active');
    
    setTimeout ( 'HomeRotate();', 4000 );
  });
  
});



function HomeRotate() {
  var e = $('#home-images .home-image-active').next();
  if ( e.length == 0 ) {
    e = $('#home-images .home-image:first-child');
  }

  $('#home-images .home-image-active').fadeOut( 1000, function() {
    $('#home-images .home-image-active').removeClass('home-image-active');
    $(e).fadeIn( 1000, function() {
      $(e).show();
    });
    $(e).addClass('home-image-active');
  });

  count ++;
  if ( count < 10 ) {
    setTimeout ( 'HomeRotate();', 4000 );
  }

}

