jQis.ready(function () {

jQis(".postWrapper h2.postTitle").hide();

	// bits taken from the real nice work of Tomas Celizna on http://formsofinquiry.com

  display_section_titles();
});

jQis(window).scroll(function () { 
  display_section_titles();
});

jQis(window).resize(function () { 
  display_section_titles();
});


function display_section_titles() {
  var sectionArray = jQis.makeArray(jQis(".postWrapper"));
  
  
  
  var window_scroll_top = jQis(window).scrollTop();
  var window_center = jQis(window).height()/2;
  window_center = 100;
  
  // sort sections by their relative distance to the center of the window
  sectionArray.sort( function(a, b) { 
    return getDiff(a, window_scroll_top, window_center) > getDiff(b, window_scroll_top, window_center) ? 1 : -1; 
  });
  

	jQis(".postWrapper h2.postTitle").hide();

  jQis(sectionArray[0]).children("h2").show();
  
}

function getDiff(item, window_scroll_top, window_center) {
  var item_viewportoffset_top = jQis(item).offset().top - window_scroll_top;

  var dist_of_top = Math.abs(item_viewportoffset_top - window_center);
  var dist_of_bottom = Math.abs(item_viewportoffset_top + jQis(item).height() - window_center);

  // return minimum of distances of top and bottom of an element
  // to center of the window
  return Math.min( dist_of_top, dist_of_bottom );
}






