/*************************/
/*** COOKIE-Funktionen ***/
/*************************/
function setrawcookie (name, value, expires, path, domain, secure) {
	if (expires instanceof Date) {
        expires = expires.toGMTString();
    } else if (typeof(expires) == 'number') {
        expires = (new Date(+(new Date()) + expires * 1e3)).toGMTString();
    }
 
    var r = [name + "=" + value], s={}, i='';
    s = {expires: expires, path: path, domain: domain};
    for (i in s){
        s[i] && r.push(i + "=" + s[i]);
    }
    
    return secure && r.push("secure"), this.window.document.cookie = r.join(";"), true;
}

function setcookie (name, value, expires, path, domain, secure) {
	return this.setrawcookie(name, encodeURIComponent(value), expires, path, domain, secure);
}
function time () {
	return Math.round(new Date().getTime()/1000);
}


/*************************/
/*** Window-Funktionen ***/
/*************************/
function getWinSize(win){
    if(!win)
		win = window;
    var s = new Object();
    if(typeof win.innerWidth != 'undefined')
    {
        s.w = win.innerWidth;
        s.h = win.innerHeight;
    }
    else
    {
         var obj = getBody(win);
         s.w = parseInt(obj.clientWidth);
         s.h = parseInt(obj.clientHeight);
    }
    return s;
}
function getBody(w){
    return (w.document.compatMode && w.document.compatMode == "CSS1Compat") ? w.document.documentElement : w.document.body || null;
}
function convertEm( px ){
	var t = 16;
	if( current_fontsize == 2 ){ // 20px = 1em
		t = 20;
	}else if( current_fontsize == 1 ){ // 18px = 1em
		t = 18;
	}
	return Math.floor(1000*px/t)/1000;
}
function fitToWindow(){
	// Angaben in em
	var MIN_HEIGHT = 48;
	var HEADER_HEIGHT = 3.25;

	var s = getWinSize();
	s.h = convertEm(s.h);
	s.w = convertEm(s.w);
	var h = Math.max(s.h, MIN_HEIGHT) - HEADER_HEIGHT;
	var elements = [
		{name:"outer_height_helper", delta:0},
		{name:"page_content", delta:14.75},
		{name:"subnav", delta:28.25}
	];
	
	for(var i=0; i<elements.length; i++){
		var dom = document.getElementById(elements[i].name);
		if(typeof dom != "undefined"){
			dom.style.height = (h - elements[i].delta)+"em";
		}
	}
}

/**************************/
/*** Schrift-Funktionen ***/
/**************************/
var current_fontsize = 0;

function set_fontsize(){
	document.getElementById("fontsize").href = getFontCss();
	document.getElementById("fontsizehelper").href = getFontCss();
	document.getElementById("userfontsize").href = getUserFontCss();
}

function getFontCss(){
	return "css/font_"+current_fontsize+".css";
}

function getUserFontCss(){
	return "css/user_font_"+current_fontsize+".css";
}

function preloadCss(){
	$("#dummy").load(getFontCss(), false, function(responseText, textStatus, XMLHttpRequest){
		$("#dummy").load(getUserFontCss(), false, function(responseText, textStatus, XMLHttpRequest){
			set_fontsize();
		});
	});
}

function increase_fontsize(){
	if(current_fontsize<2){
		current_fontsize++;
		preloadCss();
		setcookie("bsh_font", current_fontsize, time()+3600);
	}
}

function decrease_fontsize(){
	if(0<current_fontsize){
		current_fontsize--;
		preloadCss();
		setcookie("bsh_font", current_fontsize, time()+3600);
	}
}

/****************************/
/*** Seite initialisierne ***/
/****************************/
$(document).ready(function(){
	$("#font_plus").click(increase_fontsize);
	$("#font_minus").click(decrease_fontsize);
	
	$("#font_plus").mouseover(function(){$(this).css({"background-color":"#eeeeee"});});
	$("#font_plus").mouseout(function(){$(this).css({"background-color":"transparent"});});
	
	$("#font_minus").mouseover(function(){$(this).css({"background-color":"#eeeeee"});});
	$("#font_minus").mouseout(function(){$(this).css({"background-color":"transparent"});});
	
	$("li.lvl1_active").each(function(){
		$(this).parent('ul.subsubnav').css('display', 'block');
	});
	
	$(window).resize(function(){fitToWindow();});
	fitToWindow();
});
