/*
Copyright (c) 2006 Ylab, http://www.ylab.nl

Example use:
var currentViewport = new Viewport();
window.status = 'Height: ' + currentViewport.height;
20060927:yc:extended with scrollLeft and scrollTop
*/

function Viewport(){
  this.width = 0;
  this.height = 0;
  if (self.innerHeight){
    // all except Explorer
  	this.width = self.innerWidth;
  	this.height = self.innerHeight;
  	this.scrollLeft = self.pageXOffset;
  	this.scrollTop = self.pageYOffset;
  }
  else{
    // Explorer Strict Mode or CompatMode
    var iebody = (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body;
  	this.width = iebody.clientWidth;
  	this.height = iebody.clientHeight;
  	this.scrollLeft = iebody.scrollLeft;
  	this.scrollTop = iebody.scrollTop;
  }
}

function Outerport(){
  this.width = 0;
  this.height = 0;
  if (window.outerWidth){
    this.width = window.outerWidth;
    this.height = window.outerHeight;
  }
  else if (document.body){
    // other Explorers
  	this.width = document.body.clientWidth;
  	this.height = document.body.clientHeight;
  }
}

function absoluteTop(){
  var t = this.offsetTop;
  if(this.offsetParent){
    t += absoluteTop.apply(this.offsetParent);
  }
  return t;
}

function inView(){
  var abstop = absoluteTop.apply(this);
  var vp = new Viewport();
  var delta = vp.height + vp.scrollTop - abstop - 20;
  if(delta < 0){
    if(this.scrollIntoView){
      this.scrollIntoView();
    }
    else{
	    window.scrollBy(0, -1 * delta);
	  }
	}
}
