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

/*
var cf = new ContentFlow('contentFlow', {

    loadingTimeout: 30000, // milliseconds
    activeElement: 'item',       // item or content

    maxItemHeight: 250,               // 0 == auto, >0 max item height in px
    scaleFactor: 1.0,               // overall scale factor of content
    scaleFactorLandscape: 0.9,     // scale factor of landscape images ('max' := height= maxItemHeight)
    scaleFactorPortrait: 1.0,       // scale factor of portraoit and square images ('max' := width = item width)
    fixItemSize: !isIE,             // don't scale item size to fit image, crop image if bigger than item
    relativeItemPosition: "top center", // align top/above, bottom/below, left, right, center of position coordinate

    circularFlow: true,             // should the flow wrap around at begging and end?
    verticalFlow: false,            // turn ContentFlow 90 degree counterclockwise
    visibleItems: 3,               // how man item are visible on each side (-1 := auto)
    endOpacity: 1,                  // opacity of last visible item on both sides
    startItem:  STARTITEM,           // which item should be shown on startup?
    scrollInFrom: "pre",            // from where should be scrolled in?

    flowSpeedFactor: 1.2,           // how fast should it scroll?
    flowDragFriction: 1.0,          // how hard should it be be drag the floe (0 := no dragging)
    scrollWheelSpeed: 1.0,          // how fast should the mouse wheel scroll. nagive values will revers the scroll direction (0:= deactivate mouse wheel)
    keys: {                         // key => function definition, if set to {} keys ar deactivated
        13: function () { this.conf.onclickActiveItem(this._activeItem) },
        37: function () { this.moveTo('pre') },
        38: function () { this.moveTo('visibleNext') },
        39: function () { this.moveTo('next') },
        40: function () { this.moveTo('visiblePre') }
    },

    reflectionColor: "#FFFFFF", // none, transparent, overlay or hex RGB CSS style #RRGGBB
    reflectionHeight: 0.2,          // float (relative to original image height)
    reflectionGap: 0             // gap between the image and the reflection

});

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

	$("img.reflect").reflect();

	$(".ContentFlow").append('<a class="prev browse"></a><a class="next browse"></a>');
	$(".ContentFlow a.prev").click(function() {
		cf.moveTo('left');
	});
	$(".ContentFlow a.next").click(function() {
		cf.moveTo('right');
	});

	$("#titlelist").css({
		height: "0",
		padding: "0",
		border: "0",
		overflow: "hidden"
	});
	$(".opentitles").toggle(
		function() {
			var anchor = $($(this).attr("hash"));
			anchor.css({
				overflowY: "auto",
				padding: "2px 0 2px 0",
				borderBottom: "7px solid #eef0f1"
			});
			anchor.animate(
					{
						height: "400px"
					},
					400
			);
			this.blur();
		},
		function() {
			var anchor = $($(this).attr("hash"));
			anchor.css({
				overflowY: "hidden",
				padding: "0",
				border: "0"
			});
			anchor.animate(
					{
						height: "0"
					},
					400
			);
			this.blur();
		}
	);

	$("a[target=popup]").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'
	});

	$(".scroller").append('<a class="prev browse"></a><a class="next browse"></a>');
	$(".scrollable").scrollable( {
		activeClass: 'active',
		circular: false,
		clonedClass: 'cloned',
		disabledClass: 'disabled',
		easing: 'linear',
		initialIndex: 0,
		item: null,
		items: '.items',
		keyboard: true,
		mousewheel: true,
		next: '.scroller .next',
		prev: '.scroller .prev',
		speed: 400,
		vertical: false,
		wheelSpeed: 400,
		onSeek: false
	});


	$("a.ajax").click(function() {
		var anchor = this;
		var link = anchor.href + ((anchor.href.indexOf('?') == -1) ? '?' : '&') + 'type=41';
		this.blur();
		$("#ajaxtarget").addClass("loading");
		$("#ajaxtarget .content").load(link, function(response, status, xhr) {
			$("#ajaxtarget").removeClass("loading");
			if (status == "error") {
				window.location.href = anchor.href;
			}
		});
		return false;
	});
	$("a.ajax:first").click();

	$("li.print").append('<a class="arrfirst" onclick="window.print(); return false" href="#">Drucken</a>');
	$(".notepad p.print").append('<a onclick="window.print(); return false" href="#">Notizzettel drucken</a>');
	
	/* FLAPS */
	$('.uploads h1').click(function() {
		$('ul.uploads', $(this).parents('.uploads')).toggle('fast');
		$(this).toggleClass('active');
	}).css({
		cursor: 'pointer'
	});
	$('ul.uploads').hide();

	/* EQUAL HEIGHTS */
	var max_height = 0;
	var max_baseline = 0;
	var equal_elements = '#left>.block:first-child, #right .linkbuttons';
	$(equal_elements).each(function(){
		var offset = $(this).offset();
		var height = $(this).outerHeight(false);
	    if ( height > max_height) { max_height = height; }
	    if ( offset.top > max_baseline) { max_baseline = offset.top; }
	}).each(function(){
		var border_and_padding = ($(this).outerHeight(false) - $(this).height());
		var offset = $(this).offset();
		var min_height = max_height - border_and_padding;
		var adjustment = max_baseline - offset.top;
		if(max_height + adjustment > $(this).outerHeight(false) && !((max_height + adjustment) * 3/4 > $(this).outerHeight(false))) {
			$(this).css({minHeight: min_height + adjustment});
		}
	});

	/* 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();
		}
	}

});
