$(document).ready(function() {
	var required = new Array('vorname','name','firma','ort','plz','strasse','email');
	var enabled = false;
	
	$(window).scroll(function () { 
		$('#angebote_wrapper').css('padding-bottom',($(window).scrollTop()+'px'));	
	});
	
	$('.angebotelink').click( function () {
		$('#angebote_wrapper').fadeIn('slow');
	});

	$('.angeboteclose').click( function () {
		$('#angebote_wrapper').fadeOut('slow');
		$('#angebote_content').css('display', 'block');
		$('.feldcontent').each (function () {
			$(this).css('display', 'none');
		});
	});
	
	$('.zuruck').live('click', function () {
		$('#'+$(this).parent().parent().attr('id')).fadeOut('slow');
		$('#angebote_content').fadeIn('slow');
	});
	
	$('.feld').click( function () {
		element_id = $(this).attr('id');
		
		$('#angebote_content').fadeOut('slow');
		$('#'+element_id+'_content').fadeIn('slow');
	});
	
	$('.feldclose').click( function () {
		$('#'+$(this).parent().parent().parent().attr('id')).fadeOut('slow');
		$('#angebote_content').fadeIn('slow');
	});

	$('.bigfeldclose').click( function () {
		$('#'+$(this).parent().parent().parent().parent().attr('id')).fadeOut('slow');
		$('#angebote_content').fadeIn('slow');
	});
	
	$('.feldcontent .right form #abschicken').click(function() {
		element_id = $(this).parent().parent().parent().parent().attr('id');
		for (i = 0; i < required.length; i++) {
			if ($('#'+element_id+' .feldcontent .right form #'+required[i]).val() != '')
				enabled = true;
			else
				enabled = false;
		}
		
		if (enabled) {
			return true;
		} else {
			alert('* Pflichtfeld fehlt')
			return false;
		}
			
	});
	
	$('.feldcontent .right form').submit(function() {
		// Replace submit with loader gif
		element_id = $(this).parent().parent().parent().attr('id');
//		$('#'+element_id+' .feldcontent .right form #abschicken').remove();
		$('#'+element_id+' .feldcontent .right form #abschicken').replaceWith('<p id="abgeschickt" style="float:right;padding-right:10px;padding-top:5px;"><img src="images/ajax-loader.gif"> Ihre Anfrage wird versendet.</p>');
		var_string = '';
		$('#'+element_id+' .feldcontent .right form input').each(function() {
			if (var_string != '')
				var_string = var_string+'&';
			
			var_string = var_string+$(this).attr('id')+'='+$(this).val();
		});
		var_string = var_string+'&anrede='+$('#'+element_id+' .feldcontent .right form #anrede').val();
		
		$.ajax({
			type: "POST",
			url: "./ajax/contact.php",
			data: var_string,
			success: function(msg){
			$('#'+element_id+' .feldcontent .right form #abgeschickt').html('Ihre Anfrage wurde erfolgreich versendet. Vielen Dank!');
			}
		});
		return false;
			
	});
});
