// 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;
	}
}

function cycleDivs(id, speed) {
	var currentObject = $(id + ' div:visible:first');
	var nextObject = currentObject.next().length == 1 ? currentObject.next() : $(id + ' div:first-child');
	if (currentObject != nextObject) {
		nextObject.css({
			zIndex: 110,
			opacity: 0,
			display: 'block'
		}).fadeTo(speed, '1', function(){
			currentObject.css({
				zIndex: 10,
				display: 'none'
			})
			$(this).css({
				zIndex: 100
			})
		});
	}
	return false;
};

// jQuery Init
$(document).ready(function() {

	/* INIT */
	$('body').removeClass('nojs');
	$('span.mail').remove(); /* remove spam-protection */

	if($('#headerImages div').length > 1) {
		window.setInterval("cycleDivs('#headerImages', 'slow')", 3000);
	}

	$('map').each(function() {
		this.mappedImage = $('img[usemap=#' + this.name + ']');
		var container = $('.mapinfo',this.mappedImage.parent());
		$("area", this).each(function() {
			var id = this.href.match(/#(.+)$/)[0];
			this.toggleObject = $(id);
			this.toggleObject.hide().appendTo(container).css({
				left: '-300px',
				opacity: 0,
				display: 'block'
			});
		}).click(function(){
			this.blur();
			$('.block', this.toggleObject.parent()).not(this.toggleObject).animate({
				left: '-300px',
				opacity: 0
			});
			this.toggleObject.animate({
				left: 0,
				opacity: 1
			}, 250);
			$(this).addClass("active");
			return false;
		});
	});

	/*
	$('img[usemap]').maphilight({
		fill: true,
		fillColor: 'ffffff',
		fillOpacity: .2,
		stroke: true,
		strokeColor: '000000',
		strokeOpacity: 1,
		strokeWidth: 2,
		fade: true,
		alwaysOn: false,
		neverOn: false,
		groupBy: false,
		wrapClass: true
	});
	*/

	/* POPUPS */
	$('a[target=popup]').fancybox({});

	/* FORMS */
	function showFormError(text) {
		if($('p.error').size() == 0) {
			$('fieldset div:last').before('<p class="error">' + text + '</p>');
			$('p.error').slideDown();
		}
	}
	$('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;
	});

	$('#bordercontent .block:has(a[href])').click(function() {
		var link = null;
		try {
			link = $('a:first',this).get(0).href;
		}
		catch (e) {}
		var target = $('a:first',this).attr('target')
		if (link) {
			if (target) {
				window.open(link);
			}
			else {
				window.location.href = link;
			}
			return false;
		}
	}).mouseenter(function() {
		$('a', this).addClass('hover');
	}).mouseleave(function() {
		$('a', this).removeClass('hover');
	}).css({
		'cursor': 'pointer'
	});
});
