var ConstituentParts;
if (!ConstituentParts) { 
  ConstituentParts = {};
}
if (!ConstituentParts.Projects) { 
  ConstituentParts.Projects = {};
}
ConstituentParts.Projects.Grid = {
  'remove_focus_from': function(node) {
    var project = $(node);
    project.find('.highlight img').fadeOut(1000);
    project.find('.knockback img').fadeIn(1000);
  },
  
  'switch_focus_to': function(index_or_null) {
    var pr = ConstituentParts.Projects;
    var index = (index_or_null == null ? 0 : index_or_null);
    var project = $(pr.projects[index]);
    pr.Grid.focus_project(project, index_or_null);
  },
  
  'focus_project': function(project, index_or_null) {
    var pr = ConstituentParts.Projects;
    project.find('.knockback img').fadeOut(1000);
    project.find('.highlight img').fadeIn(1000);
    if (index_or_null == null) {
      $('#highlighted-project').hide();
      $('#highlighted-project').fadeIn(1250);
    }
    else {
      $('#highlighted-project .current').fadeOut(750, function() { 
        $(this).removeClass('current');
        $('#' + project.attr('id') + '-detail').addClass('current').fadeIn(1250);
      });
    }
  },
  
  'set_summary': function(project) {
    $('#highlighted-project h1').text(project.find('h1').text());
    $('#highlighted-project .summary').html(project.find('.summary').html());
  },
  
  'interrupt': function() {
    var pr = ConstituentParts.Projects;
    clearInterval(pr.interval_id);
    var current_project = pr.projects[pr.current_project_index];
    if (this != current_project) {
      $(current_project).find('.knockback img').show();
      $(current_project).find('.highlight img').hide();
      $(this).find('.knockback img').hide();
      $(this).find('.highlight img').show();
      // pr.Grid.set_summary($(this));
      $('#highlighted-project .project-detail').hide();
      $('#' + this.id + '-detail').show();
    }
  },
  
  'resume': function() {
    var pr = ConstituentParts.Projects;
    pr.interval_id = setInterval(pr.next, 5000);
    var current_project = pr.projects[pr.current_project_index];
    if (this != current_project) {
      $(current_project).find('.knockback img').hide();
      $(current_project).find('.highlight img').show();
      $(this).find('.knockback img').show();
      $(this).find('.highlight img').hide();
      // pr.Grid.set_summary($(current_project));
      $('#highlighted-project .project-detail').hide();
      $('#' + current_project.id + '-detail').show();
    }
  },
  
  'initialize': function() {
    var pr = ConstituentParts.Projects;
    // append all the grid basics
    $('#content').append('<div id="highlighted-project"></div><div id="grid-of-self-promotion"></div>');
    // move the project nodes to the grid div
    $('#list-of-self-promotion').children().appendTo('#grid-of-self-promotion');
    // and remove the list node
    $('#list-of-self-promotion').remove();
    // set up the text slides
    $('#grid-of-self-promotion .project').each(function () {
      $('#highlighted-project').append('<div class="project-detail" id="' + this.id + '-detail' + '"></div>');
      var slide_div = $('#highlighted-project div:last');
      $(this).find('h1').clone().appendTo(slide_div);
      $(this).find('.summary').clone().appendTo(slide_div);
    });
    $('#highlighted-project .project-detail').hide();
    $('#highlighted-project  div.project-detail:first').addClass('current').show();
    var max_detail_height = $('#highlighted-project div.project-detail').inject(0, function(max) {
      var cur_height = $(this).height();
      return cur_height > max ? cur_height : max;
    });
    $('#highlighted-project').height(max_detail_height);
    // then start the machine
    pr.setup('#grid-of-self-promotion .project', pr.Grid);
    // when you hover on one of the projects in the grid
    $('.project').hover(pr.Grid.interrupt, pr.Grid.resume);
  }
}

$(document).ready(function() {
  ConstituentParts.Projects.Grid.initialize();
});
