//  this is a cross browser method of determining the window dimensions
// from http://codesnippets.joyent.com/posts/show/835
Position.GetWindowSize = function(w) {
	var width, height;
        w = w ? w : window;
        this.width = w.innerWidth || (w.document.documentElement.clientWidth || w.document.body.clientWidth);
        this.height = w.innerHeight || (w.document.documentElement.clientHeight || w.document.body.clientHeight);
        
        return this;
}

function loadRemainingItems(){
  // compute amount of page below the current scroll position
  var remaining = ($('divMidWht').viewportOffset()[1] + $('divMidWht').getHeight()) 
                      - Position.GetWindowSize().height;
  //compute height of bottom element
  var last = $$(".item").last().getHeight();
  
  if(remaining < last*2 && !$('complete')){
    if(Ajax.activeRequestCount == 0){
      var url = "/profile_home/retrieve_profile_sphinx_content";
      var last = $$(".item").last().className.match(/[0-9]+/)[0];
      var current_member_id = $$(".votes").last().className.match(/[0-9]+/)[0];
      new Ajax.Request(url, {
        method: 'get',
        parameters: 'last=' + last + '&member_id=' + current_member_id,
        onLoading: function(){
          $('loading').show();
        },
        onSuccess: function(xhr){
          $('loading').hide();
          $('loading').insert({before : xhr.responseText})
        }
      });
    }
  }
}

// hide the pagination links
document.observe("dom:loaded", function(){
  $('pagination').hide();
});

// find to events that could fire loading items at the bottom
Event.observe(window, 'scroll', function(e){
  loadRemainingItems();
});

Event.observe(window, 'resize', function(e){
  loadRemainingItems();
});

