if (window.location.hash) {
 var baseurl=location.href.split(/#/);
 window.location.href=baseurl[0];
}

var is_dom = document.getElementById? 1:0;
var is_agt = navigator.userAgent.toLowerCase();
var is_ie  = ((is_agt.indexOf("msie") != -1) && (is_agt.indexOf("opera") == -1));
var heightTxt = 0;
var heightScreen = 0;
var heightScroller = 0;
var heightTxtBox = 0;
var widthScreen = 0;
var topTxt = 0;
var topImg = 0;
var topScr = 20;
var leftTxt = 0;
var widthTxt = 0;
var widthTxtBox = 0;
var heightImg = 914;
var timerId = 0;
var scrollTop = 0;
var mouseX = 0;
var mouseY = 0;
var lastMouseY = 0;
var draggingScroller=false;

function cancelerrors(){return true};
window.onerror = cancelerrors;

function setDimensions() {
  heightTxt = document.getElementById("cmscontent").offsetHeight;
  widthTxt = document.getElementById("cmscontentwrapper").scrollWidth;
  heightScreen = document.documentElement.clientHeight;
  heightTxtBox = document.getElementById("cmscontentwrapper").offsetHeight;
  widthTxtBox = document.getElementById("cmscontentwrapper").offsetWidth;
  widthScreen = document.documentElement.clientWidth;
  if (heightImg - heightScreen > heightTxt - heightTxtBox) {
   heightScroller = Math.floor((heightScreen-40)*heightScreen/heightImg);
   if (heightScreen>heightImg) heightScroller = heightScreen-40;
  } else {
   heightScroller = Math.floor((heightScreen-40)*heightTxtBox/heightTxt);
   if (heightTxtBox>heightTxt) heightScroller = heightScreen-40;
  }
  document.getElementById('wrapper').style.height=heightScreen + 'px';
  document.getElementById('cmscontentwrapper').style.width=(widthScreen-446) + 'px';
  document.getElementById('topcenter').style.width=(widthScreen-570) + 'px';
  document.getElementById('bottomcenter').style.width=(widthScreen-466) + 'px';
  document.getElementById('scroller').style.height=heightScroller + 'px';
  document.getElementById('scrollerstripe').style.height=(heightScreen-40) + 'px';
  document.getElementById('contentmarkup').style.top=(topImg+110) + 'px';
  document.getElementById('contentmarkup').style.left=(widthScreen/2-10) + 'px';
  document.getElementById('contentmarkdown').style.top=(topImg+730) + 'px';
  document.getElementById('contentmarkdown').style.left=(widthScreen/2-10) + 'px';
  document.getElementById('contentmarkleft').style.top=(topImg+200) + 'px';
  document.getElementById('contentmarkleft').style.left=(190) + 'px';
  document.getElementById('contentmarkright').style.top=(topImg+200) + 'px';
  document.getElementById('contentmarkright').style.left=(widthScreen-220) + 'px';
}

function onResize() {
  setDimensions();
  scrollDown(false,0);
  scrollUp(false,0);
  widthTxt = document.getElementById("cmscontentwrapper").scrollWidth;
  widthTxtBox = document.getElementById("cmscontentwrapper").offsetWidth;
  scrollMoveLeftRight(0);
}

function scrollUp(userScrolls,customScroll) {
 if (userScrolls) scrollStop();
 var scrollBy = customScroll ? customScroll : 10;
 if ((topTxt+1<=0 || topImg+1<=0)
 && (heightScreen<heightImg || heightTxtBox<heightTxt)) {
  if (heightImg - heightScreen > heightTxt - heightTxtBox) {
   topImg = topImg + scrollBy;
   if (topImg>0) topImg=0;
   var rate = topImg/(heightScreen-heightImg);
   if (heightTxtBox>=heightTxt) topTxt=0;
   else topTxt = Math.floor(rate*(heightTxtBox-heightTxt));
  } else {
   topTxt = topTxt + scrollBy;
   if (topTxt>0) topTxt=0;
   var rate = topTxt/(heightTxtBox-heightTxt);
   if (heightScreen>=heightImg) topImg=0;
   else topImg = Math.floor(rate*(heightScreen-heightImg));
  }
  topScr = Math.floor(rate*(heightScreen-40-heightScroller))+20;
  scrollMove(topTxt,topImg,topScr);
  if (userScrolls && timerId==0) timerId = setInterval("scrollUp(false)",20);
 } else scrollStop();
}

function scrollDown(userScrolls,customScroll) {
 if (userScrolls) scrollStop();
 var scrollBy = customScroll ? customScroll : 10;
 if ((topTxt-1>=heightTxtBox-heightTxt || topImg-1>=heightScreen-heightImg)
 && (heightScreen<heightImg || heightTxtBox<heightTxt)) {
  if (heightImg - heightScreen > heightTxt - heightTxtBox) {
   topImg = topImg - scrollBy;
   if (topImg<heightScreen-heightImg) topImg=heightScreen-heightImg;
   var rate = topImg/(heightScreen-heightImg);
   if (heightTxtBox>=heightTxt) topTxt=0;
   else topTxt = Math.floor(rate*(heightTxtBox-heightTxt));
  } else {
   topTxt = topTxt - scrollBy;
   if (topTxt<heightTxtBox-heightTxt) topTxt=heightTxtBox-heightTxt;
   var rate = topTxt/(heightTxtBox-heightTxt);
   if (heightScreen>=heightImg) topImg=0;
   else topImg = Math.floor(rate*(heightScreen-heightImg));
  }
  topScr = Math.floor(rate*(heightScreen-40-heightScroller))+20;
  scrollMove(topTxt,topImg,topScr);
  if (userScrolls && timerId==0) timerId = setInterval("scrollDown(false)",20);
 } else scrollStop();
}

function scrollDrag() {
 if (mouseY>topScr && mouseY<topScr+heightScroller) draggingScroller=true;
 else if (mouseY<topScr) scrollUp(false,300);
 else scrollDown(false,300);
 lastMouseY=mouseY;
}

function scrollStop() {
 clearInterval(timerId);
 timerId=0;
 draggingScroller=false;
}

function scrollMove(y,x,s) {
 document.getElementById('scroller').style.top=s + 'px';
 document.getElementById('cmscontent').style.top = y + 'px';
 document.getElementById('cmscontentwrapper').style.top = (x + 140) + 'px';
 document.getElementById('headleft').style.top = x + 'px';
 document.getElementById('headcenter').style.top = x + 'px';
 document.getElementById('headright').style.top = x + 'px';
 document.getElementById('topleft').style.top = (x + 15) + 'px';
 document.getElementById('topcenter').style.top = (x + 15) + 'px';
 document.getElementById('topright').style.top = (x + 15) + 'px';
 document.getElementById('centerleft').style.top = (x + 148) + 'px';
 document.getElementById('centercenter').style.top = (x + 148) + 'px';
 document.getElementById('centerright').style.top = (x + 148) + 'px';
 document.getElementById('bottomleft').style.top = (x + 765) + 'px';
 document.getElementById('bottomcenter').style.top = (x + 765) + 'px';
 document.getElementById('bottomright').style.top = (x + 765) + 'px';
 document.getElementById('footleft').style.top = (x + 879) + 'px';
 document.getElementById('footcenter').style.top = (x + 879) + 'px';
 document.getElementById('footright').style.top = (x + 879) + 'px';
 document.getElementById('contentmarkup').style.top=(x+120) + 'px';
 document.getElementById('contentmarkdown').style.top=(x+740) + 'px';
 var up='hidden';
 var down='hidden';
 if (heightTxtBox<heightTxt) {
  var up='visible';
  var down='visible';
  if (s==20) up='hidden';
  if (s==(heightScreen-40-heightScroller+20)) down='hidden';
 }
 document.getElementById('contentmarkup').style.visibility=up;
 document.getElementById('contentmarkdown').style.visibility=down;
//test:
//  document.getElementById("q").value = s +':'+heightScreen +':'+heightScroller;
}

function scrollMoveLeftRight(x) {
 document.getElementById('cmscontent').style.left = x + 'px';
 var left='hidden';
 var right='hidden';
 if (widthTxtBox<widthTxt) {
  var left='visible';
  var right='visible';
  if (x==0) left='hidden';
  if (x==widthTxtBox-widthTxt) right='hidden';
 }
 document.getElementById('contentmarkleft').style.visibility=left;
 document.getElementById('contentmarkright').style.visibility=right;
}


function scrolltoLeft(userScrolls) {
 if (userScrolls) scrollStop();
 var moveBy = 10;
 if (leftTxt+1<=0 && widthTxt>widthTxtBox) {
  leftTxt = leftTxt + moveBy;
  if (leftTxt>0) leftTxt=0;
  scrollMoveLeftRight(leftTxt);
  if (userScrolls && timerId==0) timerId = setInterval("scrolltoLeft(false)",20);
 } else scrollStop();
}

function scrolltoRight (userScrolls) {
 if (userScrolls) scrollStop();
 var moveBy = 10;
 if (leftTxt-1>=(widthTxtBox-widthTxt) && widthTxt>widthTxtBox) {
  leftTxt = leftTxt - moveBy;
  if (leftTxt<(widthTxtBox-widthTxt)) leftTxt=widthTxtBox-widthTxt;
  scrollMoveLeftRight(leftTxt);
  if (userScrolls && timerId==0) timerId = setInterval("scrolltoRight(false)",20);
 } else scrollStop();
}

 function handleKeys (evt) {
  var e = (is_ie) ? event.keyCode : evt.keyCode;
  var cancel=false;
  switch (e) {
   case 33: // pageup
    scrollUp(false,300);
    cancel=true;
    break;
   case 34: // pagedown
    scrollDown(false,300);
    cancel=true;
    break;
   case 38: //up arrow
    scrollUp(false,10);
    cancel=true;
    break;
   case 40: //down arrow
    scrollDown(false,10);
    cancel=true;
    break;
   case 37: //left arrow
    scrolltoLeft(false);
    cancel=true;
    break;
   case 39: //right arrow
    scrolltoRight(false);
    cancel=true;
    break;
   case 36: //home
    scrollUp(false,10000);
    cancel=true;
    break;
   case 35: //end
    scrollDown(false,10000);
    cancel=true;
    break;
  }
  if (cancel) {
   if (is_ie) event.returnValue = false;
   else evt.stopPropagation();
   return false;
  }
}

 if (is_ie) document.onkeydown = handleKeys;
 else document.onkeypress = handleKeys;

 document.onmousemove = function (evt) {
  if (is_ie) {mouseX=event.x; mouseY=event.y;}
  else {mouseX=evt.clientX; mouseY=evt.clientY;}
  if(!draggingScroller) return;
  var scrollBy = (heightImg - heightScreen > heightTxt - heightTxtBox)
   ? heightImg*(lastMouseY-mouseY)/(heightScreen-40-heightScroller)
   : heightTxt*(lastMouseY-mouseY)/(heightScreen-40-heightScroller);
  if (lastMouseY > mouseY) scrollUp(false,scrollBy);
  else scrollDown(false,-scrollBy);
  lastMouseY=mouseY;
  return false;
 }

 document.onmousewheel = function onMouseWheel() {
  if (event.wheelDelta >= 120) scrollUp(false,30);
  else if (event.wheelDelta <= -120) scrollDown(false,30);
 }

 //replace default action for anchorlinks
 document.onclick = function (evt) {
  var e = (is_ie) ? event.srcElement : evt.target;
  if (e.tagName == "A" && (e.href.charAt(0)=='#'
  || e.href.substring(0,e.href.indexOf('#'))==location.href)) {
   if (is_ie) event.returnValue = false;
   else evt.stopPropagation();
   var found=0;
   var anchorname=e.href.substring(e.href.indexOf('#')+1);
   for (var i = document.anchors.length; --i >= 0;)
    if(document.anchors[i].name==anchorname){found=1;break;}
   if (found==1) {
    scrollUp(false,10000);
    scrollDown(false,findPosY(document.anchors[i]));
   }
  }
 }

 function findPosY(obj) {
  var curtop = 0;
  if (obj.offsetParent) {
   while (obj.offsetParent) {
    curtop += obj.offsetTop
    obj = obj.offsetParent;
   }
  } else if (obj.y) curtop += obj.y;
  return curtop;
 }
