function getHeight() {
	if( typeof( window.innerHeight ) == 'number' ) {
		Height = window.innerHeight;
	} else if( document.documentElement &&( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		Height = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		Height = document.body.clientHeight;
	} else {
		Height = screen.availHeight;
	}
	return parseInt(Height);
}

var crossobj = false;
var contentheight = 119;
var speed = 2;
var movedownvar = false;
var moveupvar = false;

function optSubNav() {
  if($('#SubNavigation .container ul').size()==0) {return;}
  contentheight = $('#SubNavigation .container ul')[0].offsetHeight;
  if(contentheight <= 129) {return;}
  crossobj = $('#SubNavigation .container ul')[0];
  $('#SubNavigation .container').css({'overflow':'hidden'});
  $(crossobj).css({'marginTop':'0px','position':'relative'});
  var sd = $('<div id="subnavscrolldown"><span>Down</span></div>').appendTo('#SubNavigation');
  var su = $('<div id="subnavscrollup"><span>Up</span></div>').appendTo('#SubNavigation');
  $(sd).mousedown(function(){movedown();}).mouseup(function(){clearTimeout(movedownvar); movedownvar = false;}).mouseover(function(){$(this).addClass('act')}).mouseout(function(){$(this).removeClass('act')});
  $(su).mousedown(function(){moveup();}).mouseup(function(){clearTimeout(moveupvar); moveupvar=false;}).mouseover(function(){$(this).addClass('act')}).mouseout(function(){$(this).removeClass('act')});
  var activeE = $('li.active',crossobj);
  if($(activeE).size()>0) {
    if($(activeE)[0].offsetTop > 0) {
        var o = ($(activeE)[0].offsetTop*-1)+100;
        if(o < 0) {
      	 crossobj.style.marginTop=o+"px";
      	}
    }
  }
}



function movedown(){
	if (moveupvar) {clearTimeout(moveupvar); moveupvar = false;}
	if (parseInt(crossobj.style.marginTop)>=(contentheight*(-1)+100)) {
	 	crossobj.style.marginTop=parseInt(crossobj.style.marginTop)-speed+"px";
	}
	movedownvar=setTimeout("movedown()",20);
}

function moveup(){
	if (movedownvar) {clearTimeout(movedownvar); movedownvar=false;}
	if (parseInt(crossobj.style.marginTop)<=0) {
		crossobj.style.marginTop=parseInt(crossobj.style.marginTop)+speed+"px";
	}
	moveupvar=setTimeout("moveup()",20);
}



function optSize() {
  var h = getHeight();
  if(h<500) {return;}
  
  var container = ($('#Contents').size()>0) ? 'Contents' : 'ContentsWide';
  var cw = (container=='Contents') ? 480 :  710;
  
  cbpos = $("#"+container)[0].offsetTop;
  cfheight = $("#Footer")[0].offsetHeight;
  cfwidth = $("#"+container).css('width');
  
  f = (cbpos+cfheight)+20;
  ch = (parseInt(h)-f);
	$("#"+container).css({'height':ch+'px','width':cw+'px'}).addClass('optCont');
	$('body').css({'overflow':'hidden'});
}

$().ready(function(){
  //adding the print button to footer...
  var print = $('<li id="print"><span><a href="#"><span>Drucken</span></a></span></li>').appendTo('#FooterMenu');
  $(print).click(function(){
     window.print();
     return false;
  });
  //optimize subnavigation...
  optSubNav();
  
  //optimize size contentcontainer...
  optSize();
});

$(window).resize(function(){
  optSize();
});