var home = new function() {

  var init = function() {
    /* Initialize the slideshow */
    $('#slideshow .project').each( function() {
      $(this).mouseenter( function() { projectOverlay(this); } );
    });
    if ( $( '#slideshow .project').length > 1 ) {
      intervalID = setInterval( function() { home.slideForward('auto'); }, 5000 );
      $(document).bind('pageLeave.homepageSlideshow', function() {
        $(document).unbind('pageLeave.homepageSlideshow');
        clearInterval( intervalID );
      });
    }
    /* Initialize the map */
    $('area')
      .mouseenter( function() { $('#' + $(this).attr('title') + '_projects').stop(true,true).fadeIn(); })
      .mouseleave( function() { $('#' + $(this).attr('title') + '_projects').stop(true,true).fadeOut(); })
      .click( function() { application.transition( '/steel_precast/results/'+$(this).attr('title')+'/' ); } );
  };

  var projectOverlay = function(project) {
    $(project).unbind('mouseenter');
    $('.details', $(project))
      .fadeIn('fast')
      .mouseleave( function() {
        if ( $('#slideshow .project').length > 1 ) {
          intervalID = setInterval( function() { home.slideForward('auto'); }, 5000 );
        }
        $(this).fadeOut(300).unbind('mouseleave');
        $('.description', $(this).parent() ).fadeIn(300);
        $(project).bind( 'mouseenter', function() { projectOverlay(this); } );
      });
    $('.overlay', $(project)).hide();
    $('.description', $(project)).fadeOut(300);
    if ( $( '#slideshow .project').length > 1 ) {
      clearInterval( intervalID );
    }
  };

  var slideForward = function( type ) {
    if( type != 'manual' ) {
      var $active = $('#slideshow .project.active');
      if ( $active.length === 0 ) { $active = $('#slideshow .project:first'); }
      var $next = $active.next().length ? $active.next() : $('#slideshow .project:first' );
      $active.removeClass('active').fadeOut();
      $next.addClass('active').fadeIn();
    }
  };

  var mapTransition = function() {
    $('.stateMap .NEzoom').toggle(function() {
      $(this).attr( 'src', '/images/zoom_out.png' )
        .attr( 'title', 'Zoom Out' );
      $('.stateMap .NewEngland').fadeIn();
      $('.stateMap .state').fadeOut();
    }, function() {
      $(this).attr( 'src', '/images/zoom_in.png' )
        .attr( 'title', 'Northeast Zoom' );
      $('.stateMap .NewEngland').fadeOut();
      $('.stateMap .state').fadeIn();
    });
  };

  return {
    init: init,
    slideForward: slideForward,
    mapTransition: mapTransition
  };

};

function prevHeadlines() {
  $show = $('#headlines tr:visible:first').prev().prev().andSelf().prev().andSelf();
  $('#headlines tr:visible:first').next().andSelf().next().andSelf().fadeOut(300,function() {
    $show.fadeIn(300);
  });
  $('#headlineNext').fadeIn(300);
  if( !$show.prev().prev().prev().length ) { $('#headlinePrev').fadeOut(300); }
}

function nextHeadlines() {
  $show = $('#headlines tr:visible:last').next().next().andSelf().next().andSelf();
  $('#headlines tr:visible:last').prev().andSelf().prev().andSelf().fadeOut(300,function() {
    $show.fadeIn(300);
  });
  $('#headlinePrev').fadeIn(300);
  if( !$show.next().length ) { $('#headlineNext').fadeOut(300); }
}