// Start Functioning when DOM is ready
$(function() {
	
	// Get Jello for errors
	$.getScript('http://resources.7u.nl/Javascript/JQuery/jquery.jello.js');
	
	// Configure and Style jello
	var config = {
		speed: 500,
		opacity: .95,
		style: {
			backgroundColor: '#ffc6c6',
			color: '#ff4040',
			borderBottom: '1px solid #ff4040',
			fontSize: '16px'
		}
	};
	
	// Remove / add 'E-mail adres' correctly from the 
	// e-mail address field
	$('.emailadres').focus(function() {
		if ($(this).val() == 'E-mailadres' ) {
			$(this).val('');
		}
	})
	.blur(function() {
		if ($(this).val() == '') {
			$(this).val('E-mailadres');
		}
	});
	
	// Submit button
	$('.nieuwsbrief .button').click(function() {
		$('.nieuwsbrief').submit();
		return false;
	});
	$('.nieuwsbrief').submit(function() {
		
		// Check E-mail
		var regexp = /[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i;
		var result = regexp.test( $('.emailadres').val() );
		
		// Show error if false e-mail address is given and
		// stop further execution of script
		if (!result) {
			$.jello('Geef a.u.b. een geldig e-mail adres op.', config);
			return false;
		}
		
		// Set Temporary HTML as loading indicator
		var tmp_html = '<span class="loading_text" style="float:right; margin: 7px 8px 0px 0px; font-size: .75em; color:#8a9191;">';
		tmp_html = tmp_html + 'Een moment geduld a.u.b&nbsp;&nbsp;';
		tmp_html = tmp_html + '<img src="'+base_url+'assets/templates/c2b/img/loading.gif" alt="loading" border="0" />';
		tmp_html = tmp_html + '</span>';
		
		// Hide the button and show a loading indicator
		$('.button', this)
			.hide()
			.after(tmp_html);
		
		// Get the e-mailaddress value and disable the field
		var email = $('.emailadres')
			.attr('disabled', 'disabled')
			.val();
		
		// Do an AJAX post to the current request
		$.ajax({
			type: 'post',
			data: {email:email},
			timeout: 10000,
			success: function(d,s,x) {
				// Do correct actions by type of error
				switch(d) {
					case 'OK':
						// Remove the whole form
						$('.nieuwsbrief').fadeOut(500, function() {
							// Generate success HTML
							var success_html = '<span class="success_text" style="float:left;">';
							success_html = success_html + '<p>Uw E-mail adres is opgenomen in onze lijst.</p>';
							success_html = success_html + '<p>Bedankt voor uw interesse.</p>';
							success_html = success_html + '</span>';
							
							// Add success text
							$(this).after(success_html)
							
							// Eventually, remove the HTML of the form
							.remove();
							return false;
						});
					break;
					default:
						return reenable(d, config);
					break;
				}
			},
			error: function(x,s,e) {
				return reenable('Er is iets fout gegaan. Probeert het a.u.b. later nog eens');
			}
		});
		
		return false;
		
	});
	
	// Function to re-enable the form and alert
	// an error message, if one is set
	var reenable = function(error) {
		// Remove the loading text, show the normal
		// submit button and enable the e-mail field
		$('.loading_text').remove();
		$('.nieuwsbrief .button').show();
		$('.emailadres').removeAttr('disabled');
		
		// Alert an error?
		if (typeof error == 'string') {
			$.jello(error, config);
		}
		
		return false;
	};
	
});
