


function position_things () {
	var bg_image_width = 1600;
	var bg_image_height = 1000;
	
	var min_bg_image_width = 400;
	var min_bg_image_height = 250;
	
	/* quer */
	if ($(window).width()/$(window).height()>bg_image_width/bg_image_height) {
		if ( $(window).width() > min_bg_image_width ) {
			$("#theme_pic").width($(window).width());
			$("#theme_pic").height("auto");
		}
		else {
			$("#theme_pic").width(min_bg_image_width);
			$("#theme_pic").height(min_bg_image_height);
		}
	}
	/* hoch */
	else {
		if ( $(window).height() > min_bg_image_height ) {
			$("#theme_pic").height($(window).height());
			$("#theme_pic").width("auto");
		}
		else {
			$("#theme_pic").width(min_bg_image_width);
			$("#theme_pic").height(min_bg_image_height); 
		}
	}
	$("#theme_pic").css("left", 0.5*($(window).width()-$("#theme_pic").width())+"px");
	
	if ($(window).width() < min_bg_image_width) {
		$("#theme_pic").css("left",0.5*($(window).width()-min_bg_image_width)+"px");
	}
	if ($(window).height() < min_bg_image_height) {
		$("#theme_pic").css("top",0.5*($(window).height()-min_bg_image_height)+"px");
	}
	
	for (var counter=0; counter < $(".box").length; counter++) {
		$(".box:eq("+counter+")").css("left", 0.5*($(window).width()-$(".box:eq("+counter+")").outerWidth())+"px");
		var new_top = 0.5*($(window).height()-$(".box:eq("+counter+")").outerHeight());
		var min_top = 30; 
		if ( $(window).width() <= 1200) min_top = 170;
		if (new_top<min_top) new_top = min_top; 
		$(".box:eq("+counter+")").css("top", new_top+"px");
	}
}

var num_themes = 8;
var current_theme = parseInt(((500*Math.random()%num_themes))+1);

function load_theme () {
	$("#theme_pic").fadeOut(3000, function() {
		var random_number = parseInt(((500*Math.random()%num_themes))+1);
		if (random_number == current_theme) random_number = ((random_number+1)%num_themes)+1;
		current_theme = random_number;
		$("#theme_pic").attr("src", "media/theme_pics/theme_pic_"+random_number+".jpg");
	});
}




function toggle_box (box_to_toggle) {
	$(".box:visible").fadeOut(1000);
	$("#box_"+box_to_toggle+":hidden").fadeIn(1000);
}


function reset_empty_fields() {
	for (var counter=0; counter < $("#contactform .formfield").length; counter++ ) {
		var value = $("#contactform .formfield:eq("+counter+")").val();
		var name = $("#contactform .formfield:eq("+counter+")").attr("name");
		if ( value == "" ) $("#contactform .formfield:eq("+counter+")").val(name);
	}
}

function send_form() {
	reset_empty_fields();
	
	var alerts = 0;
	for (var counter=0; counter < $("#contactform .formfield").length; counter++ ) {
		var value = $("#contactform .formfield:eq("+counter+")").val();
		var name = $("#contactform .formfield:eq("+counter+")").attr("name");
		if ( ( value ==  name ) || ( value == "" ) ) {
			$("#contactform .formfield:eq("+counter+")").css("backgroundColor", "#ffc300");
			alerts++;
		}
		else {
			$("#contactform .formfield:eq("+counter+")").css("backgroundColor", "#ffffff");
		}
	}
	$("#box_contact p.alert").remove();
	if (alerts > 0 ) {
		$("#box_contact").prepend("<p class='alert'>Bitte f&uuml;llen Sie alle Felder aus.</p>");
	}
	else {
		var values = decodeURIComponent ( $("#contactform").serialize() );
		$("#box_alerts p").load("send_message.php?"+values);
		toggle_box ("alerts");
	}
}






$(document).ready(function() {
	load_theme();
	
	$("#theme_pic").load(function(){
		position_things();
		$("#theme_pic").fadeIn(3000);
	});
	
	$(".formfield").focus(function() {
		reset_empty_fields();
		if ( $(this).attr("name") == $(this).val() ) {
			$(this).val("");
			$(this).css("backgroundColor", "#ffffff");
		}
	});
	
	$(".icon").mouseenter(function(){
		var tooltip_text = $(this).attr("title");
		$("#tooltip").html(tooltip_text);
		var tooltip_bottom = parseInt($(this).css("bottom"))+($(this).outerHeight()*0.5)-($("#tooltip").outerHeight()*0.5);
		$("#tooltip").css("bottom", tooltip_bottom+"px");
		$("#tooltip").fadeIn(500);
	});
	
	$(".icon").mouseleave(function(){
		$("#tooltip").stop(true,true);
		$("#tooltip").hide();
	});
	
	$("#theme_pic").click(function(){
		reset_empty_fields();
	});
	
	$("a").focus(function(){
		$(this).blur();
	});
	
	position_things();
	setInterval(load_theme, 15000);
});

$(window).resize(position_things);
