/***********
 * COOKIES *
 ***********/
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);
}
/*********
 * FONTS *
 *********/
 function set_fontsize(){
	document.getElementById("userfontsize").href = getUserFontCss();
	document.getElementById("fontsize").href = getFontCss();
	document.getElementById("fontsizehelper").href = getFontCss();
}

function getFontCss(){
	return "css/fontsize"+current_fontsize+".css";
}

function getUserFontCss(){
	return "css/user_fontsize"+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);
	}
}
/**************************************
 * Größen dem Browserfenster anpassen *
 **************************************/
 var fitToWindowCallback = [];
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 = 530;
	var HEADER_HEIGHT = 50;

	var s = getWinSize();
	var h = parseInt(s.h);
	h = Math.max(h, MIN_HEIGHT);
	var elements = [
		{id:"#shadow_left", delta:5},
		{id:"#shadow_right", delta:5},
		{id:"#bar_left", delta:HEADER_HEIGHT + 5},
		{id:"#page_content", delta:146 + HEADER_HEIGHT},
		{id:"#sub_navigation", delta:16 + 300 + HEADER_HEIGHT},
		{id:"#information", delta:HEADER_HEIGHT+20},
		{id:"#center", delta:HEADER_HEIGHT+5}
	];

	for(var i=0; i<elements.length; i++){
		$(elements[i].id).height((h - elements[i].delta)+"px");
	}

	
	for(var i=0; i<fitToWindowCallback.length; i++){
		fitToWindowCallback[i]( h );
	}
}
/******************************
 * Animation in der Kopfzeile *
 ******************************/
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();
			}
		);
	}
}
/*********
 * Video *
 *********/
bild1= new Image();
bild1.src= "http://www.emenue.net/homepages/bodelschwingh-haus/images/video1.jpg";
bild2 = new Image();
bild2.src="http://www.emenue.net/homepages/bodelschwingh-haus/images/video1_on.jpg";

function wechsel() {
	window.document.images["video1"].src= bild2.src;
}
function zurueck() {
	window.document.images["video1"].src=bild1.src;
}

function play_video(){
	var s = getWinSize();
	$('#video_overlay').css("top", Math.max(0, (s.h-540)/2));
	
	var c = document.getElementById("video_dummy");
	if (!c) {
		var c = document.createElement("div");
		c.setAttribute("id", "video_dummy");
		document.getElementById("video_content").appendChild(c);

		c.innerHTML = "<div style='width:490px;height:490px;'>\n"+
				"<div style='padding:100px 40px 40px 40px;text-align:center;font-weight:bold;'>\n"+
					"Bestimmte Inhalte dieser Seite können nicht angezeigt werden,\n"+
					"da Ihr Browser momentan kein Adobe Flash unterstützt.\n"+
					"Um die Inhalte darstellen zu können, benötigen Sie das passende Plugin für Ihren Browser installieren.<br/><br/>\n"+
					"Weitere Informationen finden sie unter <a href='http://www.adobe.com/go/getflash'>http://www.adobe.com/go/getflash</a><br/><br/>\n"+
					"<a href='impressum.php'>\n"+
						"Wir übernehmen keine Haftung für die Inhalte, der hier verlinkten externen Seiten!\n"+
						"Informationen zum Haftungsausschluss finden Sie im Impressum.\n"+
					"</a>\n"+
				"</div>\n"+
			"</div>";
	}

	swfobject.embedSWF("flash/video1/videoplayer.swf", "video_dummy", "490", "490", "9.0.0");
	$('#video_overlay').show();
}
function hide_video(){
	$('#video_overlay').hide();
	swfobject.removeSWF("video_dummy");
}
/*******************
 * Spendenprojekte *
 *******************/
var anzahl_spendenprojekte = 0;
function showSpende( id ){
	id = id.substr(7);

	for( var i=1; i<=anzahl_spendenprojekte; i++ )
	{
		if( i != id )
		{
			$("#spende_"+i).slideUp();
			$("#mehr_"+i).show();
		}
	}
	$("#spende_"+id).slideDown();
	$("#mehr_"+id).hide();
}
/************************
 * Neues Fenster öffnen * 
 ************************/
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();
}
/********************
 * Navigation-Quads *
 ********************/
function quad_selector(nav_item_id){
	return selector = "#"+nav_item_id+" .quad";
}
function maximize_quad(nav_item_id){
	var selector = quad_selector(nav_item_id);
	$(selector).animate({
		"width": "20px",
		"height": "20px",
		"top": "-=10px"
	}, 250);
}
function minimize_quad(nav_item_id){
	var selector = quad_selector(nav_item_id);
	$(selector).animate({
		"width": "10px",
		"height": "10px",
		"top": "+=10px"
	}, 250);
}
function goto_page(nav_item_id){
	var href = $("#"+nav_item_id+" a").attr("href");
	if(typeof href==undefined){
		return;
	}
	location.href = href;
}
/********
 * INIT *
 ********/
$(document).ready(function(){
	fitToWindow();
	if(bsh_animation == true){
		page_image_fade_in();
	}

	$('div.spendentext_mehr').each(function(){
		anzahl_spendenprojekte++;
	});	
	$('div.spendentext_mehr').click(function(){
		var id = this.id;
		id = id.substr(5);
		showSpende("spende_"+id);
	});

	$('.main_nav_item').mouseenter(function(){maximize_quad(this.id);});
	$('.main_nav_item').mouseleave(function(){minimize_quad(this.id);});
	$('.main_nav_item').click(function(){goto_page(this.id);});

	$('#video').mouseenter(function(){$(this).attr("src", bild2.src)});
	$('#video').mouseleave(function(){$(this).attr("src", bild1.src)});
	$('#video').click(function(){
		play_video();
	});
	$(window).resize(function(){
		fitToWindow();
		$('#video_overlay').css("top", Math.max(0, (getWinSize().h-540)/2));
	});

	$('a[rel^="prettyPhoto"]').prettyPhoto({theme:'light_rounded', overlay_gallery: false});

	$(".lvl1_active").parent().show();
	$('.lvl0').has('.lvl1_active').each(function(){
		$(this).children('a').each(function(){
			$(this).css('font-weight', 'bold');
		});
	});
	$('.lvl0_active').children('ul').show();
});
