var scrollMaxSpeed = 30;
var scrollWheelStep = 20;
var scrollStep = 1;
var scrollAccel = 1;
var scrollFrame;
var frameDoc = false;

function showFrame(id,stylesheet,script) {
    handleResize();
	scrollFrame = document.getElementById("scrollframe");
	frames["scrollframe"].scrollTo(0,0);
	frameDoc = scrollFrame.contentDocument ? scrollFrame.contentDocument : scrollFrame.contentWindow.document;
	frameDoc.open();
	frameDoc.writeln('<html><head>');
	frameDoc.writeln('<link type="text/css" rel="stylesheet" href="css/frame.css">');
	if(stylesheet) {
	  frameDoc.writeln('<link id="css_' + stylesheet + '" href="css/' + stylesheet + '.css" rel="stylesheet" type="text/css">');
    }
    if(!script) {
      script = "";
    }	
	frameDoc.writeln('</head><body onLoad="top.handleScroll();' + script + '" onMouseWheel="top.handleWheel(event)">');
	frameDoc.write(document.getElementById(id).innerHTML);
	frameDoc.writeln('</body></html>');
	frameDoc.close();
}	

function handleResize() {
  theHeight = self.innerHeight ? self.innerHeight : document.body.clientHeight;
  headerHeight = document.getElementById("header").offsetHeight;
  footerHeight = document.getElementById("footer").offsetHeight;
  frameHeight = theHeight - headerHeight - footerHeight;
  document.getElementById("main").style.height = (frameHeight) + "px";
  document.getElementById("scrollDown").style.top = (theHeight - headerHeight - footerHeight - 23) + "px";
  handleScroll();
}

function handleScroll() {
  if (frameDoc) {
    contentHeight = frameDoc.body.scrollHeight;
    if (contentHeight > frameHeight) {
      document.getElementById("scrollUp").style.display = "block";
      document.getElementById("scrollDown").style.display = "block";
    } else {
      document.getElementById("scrollUp").style.display = "none";
      document.getElementById("scrollDown").style.display = "none";
    }
  }
}

function handleWheel(e) {
  direction = (e.wheelDelta > 0 ? -1 : 1);
	scrollFrame = frames["scrollframe"];
	scrollFrame.scrollBy(0,direction*scrollWheelStep);
}

function scrollDown() {
  scrollStart(1);
}

function scrollUp() {
  scrollStart(-1);
}

function scrollStart(direction) {
	scrollFrame = frames["scrollframe"];
	scrollFrame.scrollBy(0,direction*scrollStep);
	if (scrollStep < scrollMaxSpeed || scrollAccel < 0) {
	  scrollStep += 2*scrollAccel;
	}
	if (scrollStep > 0) {
	  scrollTimer = setTimeout("scrollStart(" + direction + ")",100);
	} else {
	  scrollAccel = 1;
	  scrollStep = 1;
	};
}

function scrollStop() {
  if (scrollAccel == 1 && scrollStep != 1) {
    scrollAccel = -2;
  }
}
