var ie=(navigator.appName.indexOf("Explorer") != -1) ? 1:0;
var box_height=306;
var scroller_height;
var text_height;
var outerBox='main';
var innerBox='tbox';
var scroll_min=0;
var scrollerY=0;
var textY;
var mouseY;
// function ermittelt die aktuelle Fenstergrösse sowie die scrollposition
function init(){
  if(document.getElementById(outerBox)){
    box_height=document.getElementById(outerBox).offsetHeight;
    text_height=document.getElementById(innerBox).scrollHeight;
    scroll_min=box_height-text_height;
    if(scroll_min<0){
      document.getElementById('main').innerHTML+='<div id=\"scrollbar\"><a id=\"scroller\"><\/a><div id=\"sct\"></div><div id=\"scb\"><\/div><\/div>';
      //scroller_height=box_height*box_height/text_height;
      scroller_height=box_height/2;
      document.getElementById('scroller').style.height= scroller_height+'px';
      document.getElementById('scroller').onmousedown=startScroll;
      document.getElementById(outerBox).style.overflow='hidden';
      document.getElementById(innerBox).style.overflow='hidden';
      document.getElementById(innerBox).style.height='287px';
    }else if(document.getElementById('submenu')){  // submenu nach unten ausrichten
      document.getElementById('submenu').style.position= 'absolute';
      document.getElementById('submenu').style.left= '0px';
      document.getElementById('submenu').style.bottom= '1.0em';
    }
    // ankernavi abfangen
    if(window.location.hash!=''){
      scrollToAncor( window.location.hash.substring(1));
    }
    //ankerlinks umschreiben
    var pageLocation=window.location.href.split('#')[0];
    for (var i = 0; i < document.links.length; ++i) {
        var locUrl=document.links[i].href.split('#');
        if(locUrl.length>1 && locUrl[0]==pageLocation){
          document.links[i].onclick=scrollToAncor;
        }
    }

    }
}
function startScroll(e){
    mouseY=mouse_pos(e)-scrollerY;
    document.onmouseup=stopScroll;
    //this.onmouseout=stopScroll;
    document.onmousemove=scrollIt;
    return false;
}
function stopScroll(){
    document.onmousemove=null;
    return false;
}

function scrollIt(e){
    var diff=mouse_pos(e)-mouseY;
    if(diff>=0 && diff<=box_height-scroller_height)scrollerY=diff;
    else if(diff<0) scrollerY=0;
    else scrollerY=box_height-scroller_height;
    scrl(scrollerY)
    return false;
}
function scrl(pos){
      document.getElementById('scroller').style.top= pos+'px';
      document.getElementById(innerBox).scrollTop=(pos/(scroller_height-box_height))*scroll_min;
      return false;
}
function scrollToAncor(a){
  if(this.href)a=this.href.split('#')[1];
  var aPos=0
  if(document.anchors[a])aPos=document.anchors[a].offsetTop;
  else aPos=document.all(a).offsetTop;
  if(aPos>-scroll_min)aPos=-scroll_min;
  var pos=-(aPos*(box_height-scroller_height))/scroll_min;
  scrollerY=pos;
  scrl(pos);
  return false;
}
function mouse_pos(e) {
    if(!e) e = window.event;
    var body = (window.document.compatMode && window.document.compatMode == "CSS1Compat") ?
    window.document.documentElement : window.document.body;
    return e.pageY ? e.pageY : e.clientY + body.scrollTop - body.clientTop;
}
window.onload = init;