// Typo3 Stuff
function decryptCharcode(n,start,end,offset){n=n+offset;if(offset>0&&n>end){n=start+(n-end-1);}else if(offset<0&&n<start){n=end-(start-n-1);}
return String.fromCharCode(n);}
function decryptString(enc,offset){var dec="";var len=enc.length;for(var i=0;i<len;i++){var n=enc.charCodeAt(i);if(n>=0x2B&&n<=0x3A){dec+=decryptCharcode(n,0x2B,0x3A,offset);}else if(n>=0x40&&n<=0x5A){dec+=decryptCharcode(n,0x40,0x5A,offset);}else if(n>=0x61&&n<=0x7A){dec+=decryptCharcode(n,0x61,0x7A,offset);}else{dec+=enc.charAt(i);}}
return dec;}
function linkTo_UnCryptMailto(s){location.href=decryptString(s,-1);}

//checkbox for powermail
function checkbox(id) {
	var checkid = 'check_'+id;
	var valueid = "value_"+id;
	var checked = document.getElementsByName(checkid)[0].checked;
	var cvalue = document.getElementsByName(valueid)[0].value;

	if(checked == false) {
		document.getElementById(id).value = '';
	}
	else {
		document.getElementById(id).value = cvalue;
	}
}


// jQuery Init
$(document).ready(function() {
	/* INIT */
	$('body').removeClass('nojs');
	$('span.mail').remove(); /* remove spam-protection */

	/* POPUP-VIDEOS */
	$("a[target=popup][href$=html]").fancybox({
		padding				: 10,
		margin				: 40,
		opacity				 : false,
		modal				: false,
		cyclic				: false,
		scrolling			: 'no',	// 'auto', 'yes' or 'no'
		width				: 640,
		height				: 360,
		autoScale			: true,
		autoDimensions		: true,
		centerOnScroll		: false,
		ajax				: {},
		swf					: { wmode: 'transparent' },
		hideOnOverlayClick	: true,
		hideOnContentClick	: false,
		overlayShow			: true,
		overlayOpacity		: 0.7,
		overlayColor		: '#777',
		titleShow			: true,
		titlePosition		: 'float', // 'float', 'outside', 'inside' or 'over'
		titleFormat			: null,
		titleFromAlt		: false,
		transitionIn		: 'fade', // 'elastic', 'fade' or 'none'
		transitionOut		: 'fade', // 'elastic', 'fade' or 'none'
		speedIn				: 300,
		speedOut			: 300,
		changeSpeed			: 300,
		changeFade			: 'fast',
		easingIn			: 'swing',
		easingOut			: 'swing',
		showCloseButton		: true,
		showNavArrows		: true,
		enableEscapeButton	: true,
		enableKeyboardNav	: true,
		onStart				: function(){},
		onCancel			: function(){},
		onComplete			: function(){},
		onCleanup			: function(){},
		onClosed			: function(){},
		onError				: function(){},
		type				: 'iframe'
	});

	/* OTHER POPUPS */
	$("a[target=popup]:not(a[href$=html])").fancybox({});

	/* HEADER */
	var origBgImage = $('#head').css('backgroundImage');
	$('#mainMenu > li:has(img)').hover(function() {
		var bgImage = 'url("' + $('img',this).get(0).src + '")';
		var currentTooltip = $('div.tooltip', this);

		$('#head').css({backgroundImage: bgImage})
		$('#mainMenu div.tooltip').not(currentTooltip).hide();
		currentTooltip.show();
		$('#headContent').hide();
	})
	$('#mainMenu:has(img)').hover(false, function() {
		$('#head').css({backgroundImage: origBgImage})
		$('#mainMenu div.tooltip').hide();
		$('#headContent').show();
	})

	/* FONTSIZES */
	var fontSizes = Array('0.75em','0.95em','1.1em');
	var newFontSize = $.cookie("fontSize");

	if (newFontSize == fontSizes[0] || newFontSize == fontSizes[1] || newFontSize == fontSizes[2]) {
		$('body').css({fontSize: newFontSize});
	}

	$('#left').append('<ul id="size" title="Schriftgröße einstellen"><li class="s1"><a href="#" >A</a></li><li class="s2"><a href="#">A</a></li><li class="s3"><a href="#">A</a></li></ul>');
	$('#size li a').click(function() {
		newFontSize = fontSizes[$('#size li').index(this.parentNode)];
		$('body').css({fontSize: newFontSize});
		$.cookie("fontSize", newFontSize, { path: '/' });
		this.blur();
		return false;
	})

	/* FLAPS */
	$("a.toggle").each(function() {
		var id = this.href.match(/#(.+)$/)[0];
		this.toggleObject = $(id);
		this.toggleObject.hide();
	}).click(function(){
		this.blur();
		this.toggleObject.animate({opacity: 'toggle', height: 'toggle'}, 250);
		$(this).toggleClass("open");
		return false;
	});

	/* INDICATIONS */
	$("a.togglesingle").each(function() {
		var id = this.href.match(/#(.+)$/)[0];
		this.toggleObject = $(id);
		this.toggleObject.hide();
	}).click(function(){
		this.blur();
		this.toggleObject.fadeIn('fast', function() {
			$('html, body').animate({ scrollTop: $('ul.indicationgroups').offset().top - 12 }, 250);
		});
		$(this).addClass('open');
		$("a.togglesingle").not(this).each(function() {
			this.toggleObject.hide();
			$(this).removeClass('open');
		});
		return false;
	});

	/* FORMS */
	$("form").submit(function() {
		var errors = 0;
		$("input.required, textarea.required, checkbox.required").each(function() {
			if(!$(this).val()) {
				showFormError("Bitte füllen Sie alle Pflichtfelder aus.");
				$(this).addClass("requiredEmpty").one('focus', function() {
					$(this).removeClass("requiredEmpty");
				});
				errors++;
			}
		})
		if(errors > 0) return false;
	});
	function showFormError(text) {
		if($("p.error").size() == 0) {
			$("fieldset div:last").before('<p class="error">' + text + '</p>');
			$("p.error").slideDown();
		}
	}

});
