// JavaScript Document
$(document).ready(function() {
  var nrgtiles = $('div[class^=image]').length;
  $('body').append('<div id="nrgrotate">&nbsp;<\/div>');
  var i=1;
  while (i<=nrgtiles) {
    $('#nrgrotate').append('<a href="#"><em>&nbsp;<\/em><\/a>');
  i++;
  }
  $('#nrgrotate a:eq(1)').addClass('active');
  $('#nrgrotate a:eq(0)').click(function() {
    homeFadeOut();
    return false;
  });
  $('#nrgrotate a:eq(1)').click(function() {
    homeFadeIn();
    return false;
  });
  timer = setTimeout(homeFadeOut, 5000); // initial rotation
});

function homeFadeIn() {
  clearTimeout(timer);
  $('#nrgrotate a:eq(0)').removeClass('active');
  $('#nrgrotate a:eq(1)').addClass('active');
  $('.image2').fadeIn(1000, function() {
    timer = setTimeout(homeFadeOut, 10000);
  });
}

function homeFadeOut() {
  clearTimeout(timer);
  $('#nrgrotate a:eq(1)').removeClass('active');
  $('#nrgrotate a:eq(0)').addClass('active');
  $('.image2').css('margin-top', '-264px').fadeOut(1000, function() {
    timer = setTimeout(homeFadeIn, 10000);
  });
}
