function inputValueEvent() {
	var inputText = jQuery('#searchpanel input.text').val();
	jQuery('#searchpanel input.text').click(function() {
		if (inputText == "Suche...") {
			jQuery(this).attr("value", "");
		}
	});
	jQuery('#searchpanel input.text').focusin(function() {
		jQuery(this).css("color", "#551285");
	});
	jQuery('#searchpanel input.text').focusout(function() {
		jQuery(this).css("color", "#551285");
	});
	jQuery('#searchpanel input.text').blur(function() {
		if (jQuery(this).val() == "") {
			jQuery(this).attr("value", inputText);
		} else {
			inputText = jQuery(this).val();
			jQuery(this).attr("value", inputText);
		}
	});
}

function plz4Download() {

	jQuery("a.plz4download").each(function() {
		var url = jQuery(this).attr("href");
		jQuery(this).removeAttr("href");
		jQuery(this).attr("rel",url);
		jQuery(this).click(function(){
			clickPDF(this);
		});
	});

	jQuery("#plz4Download").find("form").submit(function() {
		if(handlePLZ4Download(this)){
			var form = $(this);
			$.ajax({
				type: 'POST',
				url: $(this).attr("action"),
				data: $(this).serialize(),
				success: function () {
					var html = '<a target="_blank" class="closeFancyAfterClick" href="' + $(form).find("#url").val() + '" title="">&raquo; Datei öffnen/herunterladen</a>';
					$(form).html(html);
					$(form).find(".closeFancyAfterClick").click(function(){
						$.fancybox.close();
					});
				}
			});
		}
		return false;
	});
}
function handlePLZ4Download(form) {
	var error = "";
	var hasNoError = false;
	var plzField = $(form).find("#plz");
	var plz = plzField.val();

	if (plz != "") {
		if (!isNaN(plz)) {
			if(plz.length == 5) {
				hasNoError = true;
			} else {
				error = "Geben Sie eine fünfstellige PLZ ein.";
			}
		} else {
			error = "Bitte geben Sie nur Zahlen ein.";
		}
	} else {
		error = "Bitte geben Sie Ihre PLZ ein.";
	}

	if (!hasNoError) {
		fieldError(plzField, error);
	}

	return hasNoError;
}
function fieldError(inputfield, message) {
	var errorDiv = $(inputfield).parent().find(".error");
	errorDiv.html(message);
}

function easySlider() {
	/*$("#slider img.wegdamit").css("display","block");
	$("#slider").easySlider({*/
	jQuery("#slider img.wegdamit").css("display","block");
	jQuery("#slider").easySlider({
		auto: true,
		continuous: false,
		controlsShow: false,
		pause: 2400,
		speed: 2200
	});
}

function tooltip4Map() {
	jQuery('#googlemapController select option').tooltip({
		delay: 5,
		fade: 250
	});
	jQuery(document).mousemove(function(e){
		jQuery('#tooltip').css("top",e.pageY+15).css("left",e.pageX+15);
	});
}
function clickPDF(link) {
	var html = $("#plz4Download").clone(true);
	html.removeAttr("style");
	html.find("#url").val($(link).attr("rel"));
	jQuery.fancybox({
		'autoScale' : false,
		'autoDimensions' : false,
		'hideOnOverlayClick' : false,
		'width' : 650,
		'height' : 380,
		'content': html,
		'overlayOpacity' : 0.7,
		'overlayColor' : '#000'
	});
}

function doExternalLinks(){
 var h = window.location.host.toLowerCase();
jQuery("a[href^='http']:not([href^='http://" + h + "']):not([href^='http://www." + h + "'])").attr("target", "_blank");

}

jQuery(document).ready(function() {
	easySlider();
	inputValueEvent();
	plz4Download();
	tooltip4Map();
	doExternalLinks();
});
