
var scroll_target = 0;
var cTimeout = null;


function startScrollTo(id, height){
	debugtext += "startScrollTo("+id+", "+height+")" + "\n";
	
  // if everything is closed we have to scroll up
  if ( id == -1 ){
  	scroll_target = 0;
    clearTimeout(cTimeout);
    scroll("no");
    return;
  }

  var siteheight = getSiteHeight();
  var position = getPosition(id);
  var sitePosition = getCurrentYPos();
  if ( height > siteheight-30 ) height = siteheight-30 ;

  debugtext += "  siteheight " + siteheight + "\n";
  debugtext += "  position " + position + "\n";
  debugtext += "  sitePosition " + sitePosition + "\n";

  var offset;
  if ( (offset = position - sitePosition) < 0  ){
    debugtext += "  ---plus\n";
  	scroll_target = sitePosition + offset - 200;
  	clearTimeout(cTimeout);
  	scroll("up");
  } else if ( (offset = siteheight - offset - height) < 0 ) {
    debugtext += "  ---minus\n";
    scroll_target = sitePosition - offset + 200;
    clearTimeout(cTimeout);
    scroll( "down" );
  }

}


function scroll( direction, lastPos ){
  //debugtext += "scroll("+lastPos+")" + "\n";
  var speed = 25;
  if (navigator.appName.indexOf('Netscape') != -1) speed = 40;

  var sitePosition = getCurrentYPos();
  var newPosition  = sitePosition;
  if ( lastPos!=null ) {
    debugtext += "  lastPos " + lastPos + "\n";
    debugtext += "  lastPosCurrent " + getCurrentYPos() + "\n";
  	if ( getCurrentYPos() != lastPos ) return;
  }
  var finished = false;

  debugtext += "  sitePosition " + sitePosition + "\n";
  
  if ( scroll_target < sitePosition && (direction == "up" || direction == "no") ) {
    window.scrollBy(0,-1*speed);
    newPosition = newPosition - speed;
  } else if ( scroll_target - 20 > sitePosition && direction == "down" ) {
    window.scrollBy(0,speed);
    newPosition = newPosition + speed;
  } else {
  	finished = true;
  }

  debugtext += "  newPosition " + getCurrentYPos() + "\n";
  debugtext += "  newPositionCurrent " + getCurrentYPos() + "\n";
  
  // continue
  if (!finished){
    cTimeout = setTimeout("scroll('" +direction+ "', "+newPosition+")", 2);
  }
}






var shrink_containersize = 100;

function checkScroll(){
  var containerSize = document.getElementById("container").style.height;
  containerSize = parseInt(containerSize.substring(0, containerSize.length-2));
  
  var siteheight = getSiteHeight();
  var sitePosition = getCurrentYPos();
  var containeroffset = document.getElementById("container").offsetTop;

  if ( shrink_containersize < containerSize ){
    containerSize = sitePosition + siteheight - containeroffset;
    if ( containerSize < shrink_containersize ) containerSize = shrink_containersize;
    document.getElementById("container").style.height = containerSize + "px";
  } else {
    document.getElementById("container").style.height = shrink_containersize + "px";
  }
}












function getPosition(id){
//  var elem=document.getElementById(idString);
  var cont=document.getElementById("container");
  return cont.offsetTop + sitedata[getIntex(id)]["pos_y"];
}



function getCurrentYPos() {
 if (document.body && document.body.scrollTop)
   return document.body.scrollTop;
 if (document.documentElement && document.documentElement.scrollTop)
   return document.documentElement.scrollTop;
 if (window.pageYOffset)
   return window.pageYOffset;
 return 0;
}


function getSiteHeight() {
  if (navigator.appName.indexOf('Netscape') != -1){
    return self.innerHeight;
  } else if (navigator.appName.indexOf('Microsoft') != -1) {
    return document.body.clientHeight;
  } else {
    return window.innerWidth;
  }
}

