function time () {
	return Math.round(new Date().getTime()/1000);
}

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 open_window(URL, w, h, title){
	title = (typeof title=="undefined" ? "Bodelschwingh-Haus" : title);

	var InfoWin = window.open(URL,null,"width="+w+",height="+h+",top=0,left=0,screenX=0,screenY=0,resizable=yes,dependent=yes,scrollbars=yes");	
	InfoWin.moveTo(0,0);
	InfoWin.focus();
}

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 fitToWindow(){
	var MIN_HEIGHT = 675;

	var h = parseInt(getWinSize().h);
	h = Math.max(h, MIN_HEIGHT);
	
	var items = [
		{name:"#shadow", delta:0},
		{name:"#shadow_left", delta: - 5},
		{name:"#shadow_right", delta:-5},
		{name:"#bar_left", delta:-5 - 50},
		{name:"#center", delta:-5 - 50},
		{name:"#page", delta:-5 - 50 - 15},
		{name:"#navigation", delta:-5 - 50 - 15},
		{name:"#information", delta:-5 - 50 - 15},
		{name:"#page_content", delta:-5 - 50 - 15 - 122 - 5},
		{name:"#content", delta:-5 - 50 - 15 - 122 - 15},
		{name:"#sub_navigation", delta:-5 - 50 - 15 - 20 - 183 - 82}
	];
	
	for(var i=0; i<items.length; i++){
		$(items[i].name).height(h + items[i].delta);
	}
}
function set_fontsize(){
	document.getElementById("fontsize").href = getFontCss();
	document.getElementById("fontsizehelper").href = getFontCss();
}

function getFontCss(){
	return "css/fontsize"+current_fontsize+".css";
}

function increase_fontsize(){
	if(current_fontsize<2){
		current_fontsize++;
		
		set_fontsize();
		setcookie("bsh_font", current_fontsize, time()+3600);
	}
}

function decrease_fontsize(){
	if(0<current_fontsize){
		current_fontsize--;
		
		set_fontsize();
		setcookie("bsh_font", current_fontsize, time()+3600);
	}
}

var page_image_cnt = 0;
function page_image_fade_in(){
	if(page_image_cnt<4){
		page_image_cnt++;
		$("#page_image_"+page_image_cnt).fadeIn(
			(typeof bsh_animation_speed != "undefined") ? bsh_animation_speed : 1000,
			function(){
				page_image_fade_in();
			}
		);
	}
}

function maximize_quad(nav_item_id){
	var id = quad_id(nav_item_id);
	$(id).animate({
		"width": "20px",
		"height": "20px",
		"top": "-=10px"
	}, 250);
}
function minimize_quad(nav_item_id){
	var id = quad_id(nav_item_id);
	$(id).animate({
		"width": "10px",
		"height": "10px",
		"top": "+=10px"
	}, 250);
}
function goto_page(nav_item_id){
	var href = $("#"+nav_item_id+" .link").attr("href");
	if(typeof href==undefined){
		return;
	}
	location.href = href;
}

function quad_id(nav_item_id){
	var id = "#quad"+nav_item_id.substr(8);
	return ( ( ("#quad_"+current_page) == id ) ? "" : id );
}

function init_events(){
	$('.nav_item').mouseenter(function(){maximize_quad(this.id);});
	$('.nav_item').mouseleave(function(){minimize_quad(this.id);});
	$('.nav_item').click(function(){goto_page(this.id);});
	$('.nav_item_bottom').click(function(){goto_page(this.id);});
	
	$('#video').mouseenter(function(){$(this).attr("src", "bilder/video1_on.jpg")});
	$('#video').mouseleave(function(){$(this).attr("src", "bilder/video1.jpg")});
	$('#video').click(function(){
		play_video();
	});
	$(window).resize(function(){
		fitToWindow();
		$('#video_overlay').css("top", Math.max(0, (getWinSize().h-540)/2));
	});

	$("img[rel*=blockSaveAs]").blockSaveAs({img:"bilder/transparent.gif", parent:"#shadow"});
	$('a[rel=lightbox]').lightBox();
	$("a[rel^='prettyPhoto']").prettyPhoto({theme:'light_rounded'});
}