
// cp function
var cp = {
	// Check if Firebug is installed and output the log
	log: function(msg){if (window.console && window.console.firebug){console.log((new Date().getTime() - ts) + ': '+ msg);};},

	init: function(){
		cp.log('init()');
	},

	domready: function(){
		cp.log('domready()');
		$("a.overlay").fancybox();
		$('form.ajax input.button').click(function(){
			
			var dataString = new String;
			var parentForm = $(this).parents('form');
			parentForm.find(':input').each(function(){
				if($(this).attr('id'))
					dataString += $(this).attr('id') + '=' + $(this).val() + '&';
			})	
										
			$.ajax({
				type: 'POST',
				url: parentForm.attr('action'),
				data: dataString,
				success: function(data){
					switch(data){
						case 'emailnotvalid':
						parentForm.prepend('<p class="message error bottom10">Your email address is not valid.</p>');
						break;
						
						case 'usersubscribed':
						parentForm.prepend('<p class="message valid bottom10">You are correctly subscribed.</p>');						
						break;	
						
						case 'useralreadyexist':
						parentForm.prepend('<p class="message error bottom10">This email address is already subscribed.</p>');
						break;		

						case 'userunsubscribed':
						parentForm.prepend('<p class="message valid bottom10">You are correctly unsubscribed.</p>');
						break;	
						
						case 'usernotvalid':
						parentForm.prepend('<p class="message error bottom10">This email address is not in our database.</p>');
						break;	

						case 'emailsent':
						parentForm.prepend('<p class="message valid bottom10">Your message has been sent.</p>');
						break;		
						
						case 'emailnotsent':
						parentForm.prepend('<p class="message error bottom10">Your message has not been sent. Please try again.</p>');
						break;	
						
						case 'noteempty':
						parentForm.prepend('<p class="message error bottom10">The note field is required.</p>');
						break;																																			
					}
					window.setTimeout(function() {
						$('.message').fadeOut();
					}, 3000);					
				}
			});
						
			return false;
		})
	}
}
// Execute post DOM ready manipulation
$(document).ready(function(){cp.domready()});

// Execute pre DOM ready manipulation
cp.init();