function intertextual_show(t,id) {
	jQuery('.intertextual-t'+id,document).addClass('intertextual-j').addClass('intertextual-j'+id.toString());
	intertextual_slipto(t,id);
}

function intertextual_hide(id) {
	jQuery('.intertextual-t'+id,document).removeClass('intertextual-j').removeClass('intertextual-j'+id.toString());
	// Undo extreme shifts
	jQuery('.ix-wonky').each(function(i) { intertextual_unwonky(jQuery(this)); });
}

function intertextual_absolute(what) {
  what.css({ position: "absolute", marginLeft: 0, marginTop: 0,
	     top: what.position().top, left: what.position().left });
}

jQuery(function(){
  jQuery('.ix-slippy').wrapInner("<div class='ix-slipin'></div>");
  jQuery('.ix-slipin').css('border','1px solid grey');
  jQuery('.ix-slipin').each(function(i) {
    jQuery(this).css('padding',jQuery(this).parent('.ix-slippy').css('padding'));
    jQuery(this).parent('.ix-slippy').css('padding','0');
  });
  jQuery('.ix-slippy').parents('table').find('td:not(.ix-slippy)').css('border','1px solid grey'); 
});

function intertextual_top() {
  return jQuery('html').scrollTop();
}

function intertextual_bottom() {
  var top=intertextual_top();
  return top+jQuery(window).height();
}

function intertextual_onscreen(offset) {
  var top=jQuery('html').scrollTop();
  var bottom=top+jQuery(window).height();
  return (offset>=top && offset<bottom);
}

function intertextual_slipup(src,dest) {
	var slipin=dest.parents('.ix-slipin');
	var slippy=dest.parents('.ix-slippy');
	var slh=slippy.height();
	intertextual_absolute(slipin);
	slippy.css('height',slh);
	var shift=src.offset().top-dest.offset().top;
	var origshift=shift;
	// no scrolling off top
	if(slipin.offset().top+shift<slippy.offset().top)
          shift=slippy.offset().top-slipin.position().top;
	// no scrolling off bottom
	if(slippy.offset().top+slippy.height()<slipin.position().top+slipin.height()+shift)
	  shift=slippy.offset().top+slippy.height()-(slipin.position().top+slipin.height());
	// no moving if on screen
	if(intertextual_onscreen(dest.offset().top) && intertextual_onscreen(dest.offset().top+dest.height())) {
		return;
	}
	slipin.css({ position: "absolute"});
	slipin.animate({
		   top: slipin.position().top+shift, 
		   left: slipin.position().left },
		   {
		   complete: function() {
		     // Check no more drastic measures are needed
 	  	     if(intertextual_onscreen(dest.offset().top)) {
	               return; // okay
		     }
	             var more_shift=dest.offset().top-intertextual_bottom()+20;
		     if(more_shift<0)
		        more_shift=dest.offset().top-intertextual_top()+20;
                     slipin.css({ position: "absolute", 'background-color': 'white'});
		     slipin.addClass('ix-wonky');
	             slipin.animate({
		       top: slipin.position().top-more_shift,
		       left: slipin.position().left },{easing: 'linear'});
	             slipin.addClass('ix-wonky');
                   },
		   easing: 'linear'
	});
}

// XXX we should audit our use of position vs offset
function intertextual_unwonky(slipin) {
	var slippy=slipin.parents('.ix-slippy');
	slipin.stop();
	slipin.css({top: slippy.position().top, background: 'none'});
	slipin.removeClass('ix-wonky');
}

function intertextual_slipto(src,id) {
	var nodes=jQuery('.ix-slippy',document).find('.intertextual-t'+id);
	if(nodes.count==0)
		return;
	var node=nodes[0];
	intertextual_slipup(jQuery(src),jQuery(node));
}

jQuery(function() {
    jQuery('.ix-slippy',document).each(function(i){
    });
});

