window.onresize = init;
if(document.all) { visible='visible'; hidden='hidden'; } else { visible='show'; hidden='hide'; }

var up, dn, nav;
var timerUp, timerDn, timerSlide;
var windowHeight, contentHeight, offsetHeight;
var ntop, nbot; // номер верхней и нижней отображаемой кнопки

function init()
{
 up = (document.all)? document.all["up"].style : document.layers["up"];
 dn = (document.all)? document.all["dn"].style : document.layers["dn"];
 nav= (document.all)? document.all["nav"].style: document.layers["nav"];
 _tree= (document.all)? document.all["tree"].style: document.layers["tree"];

 contentHeight = (document.all)? document.all["nav"].scrollHeight : nav.document.height;
 windowHeight = (document.all)? document.body.offsetHeight : window.innerHeight;
 offsetHeight = contentHeight - windowHeight;
 treeHeight = (document.all)? document.all["tree"].scrollHeight : _tree.document.height;

 up.top = 0;
 dn.top = windowHeight - 22;
 nav.top = 0;
 updateArrows();
 timerSlide=0;
 if(contentHeight + treeHeight > windowHeight)
 {
 	_tree.top = contentHeight;
 	_tree.bottom = "auto";
 }
 else
 {
 	_tree.top = "auto";
 	_tree.bottom = 0;
 }
}

function updateArrows() {
 up.visibility = firstBut()? visible : hidden;
 dn.visibility = canMoveUp()? visible : hidden;
}

function firstBut () { // номер первой отображаемой кнопки
 var i,x;
 for(i=x=0; x!=-parseInt(nav.top) && i!=nbuts-1; i++) x+=imgon[i].height;
 return i;
}

function canMoveUp () { // можно ли двигать навигатор вверх
 return (-parseInt(nav.top) < offsetHeight)? 1:0;
}

var i, butheight, direction, speed=5;
function slide () {
 var delta = speed;
 if(i < butheight)
 {
  // последний сдвиг с выравниванием по высоте кнопки
  if(i+delta > butheight) delta = butheight - i;
  i += delta;
  nav.top = parseInt(nav.top) + direction*delta;
  updateArrows();
  timerSlide = setTimeout("slide()",1);
 } else {
  clearTimeout(timerSlide);
  timerSlide=0;
 }
}

function ScrollDn() {
 if(timerSlide) return; // без повторных запусков
 if(parseInt(nav.top) >= 0) return;
 i=0; butheight = imgon[firstBut()-1].height; direction = 1; slide();
 timerDn = setTimeout("ScrollDn()",800);
}

function ScrollUp() {
 if(timerSlide) return;
 if(!canMoveUp()) return;
 i=0; butheight = imgon[firstBut()].height; direction = -1; slide();
 timerUp = setTimeout("ScrollUp()",800);
}

function ScrollStop() {
 clearTimeout(timerUp);
 clearTimeout(timerDn);
}

