
	//----------------------------------------- PUBLIC VARS ------------------------------------------//
	
	var IE7 = false;
	var searchresultsHeadline;
	var partnerTeaserInMove = false;
	var residenceListInMove = false;
	var teaserInterval;
	var teaserInMove = false;
	
	//--------------------------------------- BASIC FUNCTIONS ----------------------------------------//	
	
	function contactFormInit(){
		// Checks if an input is existing
		
		$("form[name='contactform']").submit(function(e){
			var submitForm = true;
			
			$.each($("input[compare], textarea[compare]", this), function(i, obj){
				if(!$(this).val().replace(/ /g, "").length || $(this).val() == $(this).attr("compare") || $(this).val() == $(this).attr("error")){
					$(this).val($(this).attr("error"));
					$(this).css("color", "#e00");
					submitForm = false;
				}
			});
			if(!submitForm){
				e.preventDefault();
			}
		});
	}
	
	function contentInit(){
		// Inits all content type actions
		
		// Imageteaser
		if($(".content .image_wrap .thumbs_wrap .thumbs").length){
			$(".content .image_wrap .thumbs_wrap .thumbs a").click(function(e){
				e.preventDefault();
				//$(".content .image_wrap .image img").attr("src", $(this).attr("image"));
				$(".content .image_wrap .image img").hide();
				$(".content .image_wrap .image img:eq(" + $(this).attr("image") + ")").show();
			});
		}
		
		// Imagegallery
		if($("a[rel='group']").length){
			$("a[rel='group']").lightBox();
		}
		
		// Dropdown-List
		if($(".content .list").length){
			$.each($(".content .list a[class!='hyperlink']"), function(i, obj) {
				if($(this).hasClass("opened") && $(this).next().is("div")){
					$(this).removeClass("opened");
				}
				$(this).next("div").hide();
			});
		}
		$(".content .list > a[class!='hyperlink']").click(function(e){
			e.preventDefault();
			if(!$(this).hasClass("opened") && $(this).next().is("div")){
				if($(this).hasClass("hideothers")){
					$.each($(".content .list a"), function(i, obj) {
						if($(this).hasClass("opened") && $(this).next().is("div")){
							$(this).removeClass("opened");
						}
						$(this).next("div").hide();
					});
				}
				$(this).addClass("opened");
				$(this).next("div").show();
			} else {
				$(this).removeClass("opened");
				$(this).next("div").hide();
			}
		});
	}
	
	function formsInit(){
		// Inits all form event listeners
		$.each($("select"), function(i, obj) {
			$(this).css({
				"paddingLeft"	: "0px",
				"paddingRight"	: "0px"
			});
		});
		$("select").selectmenu();
		$.each($("input[compare], textarea[compare]"), function(i, obj){
			if($(this).attr("compare").length){
				$(this).css("color", "#c0c0c0");
			}
		});
		$("input[type=file]").filestyle({
			image: "fileadmin/img/input_file.png",
			imageheight : 27,
			imagewidth : 119,
			width : 361
		});
		$(".header_nav .right form input[name='input']").autocomplete(cities, {multiple:false});
	}
	
	function fulltextSearchCheck(){
		// Checks if an input is existing
		
		$(".header_nav .right form").submit(function(e){
			if(!$(".header_nav .right form input[name='input']").val().replace(/ /g, "").length ||
			    $(".header_nav .right form input[name='input']").val() == $(".header_nav .right form input[name='input']").attr("compare")){
					e.preventDefault();
					$(".header_nav .right form input[name='input']").val($(".header_nav .right form input[name='input']").attr("compare"));
					$(".header_nav .right form input[name='input']").css("color", "#e00");
			}
		});
	}
	
	function googleMapsInit(){
		// Inits the "Anfahrtsplan"
		
		if($('#anfahrtsplan').length){
			$('#anfahrtsplan').css('display', 'block');
			var geocoder;
			var map;
			var address = $('#anfahrtsplan').attr('address');
			geocoder = new google.maps.Geocoder();
			var latlng = new google.maps.LatLng(-34.397, 150.644);
			var myOptions = {
				zoom: 14,
				center: latlng,
				mapTypeId: google.maps.MapTypeId.ROADMAP
			};
			map = new google.maps.Map(document.getElementById("anfahrtsplan"), myOptions);
			if(geocoder){
				geocoder.geocode({'address': address}, function(results, status){
					if(status == google.maps.GeocoderStatus.OK){
						if(status != google.maps.GeocoderStatus.ZERO_RESULTS){
							map.setCenter(results[0].geometry.location);
							var infowindow = new google.maps.InfoWindow({content: $('#anfahrtsplan').attr('caption')});
							var marker = new google.maps.Marker({position: results[0].geometry.location, map: map, title:address});
							google.maps.event.addListener(marker, 'click', function(){});
						} else {
							alert("Der Anfahrtsplan kann derzeit aufgrund einer Verbindungsstörung zu Google Maps nicht angezeigt werden. Bitte versuchen Sie es in Kürze erneut.");
						}
					} else {
						alert("Der Anfahrtsplan kann derzeit aufgrund einer Verbindungsstörung zu Google Maps nicht angezeigt werden. Bitte versuchen Sie es in Kürze erneut. " + status);
					}
				});
			}
		}
	}
	
	function partnerTeaserInit(){
		// Inits the partner teaser event listeners
		
		$(".partner .left").click(function(e){
			e.preventDefault();
			partnerTeaserMove(true);
		});
		$(".partner .right").click(function(e){
			e.preventDefault();
			partnerTeaserMove(false);
		});
	}
	
	function partnerTeaserMove(prev){
		// Moves the partner teaser
		
		if(!partnerTeaserInMove){
			partnerTeaserInMove = true;
			var currentMargin = parseInt($(".partner .wrap .innerwrap").css("marginLeft"));
			if(currentMargin < 0){currentMargin = currentMargin - (currentMargin * 2);}
			var currentPage = currentMargin / 910;
			var nextMargin;
			var pagesCount = $(".partner .wrap .innerwrap .group").length;
			if(!prev){
				nextMargin = -910 * currentPage - 910;
				if(currentPage + 1 == pagesCount){
					nextMargin = 0;
				}
			} else {
				nextMargin = -910 * currentPage + 910;
				if(currentPage == 0){
					nextMargin = -910 * pagesCount + 910;
				}
			}
			$(".partner .wrap .innerwrap").animate({marginLeft: nextMargin+"px"}, function(){
				partnerTeaserInMove = false;
			});
		}
	}
	
	function replaceInputText(){
		// Checks textfield on enter and leave and replaces content and style if necessary
		
		$("input[type='text'], textarea").focus(function(e){
			if($(this).attr("compare").length){
				if($(this).val() == $(this).attr("compare")){
					$(this).val("");
					$(this).css("color", "#000");
				}
			}
			if($(this).attr("error").length){
				if($(this).val() == $(this).attr("error")){
					$(this).val("");
					$(this).css("color", "#000");
				}
			}
		});
		$("input[type='text'], textarea").blur(function(e){
			if($(this).attr("compare").length){
				if(!$(this).val().replace(/ /g, "").length){
					$(this).val($(this).attr("compare"));
					$(this).css("color", "#b2b2b2");
				}
			}
		});
	}
	
	function residenceSearchInit(){
		// Inits the residence search event listener
		
		searchresultsHeadline = $(".residence_searchresults .right h1").html();
		
		$(".residence_searchbox form").submit(function(e){
			e.preventDefault();
			if($(".residence_searchbox form input[name='residence_searchbox[city]']").val().replace(/ /g, "").length && 
			   $(".residence_searchbox form input[name='residence_searchbox[city]']").val() != $(".residence_searchbox form input[name='residence_searchbox[city]']").attr("compare") &&
			   $(".residence_searchbox form input[name='residence_searchbox[city]']").val() != $(".residence_searchbox form input[name='residence_searchbox[city]']").attr("error")){
				residenceSearchExecute();
			} else {
				$(".residence_searchbox form input[name='residence_searchbox[city]']").val($(".residence_searchbox form input[name='residence_searchbox[city]']").attr("error"));
				$(".residence_searchbox form input[name='residence_searchbox[city]']").css("color", "#e00");
			}
		});
	}
	
	function residenceSearchExecute(){
		// Loads the search results and fades them in
		$(".residence_searchbox .preloader_wrap").show().css("z-index", "1");
		$.ajax({
			url: 'ajax.php',
			cache: false,
			type: 'GET',
			data: 'city='+$(".residence_searchbox form input[name='residence_searchbox[city]']").val()+'&filter='+$(".residence_searchbox form select[name='type']").val()+'&headline='+searchresultsHeadline,
			success: function(msg){
				$(".residence_searchresults").empty();
				$(".residence_searchresults").html(msg);
				$(".residence_searchbox .preloader_wrap").css("z-index", "-1");
				$(".residence_searchbox .preloader_wrap").hide();
				residenceSearchExecuteGoogle();
				residenceSearchAnimate();
			}
		});
	}
	
	function residenceSearchExecuteGoogle(){
		// Activates the google maps api
		var map;
		var latlng = new google.maps.LatLng(-34.397, 150.644);
		var myOptions = {
			zoom: 8,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		map = new google.maps.Map(document.getElementById("googlemaps"), myOptions);
		var centerCoordinatesTag = $(".residence_searchresults .right .results").attr("coordinates");
		centerCoordinatesTag = centerCoordinatesTag.split(";");
		var centerCoordinates = new google.maps.LatLng(centerCoordinatesTag[0], centerCoordinatesTag[1]);
		map.setCenter(centerCoordinates);
		var markerArray = new Array();
		var infoArray = new Array();
		var captionArray = new Array();
		var infoWindow = new google.maps.InfoWindow({content: ''});
		$.each($(".residence_searchresults .right .results .wrap a"), function(i, obj) {
			var caption = '<p class="mapswindow"><b>'+$(this).attr("asylum_name")+'</b><br />'+$(this).attr("maps").substr(0, $(this).attr("maps").length - 4)+'<br /><a class="google_right" href="'+$(this).attr("href")+'" target="_blank">zur Website des Heims</a></p>';
			captionArray[i] = $(this).attr("maps");
			var curCoordinatesTag = $(this).attr("coordinates").split(";");
			var curCoordinates = new google.maps.LatLng(curCoordinatesTag[0], curCoordinatesTag[1]);
			markerArray[i] = new google.maps.Marker({position: curCoordinates, map: map});
			google.maps.event.addListener(markerArray[i], 'click', function(){
				infoWindow.setContent(caption);
				infoWindow.open(map, markerArray[i]);
				$(".mapswindow").parent().css('overflow', 'hidden');
			});
			$(this).click(function(e){						
				e.preventDefault();
				infoWindow.setContent(caption);
				infoWindow.open(map, markerArray[i]);
				$(".mapswindow").parent().css('overflow', 'hidden');
			});
		});

	}
	
	function residenceSearchAnimate(){
		// Fades the search results in
		
		$(".residence_searchbox .preloader_wrap").queue(function(){
			$(".residence_searchresults .right .results .wrap").css("marginTop", "0px");
			$(".residence_searchresults_wrap").fadeIn(650);
			$(".residence_searchbox form a").trigger("click");
			if(IE7){
				$(".residence_searchresults_spacer").animate({height: "351px"}, 650, function(){
					$(".residence_searchresults_wrap").css("z-index", 1);
				});
			} else {
				$(".residence_searchresults_spacer").animate({marginTop: "360px"}, 650, function(){
					$(".residence_searchresults_wrap").css("z-index", 1);
				});
			}
			$(this).dequeue();
		});
	}
	
	function residenceSearchResultInit(){
		// Inits the residence search result event listener
		
		$(".residence_searchresults .right input").live('click', function(e){
			e.preventDefault();
			residenceSearchResultMove();
		});
	}
	
	function residenceSearchResultMove(){
		// Moves the residences search result list
		
		if(!residenceListInMove){
			residenceListInMove = true;
			var currentMargin = parseInt($(".residence_searchresults .right .results .wrap").css("marginTop"));
			if(currentMargin < 0){currentMargin = currentMargin - (currentMargin * 2);}
			var currentPage = currentMargin / 224;
			var linkCount = $(".residence_searchresults .right .results .wrap a").length;
			var nextMargin;
			var pagesCount = linkCount / 8;
			if(pagesCount > Math.floor(pagesCount)){pagesCount = Math.floor(pagesCount) + 1;}
			nextMargin = -224 * currentPage - 224;
			if(currentPage + 1 == pagesCount){
				nextMargin = 0;
			}
			$(".residence_searchresults .right .results .wrap").animate({marginTop: nextMargin+"px"}, function(){
				residenceListInMove = false;
			});
		}
	}
	
	function smoothScroll(){
		$('a[href*=#]').click(function() {
			if(location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') &&
			   location.hostname == this.hostname){
				var $target = $(this.hash);
				$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
				if($target.length){
					var targetOffset = $target.offset().top;
					$('html,body').animate({scrollTop: targetOffset}, 800);
					return false;
				}
			}
		});
	}
	
	function hardScroll(){
		$('a[href*=#]').click(function() {
			if(location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') &&
			   location.hostname == this.hostname){
				var $target = $(this.hash);
				$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
				if($target.length){
					var targetOffset = $target.offset().top;
					$('html,body').scrollTop(targetOffset);
					return false;
				}
			}
		});
	}
	
	function teaserInit(){
		// Adds event listeners to arrows and hides unseen teaser data
		
		$(".teaser .wrap .group .teasertext").fadeTo(0, 0.9);
		$.each($(".teaser .wrap .group"), function(i, obj) {
			if(i + 1 < $(".teaser .wrap .group").length){
				$(this).hide();
			}
		});
		if($(".teaser .wrap .group").length > 1){
			$(".teaser .arrow_left").css("display", "block");
			$(".teaser .arrow_right").css("display", "block");
			$(".teaser .wrap .group .teaserlink").css({
				"left"	: "200px",
				"width"	: "550px"
			});
			teaserLoop(5000);
		}
		$(".teaser .arrow_left").click(function(e){
			e.preventDefault();
			teaserMove(true, 250, true);
		});
		$(".teaser .arrow_right").click(function(e){
			e.preventDefault();
			teaserMove(false, 250, true);
		});
	}
	
	function teaserLoop(interval){
		// Loops teaser movement
		
		teaserInterval = setInterval(function(){teaserMove(false);}, interval);
	}
	
	function teaserMove(prev, speed, button){
		// Shows next or previous teaser data
		
		if(button){clearInterval(teaserInterval);}
		if(!teaserInMove){
			teaserInMove = true;
			var currentGroup = $(".teaser .wrap .group:visible");
			var nextGroup = currentGroup.prev(".group");
			if(prev){nextGroup = currentGroup.next(".group");}
			if(!speed){speed = 750;}
			
			if(!nextGroup.length){
				nextGroup = $(".teaser .wrap .group:last");
				if(prev){nextGroup = $(".teaser .wrap .group:first");}
			}
			
			currentGroup.fadeOut(speed);
			nextGroup.fadeIn(speed, function(){
				teaserInMove = false;
			});
		}
		
	}
		
	//---------------------------------------- INIT FUNCTIONS ----------------------------------------//
	
	$(document).ready(function(){
		// Init function calls
		
		replaceInputText();
		contactFormInit();
		contentInit();
		formsInit();
		fulltextSearchCheck();
		partnerTeaserInit();
		residenceSearchInit();
		residenceSearchResultInit();
		//smoothScroll();
		hardScroll();
		teaserInit();
		googleMapsInit();
	});
