/***

Scroll-UP, V1.0

**/
var goto_top_type = -1;
var goto_top_itv = 0;

function goto_top_timer() {
var y = goto_top_type == 1 ? document.documentElement.scrollTop : document.body.scrollTop;

var delta= 0;
var vo = 100; // aenderbar
var bremsweg = document.documentElement.clientHeight*0.5; // aenderbar

if (y > (bremsweg)) { // Konstante verschiebung mit vo
	y = y - vo;
} else { // Verzögern,
	delta = Math.ceil(y*0.3); 
	if (delta < 2) delta = 2; // vmin
	y = y - delta; 
}

//y -= Math.ceil(y * 15 / 100); // originalgleichung
if (y < 0)
y = 0;

if (goto_top_type == 1)
document.documentElement.scrollTop = y;
else
document.body.scrollTop = y;

if (y == 0) {
clearInterval(goto_top_itv);
goto_top_itv = 0;
}
}

function goto_top() {
if (goto_top_itv == 0) {
if (document.documentElement && document.documentElement.scrollTop)
    goto_top_type = 1;
else if (document.body && document.body.scrollTop)
    goto_top_type = 2;
else
    goto_top_type = 0;

if (goto_top_type > 0)
    goto_top_itv = setInterval('goto_top_timer()', 25);
}
}









/***

Advanced scroll functions: Prototype V0.B

**/
var ymax = 0;

function scrolldownonly() {
	ymax = document.documentElement.scrollHeight; //////////////////////////////!!!!!!!!!!!!!!!
	//alert('Ymax=' + ymax);
	preparescroll(0);  //up=0; nur down; up=1: auch up
}

function scrolldownup() {
	ymax = document.documentElement.scrollHeight; ////////////////////////////////!!!!!!!!!!!!!!
	//alert('Ymax=' + ymax);
	preparescroll(1);  //up=0; nur down; up=1: auch up
}

function preparescroll(up) {
if (goto_top_itv == 0) { //up=0; nur down; up=1: auch up
   if (document.documentElement && document.documentElement.scrollTop)
      goto_top_type = 1;
   else if (document.documentElement && document.documentElement.clientHeight)
      goto_top_type = 1;
   else if (document.body && document.body.scrollTop)
      goto_top_type = 2;
   else if (document.body && document.body.clientHeight)
      goto_top_type = 2;
   else
    goto_top_type = 0;

   if (goto_top_type > 0){
   	 //alert('holdrio');
      goto_top_itv = setInterval('scrolldown('+up+')', 25);
	}
   }
}

function scrolldown(up) {  //up=0; nur down; up=1: auch up
var y = goto_top_type == 1 ? document.documentElement.scrollTop : document.body.scrollTop;

var delta= 0;
var vo = 60; // aenderbar
var bremsweg = document.documentElement.clientHeight*0.3; // aenderbar
var restweg = document.documentElement.clientHeight;

if (y == 0) y = 10;

if ((ymax-y) > (bremsweg + restweg)) { // Konstante verschiebung mit vo
	y = y + vo;
	
} else { // Verzögern,
	delta = Math.ceil((ymax-restweg-y)*0.3); 
	if (delta < 2) delta = 2; // vmin
	y = y + delta; 
	//alert('Delta y='+delta);
}

if (goto_top_type == 1)
document.documentElement.scrollTop = y;
else
document.body.scrollTop = y;

if (y > (ymax-document.documentElement.clientHeight)) {
clearInterval(goto_top_itv);
goto_top_itv = 0;
if (up) setTimeout("timehelper()",340); // wait a bit
}
}

function timehelper() {
	goto_top_itv = setInterval('scrollup()', 25);
}

function scrollup() {
var y = goto_top_type == 1 ? document.documentElement.scrollTop : document.body.scrollTop;

var delta= 0;
var vo = 100; // aenderbar
var bremsweg = document.documentElement.clientHeight*0.5; // aenderbar

if (y > (bremsweg)) { // Konstante verschiebung mit vo
	y = y - vo;
	
} else { // Verzögern,
	delta = Math.ceil(y*0.3); 
	if (delta < 2) delta = 2; // vmin
	y = y - delta; 
	//alert('Delta y='+delta);
}

if (y < 0)
y = 0;

if (goto_top_type == 1)
document.documentElement.scrollTop = y;
else
document.body.scrollTop = y;

if (y == 0) {
clearInterval(goto_top_itv);
goto_top_itv = 0;
}
}




function Scrollanchor() {
	ymax = document.documentElement.scrollHeight;
if (goto_top_itv == 0) { 
   if (document.documentElement && document.documentElement.scrollTop)
      goto_top_type = 1;
   else if (document.documentElement && document.documentElement.clientHeight)
      goto_top_type = 1;
   else if (document.body && document.body.scrollTop)
      goto_top_type = 2;
   else if (document.body && document.body.clientHeight)
      goto_top_type = 2;
   else
    goto_top_type = 0;

if (goto_top_type > 0) {	
	var ziely = elmYPosition('testname');
    goto_top_itv = setInterval('Scrollanchor_timer('+ziely+')', 25);
	}
}
}

function Scrollanchor_timer(ziely) {
var y = goto_top_type == 1 ? document.documentElement.scrollTop : document.body.scrollTop;
var delta= 0;
var weg = 0;
var vo = 100; // aenderbar
var bremsweg = document.documentElement.clientHeight*0.5; // aenderbar
var offs = 20;
//alert ("ziely="+ziely+" "+"bremsweg="+bremsweg);

//Richtung
if ((ziely-offs) > y) { // vorwaerts
	// vo = vo;
 } else { //rueckwaerts
 	vo = -vo;
 }
weg = ziely-y-offs;
//alert ("aktuelles_y="+y+" "+"weg="+weg+" "+"vo="+vo);

if (Math.abs(weg) > bremsweg) { // Konstante verschiebung mit vo
	y = y + vo;
	
} else { // Verzögern,
	delta = Math.ceil(weg*0.4);
	if (Math.abs(delta) < 1) {
		if (vo > 0) delta = 1; // vmin
		if (vo < 0) delta = -1;
	}
	y = y + delta; 
	//alert ("delta="+delta);
}

if (goto_top_type == 1)
document.documentElement.scrollTop = y;
else
document.body.scrollTop = y;

if ((Math.abs(y-ziely+offs) <  2) || (y > (ymax-document.documentElement.clientHeight))) { // Anker ganz unten
clearInterval(goto_top_itv);
goto_top_itv = 0;
}
}

function elmYPosition(ename) {
var elm = document.getElementsByName(ename)[0];
var y = elm.offsetTop;
var node = elm;
while (node.offsetParent && node.offsetParent != document.body) {
    node = node.offsetParent;
      y += node.offsetTop;
 } return y;
}

