$(document).ready(function(){
	jQuery.fx.off = false;
	
	// paste in dom-inits here
//accordion
	$(".accordion").simpleAccordion();
	

		$(function(){
		$('a.d3').hover(function() { //mouse in
 			$(this).stop().animate({ backgroundPosition: '0' }, 400);
				}, function() { //mouse out
 			$(this).stop().animate({ backgroundPosition: '-290px' }, 500);
		}); 			
	});
	

 

	
	$(function(){
    $('#navi>li>ul').hide();
    $('#navi>li').hover(function(){
        // check that the menu is not currently animated
        if ($('#navi ul:animated').size() == 0) {
            // create a reference to the active element (this)
            // so we don't have to keep creating a jQuery object
            $heading = $(this);
            // create a reference to visible sibling elements
            // so we don't have to keep creating a jQuery object
            $expandedSiblings = $heading.siblings().find('ul:visible');
            if ($expandedSiblings.size() > 0) {
                $expandedSiblings.slideUp(0, function(){
                    $heading.find('ul').slideDown(0);
                });
            }
            else {
                $heading.find('ul').slideDown(0);
            }
        }
    },
	 function(){
	 	$('#navi>li>ul').hide();
	 });
});
 
 $(function(){
    $('#navi>li>ul>li>ul').hide();
    $('#navi>li>ul>li').mouseover(function(){
        // check that the menu is not currently animated
        if ($('#navi ul:animated').size() == 0) {
            // create a reference to the active element (this)
            // so we don't have to keep creating a jQuery object
            $heading = $(this);
            // create a reference to visible sibling elements
            // so we don't have to keep creating a jQuery object
            $expandedSiblings = $heading.siblings().find('ul:visible');
            if ($expandedSiblings.size() > 0) {
                $expandedSiblings.slideUp(0, function(){
                    $heading.find('ul').slideDown(500);
                });
            }
            else {
                $heading.find('ul').slideDown(0);
            }
        }
    });
});

	// cms lightbox to fancybox
	$("a[rel*='lightbox[']").each(function(){
		var rel = $(this).attr("rel").split('[');
		$(this).attr("rel", rel[0]);
		var rev = rel[1].split(']');
		$(this).attr("rev", rev[0]);
	});
	
	// fancybox
	$("a[rel=lightbox]").fancybox({'type': 'image', titlePosition: 'over', 'padding': 0});
	$("a[rel=lightbox_pixaround]").fancybox();
	$("a[rel=lightbox_iframe]").fancybox({
		'width'				: '75%',
		'height'			: '75%',
		'autoScale'			: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe'
	});
	
	// inputfill
    $(".inputfill").focus(function(){if ($(this).val() == $(this).attr("alt")) $(this).val("");
    }).blur(function(){if ($(this).val() == '') $(this).val($(this).attr("alt"));});
	
	// noSpam
	$("a.escape").each(function(){
		var el = $(this);
		var p = el.text().split('∂');
		if (el.attr('rel') != '') el.attr('href', 'mailto:'+p[0]+'@'+el.attr('rel'));
		else el.attr('href', 'mailto:'+p[0]+'@'+p[1]);
		el.html(p[0]+'@'+p[1]);
	});
});

$(document).ready(function(){
	// contact form
	$(".contact-form").each(function(){
		form_validation($(this), 'soft');
	});
	$(".contact-form .submitbox input").each(function(){
		var submitButton = $(this);
		submitButton.fadeTo('fast', 0.5);
		$(this).attr("disabled", "disabled");
		submitButton.parent().mouseenter(function(){
			var button = $(this).children().eq(0);
			if (!submitButton.closest("form").hasClass("valid")) button.val(button.attr("alt"));
		}).mouseleave(function(){
			var button = $(this).children().eq(0);
			button.val(button.attr("title"));
		});
		/*submitButton.click(function(e){
			e.preventDefault();
		});
		submitButton.hover(function(){
			if (submitButton.val() == 'Abschicken') submitButton.val("Bitte ausfüllen.")
			else {
				submitButton.val("Abschicken")
			}
		});*/
	});
	$(".contact-form label.required").next().keyup(function(){
		form_validation($(this).closest("form"), 'soft');
	}).blur(function(){
		form_validation($(this).closest("form"), 'soft');
	});
	$(".close-message").click(function(event){
		$(this).parent().parent().slideUp();
		event.preventDefault();
	});
	
	softcheckForm();
});

function softcheckForm(){
	$(".contact-form").each(function(){
		form_validation($(this), 'soft');
	});
	setTimeout('softcheckForm()', 200);
}

function form_validation(form, type){
	var errors_occured = 0;
	var form = $(form);
	var submitButton = $("input[type=submit]", form);
	$("label.required", form).each(function(){
		var label = $(this); // label
		var input = label.next(); // input
		//if (input.val().replace(/\s*/gi, '') == '') var strEmpty = true;
		//else var strEmpty = false;
		if (
				// regular text inputs
				input.val() == '' ||
				// email address inputs
				label.hasClass("mail") && !input.val().match(/^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/) ||
				// numeric inputs
				label.hasClass("number") && isNaN(parseInt(input.val())) ||
				// name inputs
				label.hasClass("name") && input.val() == 'Ihr Vor- und Nachname' ||
				// textarea inputs
				label.hasClass("message") && input.val() == 'Ihre Nachricht' ||
				// select inputs
				label.hasClass("select") && input.val() == 'Bitte wählen'
		) {
			if (type != 'soft') {
				label.addClass("error");
				input.addClass("error");
			}
			else {
				label.removeClass("valid");
				input.removeClass("valid");
				form.removeClass("valid");
			}
			errors_occured = 1;
		}
		else {
			label.removeClass("error").addClass("valid");
			input.removeClass("error").addClass("valid");
		}
	});
	if (errors_occured === 0) {
		if (type == 'soft') {
			submitButton.fadeTo('fast', 1).attr("disabled", "");
			form.addClass("valid");
			//form.submit("return form_validation(this)");
		}
		/*if ($(form).hasClass("contact-form")) {
			$.post("formmail/formmail.php", $(form).serialize());
			$(".").fadeIn();
		}*/
		return true;
	}
	else {
		if (type == 'soft') {
			submitButton.fadeTo('fast', 0.5).attr("disabled", "disabled");
		}
		else {
			$(".error-message", form).slideDown(function(){
				//window.location.href = "#fehlermeldung";
			});
			//$(".error-message", form).fadeIn();
		}
		return false;
	}
}
