// JavaScript Document

$(document).ready(function()
{	
	var bodyDefHeight = $("#body_content").height();

	function footerSlideUp() {
		$("#info").hide();
	}
	
	function footerSlideDown() {
		$("#info").slideDown("slow");
	}

	function setBodyHeight() {
		var documentHeight = $(window).height();
		var bodyHeaderHeight = $("#body_header").height();
		var navigationHeight = $("#navigation_container").height();
		documentHeight = documentHeight - bodyHeaderHeight - navigationHeight - 90;
		
		if (getBrowser()==true && getBrowserVersion()>=6.0 && getBrowserVersion()<7.0) {
			documentHeight = documentHeight;
		}
		
		/*if (documentHeight < bodyDefHeight) {
			documentHeight = bodyDefHeight;	
		}*/
		
		//alert("defHeight: "+bodyDefHeight+" newHeight: "+documentHeight);
		
		$("#body_content").animate({
			height: documentHeight						   
		}, "def", function(){
			$("body").css({overflow: "auto"});	
		});
		//$("#body_content").height(documentHeight);
	}
		
	function gallery() {
		var childCounter = 0;
		
		$("#body_content_image > .relative > div").each(function(){
			childCounter = childCounter + 1;														 
		});
		
		if (childCounter>1) {
			$("#body_content_image > .relative > div:not(:first)").hide()
			//$("#body_content_image").append("<div id='gallery_navigation'><img src='fileadmin/template/images/arrow.gif' /></div>");
			$(".body_content_single_image").css({cursor: "pointer"});
			$(".body_content_single_image").click(function(){
				if($(this).next().attr("class")!=undefined) {
					$(this).fadeOut();
					$(this).next().fadeIn();
				} else {
					$(this).fadeOut();
					$(".body_content_single_image:first").fadeIn();
				}		   
			});
		}
	}
	
	function popup(target, type) {		
		$("body").append("<div id='popup'><div id='popup_background'></div><div id='popup_window'><div id='popup_close'><a href='#' class='popup_close_text'>Fenster schlie&szlig;en</a></div><div id='popup_message'></div><div id='popup_content'></div></div></div>");
		$("#popup").hide();
		$("#popup_background").css({opacity: 0});
		$("#popup_window").hide();
		$(".popup_close_text").click(function(){
			$("#popup").fadeOut("def", function(){
				$(this).remove();									
			});
			
			return false;
		});
				
		$("#popup_content").load(target+" #body_content_text > *", function(){			
			switch(type) {
				case "form":
					$("#popup_content > form").submit(function(){
						var valid = true;
						$("#popup_content > form .needed").each(function() {
							if ($(this).attr("value")=="") { valid = false }												 
						});
						
						if (valid == true) {
							var data = $(this).serialize();
							var action = $(this).attr("action");
							
							$("#popup_message").empty();
							$("#popup_content").empty();
							$("#popup_content").html("<div class='loading'><img src='fileadmin/template/images/loading_white.gif' /></div>");
							
							$.ajax({
								type: "POST",
								url: action,
								data: data,
								success: function(result)
								{					
									$("#popup_content").empty();
									$("#popup_content").html("Ihre Nachricht wurde erfolgreich verschickt.")
								}
							});
						} else {
							$("#popup_message").empty();
							$("#popup_message").html("Sie m&uuml;ssen alle gekennzeichneten Felder ausf&uuml;llen.");	
						}
						
						return false;								 
					});
				break;
				case "print":
					$("#druck_link > a").click(function(){
						window.print();
						
						//var druckvorschau = window.open(target, "Druckvorschau", "width=400,height=500, scrollbars=yes");
  						//druckvorschau.print();
						//druckvorschau.close();
						
						return false;									
					});
				break;
			}
			
			$("#popup").show();
			$("#popup_background").animate({
				opacity: 0.7							   
			}, "slow", function(){
				$("#popup_window").fadeIn("slow");	
			});
		});
	}
	
	function ajaxShortcuts() {
		$(".tipafriend").click(function(){
			var target = $(this).attr("href");
			
			popup(target, "form");
			
			return false;								
		});
		
		$(".print").click(function(){
			var target = $(this).attr("href");
			
			popup(target, "print");
			
			return false;					   
		});	
	}
	
	function getBrowser() {
		return jQuery.browser.msie;	
	}
	
	function getBrowserVersion() {
		return jQuery.browser.version;	
	}
	
	function inputFocusGen(element) {
		element.focus(function(){
			$(this).css({backgroundColor: "#e5e5e5"});
		});	
		
		element.blur(function(){
			$(this).css({backgroundColor: "#ffffff"});								   
		});
	} 
	
	inputFocusGen($(".contact_text, .contact_textarea"));
	gallery();
	
	// Inhaltsgröße an Fenstergröße anpassen
	
	setBodyHeight();
	$(window).resize(function(){
		setBodyHeight();						  
	});
	
	// divs beim Start verstecken
	
	var stufe2active = $(".stufe2 > li span").html();
	
	if (jQuery.browser.msie && jQuery.browser.version<7) {
		
	} else {
		/*$(".stufe1sub").not(".stufe1active").bind("mouseenter", function() {
			if (this.opened == false || this.opened == undefined) {
				$(this).parent("li").animate({
					backgroundPosition: 0							 
				},"def",function(){
					this.firstChild.opened = true;
				});
			}																 
		});
		
		$(".stufe1sub").not(".stufe1active").bind("mouseleave", function() {
			if (this.opened == true) {
				$(this).parent("li").animate({
					backgroundPosition: -150							 
				},"def",function(){
					this.firstChild.opened = false;	
				});	
			}																 
		});*/
		
		$(".stufe1sub").not(".stufe1active").hover(function(){
			$(this).parent("li").animate({
				backgroundPosition: 0							 
			},"def");
		},function(){
			$(this).parent("li").animate({
				backgroundPosition: -150							 
			},"def");	
		});
		
		/*$(".stufe1sub").not(".stufe1active").hover(function(){
			//alert(this.opened);
			if (this.opened == false || this.opened == undefined) {
				$(this).parent("li").animate({
					backgroundPosition: 0							 
				},"def",function(){
					this.firstChild.opened = true;
				});
			}
		},function(){
			if (this.opened == true) {
				$(this).parent("li").animate({
					backgroundPosition: -150							 
				},"def",function(){
					this.firstChild.opened = false;	
				});	
			}
		});*/
	}
	/*if (stufe2active!=null) {
		$(".stufe1liactive").css({backgroundPosition: "-150px 0px"});
		$(".stufe1liactive").animate({
			backgroundPosition: 0							 
		});	
	}*/
	
	if (stufe2active==null) {
		$(".stufe2").css({opacity: 0, left:0});
		$(".stufe2").animate({
			opacity: 1,
			left: 175
		});
	}
	
	var stufe3active = $(".stufe3 > li span").html();
	if (stufe3active==null) {
		$(".stufe3").css({opacity: 0, left:0});
		$(".stufe3").animate({
			opacity: 1,
			left: 175
		});
	}
						
	$("#menu_button").css({cursor: "pointer"});
	$("#menu_button").click(function(){
		if ($("#navigation_box").css("display")=="none") {
			$("body").css({overflow: "hidden"});
			$("#navigation_box").slideDown("slow", function(){
				setBodyHeight();											  
			});
		} else {
			$("#navigation_box").slideUp("slow", function(){
				setBodyHeight();											  
			});
		}							 
	});
	
	$("#font_size").append("<div id='font_inc'>A++</div><div id='font_reset'>A</div><div id='font_dec'>A--</div>");
	var old_font_size = 12;
	var new_font_size = old_font_size;
	
	$("#font_inc").click(function(){
		if (new_font_size+1<=14) {
			new_font_size = new_font_size + 1;
			
			$("#body_content_text").animate({
				fontSize : new_font_size								
			});
		}
	});
	
	$("#font_reset").click(function(){
		new_font_size = old_font_size;
									
		$("#body_content_text").animate({
			fontSize : new_font_size								
		});
	});
	
	$("#font_dec").click(function(){
		if (new_font_size-1>=10) {
			new_font_size = new_font_size - 1;
			
			$("#body_content_text").animate({
				fontSize : new_font_size								
			});
		}
	});
	
	if (getBrowser()==false) {
		ajaxShortcuts()
	} else if (getBrowserVersion()!=6.0) {
		ajaxShortcuts()
	}
	
	// Anfahrtskosten-Rechner
	
	$("#stadt").change(function () {
			var preis = this.options[this.selectedIndex].value;
			if (preis!="") {
				preis = "Anfahrtskosten: " + preis;	
			}
			$("#preis-ausgabe").html(preis)

	}).change();
		
	// Footer-Animation
	var infoHeight = $("#info").height();
	var footerHeight = $("#footer").height();
	var footer_var = false;
	
	$("#body_header").css({opacity: 0.75});
	
	if (getBrowserVersion()!=6.0) {
	
	$("#info").show();
	$("#footer").css({bottom: -80});
	
	if (getBrowser()==true) {
		infoHeight = infoHeight + 40;	
	} else {
		infoHeight = infoHeight + 20;	
	}
	
	$("#info_header").bind("mouseenter",function(){
		if (footer_var == false) {
			$("#footer").animate({
				bottom: 0				
			}, "def", function(){
				footer_var = true;	
			});
		}
	});
	$("#footer").bind("mouseleave", function(){
		if (footer_var == true) {
			$(this).animate({
				bottom: -80			
			}, "def", function(){
				footer_var = false;		
			});
		}
	});
	
	/*$("#footer").animate({
		bottom: -infoHeight			 
	}, "def", function(){
		$("#info_header").bind("mouseenter",function(){
			if (footer_var == false) {
				$("#footer").animate({
					bottom: 0				
				}, "def", function(){
					footer_var = true;	
				});
			}
		});
		$("#footer").bind("mouseleave", function(){
			if (footer_var == true) {
				$(this).animate({
					bottom: -infoHeight				
				}, "def", function(){
					footer_var = false;		
				});
			}
		});
	});*/
	
	}
	
	/* newsletter directmail */
	
	/*$("#newsletter-dm .optional:last").before('<div class="show-optional"><a href="">[+] Weitere Angaben</a></div>');
	$("#newsletter-dm .show-optional a").click(function(){
		if (!$("#newsletter-dm .optional").hasClass("open"))
		{
			$("#newsletter-dm .optional").slideDown("def");	
			$("#newsletter-dm .optional").addClass("open");
			$("#newsletter-dm .show-optional a").html("[-] Weitere Angaben");
		} else {
			$("#newsletter-dm .optional").slideUp("def");	
			$("#newsletter-dm .optional").removeClass("open");	
			$("#newsletter-dm .show-optional a").html("[+] Weitere Angaben");
		}
		
		return false;
	});*/
	

	$("#newsletter-dm-form .text-field").focus(function(){
		$(this).css({backgroundColor: "#e5e5e5"});
	});	
	
	$("#newsletter-dm-form .text-field").blur(function(){
		$(this).css({backgroundColor: ""});								   
	});
	
	var newsletter_dm = $("#newsletter-dm-form").validate({
		rules: {
			"FE[tt_address][title]" : "required",
			"FE[tt_address][name]" : "required",
			"FE[tt_address][email]" : "required email"
		},
		messages: {
			"FE[tt_address][title]" : "Bitte wählen Sie eine Anrede.",
			"FE[tt_address][name]" : "Bitte geben Sie Ihren Namen ein.",
			"FE[tt_address][email]": {
				required: "Bitte geben Sie Ihre E-Mail-Adresse ein.",
				email: "Ihre Eingabe muss vom Format name@meinedomain.de sein."
			}
		},
		//errorLabelContainer: "#messageBox",
		//wrapper: "li",
		errorPlacement: function(error, element) {
		error.appendTo(element.parent("div").find(".messageBox"));
		},
		errorClass: "invalid"
	});
	
	/* newsletter old */	
	
	$("#newsletter .eintragen-opt").hide();
	var newsletter ="";
	$("#newsletter input[@name='Action']").click(function(){
		//alert($(this).val());
		switch ($(this).val()) {
			case "delete":
				$("#newsletter .eintragen-opt").fadeOut("def");
				newsletter = "";
				$("#newsletter-form").unbind('submit');
				newsletter = $("#newsletter-form").validate({
				rules: {
					"E-Mail-Adresse": "required email"
				},
				messages: {
					"E-Mail-Adresse": {
						required: "Bitte geben Sie Ihre E-Mail-Adresse ein.",
						email: "Ihre Eingabe muss vom Format name@meinedomain.de sein."
					}
				},
				//errorLabelContainer: "#messageBox",
				//wrapper: "li",
				errorPlacement: function(error, element) {
					error.appendTo(element.parent("div").find(".messageBox"));
				},
				errorClass: "invalid"
			});
			break;
			case "insert":
				$("#newsletter .eintragen-opt").fadeIn("def");
				newsletter = "";
				$("#newsletter-form").unbind('submit');
				newsletter = $("#newsletter-form").validate({
				rules: {
					Name: "required",
					"E-Mail-Adresse": "required email"
				},
				messages: {
					Name: "Bitte geben Sie Ihren Namen ein.",
					"E-Mail-Adresse": {
						required: "Bitte geben Sie Ihre E-Mail-Adresse ein.",
						email: "Ihre Eingabe muss vom Format name@meinedomain.de sein."
					}
				},
				//errorLabelContainer: "#messageBox",
				//wrapper: "li",
				errorPlacement: function(error, element) {
					error.appendTo(element.parent("div").find(".messageBox"));
				},
				errorClass: "invalid"
			});
			break;
		}									 
	});
	
	inputFocusGen($("#baukasten .input-text"));
	$("#baukasten .baukasten-opt").hide();
	$("#baukasten .baukasten-opt-toggle").click(function(){
		if($("#baukasten .baukasten-opt").css("display")=="none"){
			$("#baukasten .baukasten-opt").fadeIn("def");	
		} else {
			$("#baukasten .baukasten-opt").fadeOut("def");		
		}	
		
		return false;
	});
	
	var baukasten = $("#baukasten-form").validate({
		rules: {
			Vorname: "required",
			Name: "required",
			"E-Mail-Adresse": "required email",
			Telefon: "required number"
		},
		messages: {
			Vorname: "Bitte geben Sie Ihren Vornamen ein.",
			Name: "Bitte geben Sie Ihren Namen ein.",
			"E-Mail-Adresse": {
				required: "Bitte geben Sie Ihre E-Mail-Adresse ein.",
				email: "Ihre Eingabe muss vom Format name@meinedomain.de sein."
			},
			Telefon: {
				required: "Bitte geben Sie Ihre Telefonnummer ein.",
				number: "Ihre Eingabe darf nur aus Zahlen bestehen."
			}
		},
		//errorLabelContainer: "#messageBox",
		//wrapper: "li",
		errorPlacement: function(error, element) {
		error.appendTo(element.parent("div").find(".messageBox"));
		},
		errorClass: "invalid"
	});
	
	/*$("#footer").slideDown(1000, function(){
		$("#footer").bind("mouseenter",function(){
			
			//$("#info").slideDown("def");										
		}).bind("mouseleave", function(){
			
			//$("#info").slideUp("def");	
		});
		
		
		//$("#footer").hover(footerSlideDown, footerSlideUp);
	});*/
});
