(function ($) {
	// VERTICALLY ALIGN FUNCTION
	$.fn.vAlign = function() {
		return this.each(function(i){
		var ah = $(this).height();
		var ph = $(this).parent().height();
		var mh = (ph - ah) / 2;
		$(this).css('margin-top', mh);
	});
};
})(jQuery);

jQuery('document').ready(function(){
	
	var windowHeight = jQuery(window).height();

	jQuery(".button-link[rel]").overlay({
		mask: {
			color: '#4b4643',
			opacity: 0.9
		},
		left : 'center',
		top: 'center',
		onClose: function() {
			jQuery('.error').css({
				'visibility' : 'hidden'
			});
		}
	});
	
	jQuery('.columnize .button-link:first').css({
		'margin-top': 15								 
	});

	jQuery("#flowtabs li:first a").addClass("current");
	jQuery('.columnize ul').addClass("dontsplit");
	if (jQuery(".columnize ul, .columnize ol").length > 0){
		jQuery('.columnize').columnize({ columns: 2});
	}else{
		jQuery('.columnize').columnize({ columns: 2, lastNeverTallest: 1});
	}
	
	

	jQuery("#plus_lefttop").animate({left: '+=149', top: '+=153'}, 2000);

	jQuery("#plus_rightbottom").animate({left: '-=155', top: '-=149'}, 2000, function() { jQuery(this).html('<img src="fileadmin/templates/images/plus_red_bottomright.gif" id="plus_red" style="display:none"/>');jQuery('#plus_red').fadeIn(6000);});

	jQuery("#footer .footer_box:first").addClass("footer_box_first");
	jQuery("#footer .footer_box:last").addClass("footer_box_last");

	jQuery("#flowpanes").scrollable({ vertical: true, speed: 700, circular: true, mousewheel: true }).navigator({
		navi: "#flowtabs",
		naviItem: 'a',
		activeClass: 'current'
	});

	jQuery('.footer_box a').vAlign();
	jQuery('#flowtabs li a').vAlign();
	jQuery('.tx_dfardourptc_pi1_selectables_element p, .tx_dfardourptc_pi1_selectables_element h4').vAlign();

	jQuery('.tx_powermail_pi1_fieldwrap_html label').each(function(){
		var labelColor = '#999';
		var restingPosition = '5px';
		// style the label with JS for progressive enhancement
		jQuery(this).css({
			'color' : labelColor,
				'position' : 'absolute',
				'top' : '6px',
				'left' : restingPosition,
				'display' : 'inline',
				'z-index' : '99'
		});

		// grab the input value
		var inputval = jQuery(this).next('input,textarea,select').val();

		// grab the label width, then add 5 pixels to it
		var labelwidth = jQuery(this).width();
		var labelmove = labelwidth + 5;

		//onload, check if a field is filled out, if so, move the label out of the way
		if(inputval !== ''){
			jQuery(this).stop().animate({ 'left':'-'+labelmove }, 1);
		}

		// if the input is empty on focus move the label to the left
		// if it's empty on blur, move it back
		jQuery('input,textarea,select').focus(function(){
			var label = jQuery(this).prev('label');
			var width = jQuery(label).width();
			var adjust = width + 5;
			var value = jQuery(this).val();
			if(value == ''){
				label.stop().animate({ 'left':'-'+adjust }, 'fast');
			} else {
				label.css({ 'left':'-'+adjust });
			}
		}).blur(function(){
			var label = jQuery(this).prev('label');
			var value = jQuery(this).val();
			if(value == ''){
				label.stop().animate({ 'left':restingPosition }, 'fast');
			}
		});
	});
	
	jQuery('#ajax-form, #ajax-form1').submit(function() {
	  		
			var currentID = jQuery(this).attr('id');
			jQuery.ajax({
				url: jQuery(this).attr('action'),
				cache: false,
				data: jQuery(this).serialize(),
				type: 'POST',
				success: function(data) {
					jQuery('#'+currentID).parents('.tx-powermail-pi1').empty().append(jQuery(".tx-powermail-pi1_thx", data).html());
				},
				error: function(){
					alert("Bei der Übermittlung des Formulars ist ein Fehler aufgetreten. Bitte versuchen Sie es erneut oder nehmen Sie direkt Kontakt zu uns auf. Alle Informationen finden Sie über die Seite 'Kontakt' in der Navigation.");
				}
			});
	  		return false;
	});



});


