var current_section = null;

$(document).ready(function() {

  
  // initial load of projects in full screen
  if(width_check() == 'full screen') {
    // set window height and width
    //$('body').css('width', $(document).width()).css('height', $(document).height());
    // set current project
    if(document.location.hash == '') {
      $('section[subject=oregon-humane-society]').fadeIn('slow');
      current_section = 'oregon-humane-society';
    } else {
      $('section[subject='+document.location.hash.replace('#','')+']').delay(100).fadeIn('slow');
      current_section = document.location.hash.replace('#','');
    }
  }
  
  // initial load of projects in web view
  if(width_check() != 'full screen') {
    $('body').css('width', 'auto').css('height', 'auto');
    if(document.location.hash == '') {
      $('section[subject=oregon-humane-society]').slideDown('slow');
      current_section = 'oregon-humane-society';
    } else {
      $('section[subject='+document.location.hash.replace('#','')+']').delay(100).slideDown('slow');
      current_section = document.location.hash.replace('#','');
    }
  }
  
  // navigation - only used in web view
  $('nav a').click(function() {
    var next_section = $(this).attr('hash').replace('#','');
    $('section[subject='+current_section+']').slideUp('slow', function() {
      $('section[subject='+next_section+']').slideDown('slow');
      current_section = next_section;
    });
  });
  
  $('header h1').mouseenter(function() {
    $('header details').stop().slideDown();
  });
  
  $('header details').mouseleave(function(){
    $(this).stop().slideUp();
  });
  
  // handle dual image scrolling
  $('.scroll_together img').mousemove(function(e) {
    var obj = $(this);
    var mouse_y = (e.pageY - $(obj).parent().parent().parent().offset().top);
    var img_height = obj.height();
    var box_height = $(obj).parent().parent().parent().height();
    var margin_top = $(obj).css('margin-top').replace('%','').replace('px','');
    
    var offset = (mouse_y * img_height / box_height) / img_height;
    offset *= (img_height - box_height);
    
    $(obj).parent().siblings().andSelf().children('img').css('margin-top', '-'+offset+'px');
  });
  
  // watching for navigation press in full screen
  $(document).keypress(function(e) {
    if(width_check() == 'full screen') {
      switch(e.charCode) {
        case 106: 
          slide_up();
          break;
        case 107:
          slide_down();
          break;
        case 112:
          previous_project();
          break;
        case 110:
          next_project();
          break;
     }
    }
  });
});

function width_check() {
  if($(window).width() > 1400) {
    return 'full screen' 
  } else {
    return 'narrow screen'
  }
}

function slide_up() {
  var current_section = $('section').filter(':visible').attr('subject');
  var current_slide = $('section[subject='+current_section+'] article').filter(':visible');
  var next_slide = current_slide.next();
  if(next_slide.is('article')) {
    current_slide.hide();
    next_slide.show();
  }
}

function slide_down() {
  var current_section = $('section').filter(':visible').attr('subject');
  var current_slide = $('section[subject='+current_section+'] article').filter(':visible');
  var next_slide = current_slide.prev();
  if(next_slide.is('article')) {
    current_slide.hide();
    next_slide.show();
  }
}

function previous_project() {
  var current_section = $('section').filter(':visible');
  var next_project = current_section.prev();
  if(next_project.is('section')) {
    current_section.hide();
    next_project.show();
    current_section = next_project;
    document.location.hash = next_project.attr('subject');
  }
}

function next_project() {
  var current_section = $('section').filter(':visible');
  var next_project = current_section.next();
  if(next_project.is('section')) {
    current_section.hide();
    next_project.show();
    current_section = next_project;
    document.location.hash = next_project.attr('subject');
  }
}
