// Remember to include viewfuncs.js before using this.


// Scrolls to a tag whose id is 'top'.
//   Include commonfuncs.js, and in your page call: addOnload(toTopFunc);
var toTopFunc = function toTop() {
  var tag = document.getElementById('top');
  if (tag != null && tag.scrollIntoView) {
    tag.scrollIntoView(true);
  }
  else window.location='#top';
}

// Replaces an img tag's url.
function switchPic(id, url) {
  var maxW = 395;  // Card pics can be no wider than this

  var tag = document.getElementById(id);
  if (tag != null) {
    tag.src = url;
    var w = tag.width;
    var h = tag.height;
    if (w > maxW + 4) {
      var scale = 1-((w-maxW)/w);
      tag.width = w * scale;
      tag.height = h * scale;
    }
  }
}


var originalOffsets = new Object();
function initScroll(id, initialY) {
  var tag = document.getElementById(id);
  if (tag == null) return;
  originalOffsets[id] = initialY;
  setTimeout("scrollit('"+ id +"')",750);
}

// Repeatedly shifts an element into view.
//   But not above its original position.
//   Do not call this directly. Use initScroll() instead.
function scrollit(id) {
  var tag = document.getElementById(id);
  if (tag == null) return;
  if (typeof originalOffsets[id] == "undefined") return;
  var pos = originalOffsets[id];

  var viewOffsetY = parseInt(getViewOffset().y);
  var newPosition = 0;
  if (viewOffsetY > pos) newPosition = viewOffsetY-pos;
  else newPosition = 0;
  tag.style.top = newPosition +"px";
  setTimeout("scrollit('"+ id +"')",750);
}

