$.fn.common = function() {
	return this.each(function() {

		$("body").addClass("dom");

		// Lang
		$("form#lang").dropdownmenu();

		// Meta-Navigation
		$("#service li:not(:last-child)").append("<span>|</span>");

		// »Seite drucken«
		$("#footer").append("<p class=\"link-list-item\"><a href=\"javascript:window.print();\">Seite drucken</a></p>");

		// Schnelleinstieg
		$("#content .teaser.schnelleinstieg ul + ul").addClass("dom");

		// Table:Layout
		$('table.border thead th').each(function() {
			if($(this).children('span').length == 0) $(this).wrapInner('<span></span>');
		});

		// Accordion
		$('.accordion').wrapInner('<div style="display: none;"></div>').append('<a href="#">einblenden</a>');
		$('.accordion a').click(function() {
			$(this).prev().animate({height: "toggle", opacity: "toggle"}, function() {
				if($(this).next().text() == 'einblenden') {
					$(this).next().text('ausblenden');
				} else {
					$(this).next().text('einblenden');
				}
			});
			return false;
		});

	});
};

$.fn.searchform = function() {
	return this.each(function() {

		var searchLabel = $("label#label-suchbegriff");
		var searchInput = document.getElementById("suchbegriff");
		var searchValue = searchLabel.text();
		searchInput.value = searchValue;
		searchInput.onfocus = function() {
			if (this.value == searchValue) this.value = "";
		};
		searchInput.onblur = function() {
			if (this.value == "") this.value = searchValue;
		};
		searchInput.form.onsubmit = function() {
			searchInput.onfocus();
		};
		searchLabel.remove();

	});
};

$.fn.form = function() {
	return this.each(function() {

		// :focus
		$(".text input, textarea").focus(
			function() {
				$(this).addClass("focus").prev("label").addClass("focus");
				$(this).parent().prev("label, span.label").addClass("focus");
			}
		).blur(
			function() {
				$(this).removeClass("focus").prev("label").removeClass("focus");
				$(this).parent().prev("label, span.label").removeClass("focus");
			}
		);

		// button:hover
		$("input.button").hover(
			function() { $(this).addClass("hover"); }, function() { $(this).removeClass("hover"); }
		);

	});
};

$.fn.inlineform = function() {
	return this.each(function() {

		$("form.inline").dropdownmenu();

	});
};

$.fn.onlinepetition = function() {
	return this.each(function() {

		$("fieldset#Vertretung").addClass("closed");
		$("input#vertretung").click(
			function() {
				$("fieldset#Vertretung").animate({height: "toggle", opacity: "toggle"}, function() {
					$(this).toggleClass("closed");
				});
			}
		);

	});
};

$.fn.kontaktformular = function() {
	return this.each(function() {

		$("input#kontakt-antwort:not(:checked), input#kontakt-email:not(:checked)").parent().parent().next('li.sub').addClass("closed");
		$("input#kontakt-antwort, input#kontakt-email").click(
			function() {
				$(this).parent().parent().next('li.sub').animate({height: "toggle", opacity: "toggle"}, function() {
					$(this).toggleClass("closed");
				});
			}
		);

	});
};

$.fn.abgeordnetensuche = function() {
	return this.each(function() {
		$("input#abgsuche-wahlkreissuchemodus-adresse:not(:checked)").parents("ol").next().addClass("closed");
		$("input#abgsuche-wahlkreissuchemodus-landkreis:not(:checked)").parents("ol").next().next().addClass("closed");
		$("input#abgsuche-wahlkreissuchemodus-adresse, input#abgsuche-wahlkreissuchemodus-landkreis").click(function() {
			$(this).parents("ol").nextAll("ol").toggleClass("closed");
		});

	});
};

$.fn.toggleval = function() {
	return this.each(function() {

		$("input.toggleval").each(function() {
			var elementId = $(this).attr("id");
			var elementValue = $(this).attr("title");
			if(!($(this).val())) $(this).val(elementValue).addClass('empty');
			$(this).focus(function() {
				if($(this).val() == elementValue) $(this).val('').removeClass('empty');
			}).blur(function() {
				if(!($(this).val())) $(this).val(elementValue).addClass('empty');
			});
			$(this).parents("form").submit(function() {
				$(this).find("input#" + elementId).each(function() {
					if($(this).val() == elementValue) $(this).val('');
				});
			});
			$(this).removeAttr("title");
		});

	});
};

$(document).ready(function() {
	$().common();
	$("label#label-suchbegriff").searchform();
	$("#content form").form();
	$("form.inline").inlineform();
	$("#onlinepetition form").onlinepetition();
	$("#kontaktformular form").kontaktformular();
	$("#abgeordnetensuche form").abgeordnetensuche();
	$("input.toggleval").toggleval();
});

var currContent = null;
var currReq = null;

var AjaxHelper = {
	replaceContainer: function(container_id, url, spinnerText) {
		$("#redner h2").append(" <img src=\"/assets/slt/gen/img/bas/loading.gif\" alt=\"("+spinnerText+")\" class=\"busy\" />");

		if ( currReq ) currReq.abort();

		currReq = $.post(url, function(response) {
			if(response!=currContent) {
				$("#" + container_id).replaceWith(response);
				currContent = response;
			}
			$("#redner h2 img.busy").remove();
			$("#vod").each(function() {
				dynamicTabs();
			});
			$("#abgeordnetensuche form").each(function() {
				$(this).abgeordnetensuche();
			});
		});
	}
};

AjaxHelper.replaceContainer2 = function(container_id, url, spinnerText, data, allowMultipleRequests, readyFct) {
	$("#redner h2").append(" <img src=\"/assets/slt/gen/img/bas/loading.gif\" alt=\"("+spinnerText+")\" class=\"busy\" />");

	if ( currReq && allowMultipleRequests!=true) currReq.abort();

	currReq = $.post(url, data, function(response) {
		//if(response!=currContent) {
			$("#" + container_id).replaceWith(response);
			currContent = response;
		//}
		$("#redner h2 img.busy").remove();
		$("#abgeordnetensuche form").each(function() {
			$(this).abgeordnetensuche();
		});
		if(readyFct!=null)
		readyFct();
	});
};

