$(function(){


	// Setup join mailing list form
	var fmInquiry = $('#inquiry');


	fmInquiry.find('.ribbon button').click(function(){
		
		if($(this).parent().hasClass('opened'))
		{
			$('body').animate({ marginTop: -300 }, function(){
				_gaq.push(['_trackEvent', 'Forms', 'Have a question?', 'Closed']);
				$('form#inquiry .ribbon').removeClass('opened');
			});
		}
		else
		{
			$('body').animate({ marginTop: 0 }, 500, function(){
				_gaq.push(['_trackEvent', 'Forms', 'Have a question?', 'Opened']);
				$('form#inquiry .ribbon').addClass('opened');
			});
		}

	});


	// Setup contact us form
	fmInquiry.validate({
		onkeyup: false,
		onfocusout: false,
		onclick: false,
		onsubmit: true,
		rules: {
			name: {
				required: true
			},
			email: {
				email: true,
				remote: '/forms/validate-email-address/'
			},
			question: {
				required: true
			}
		},
		messages: {
			name: {
				required: '(required)'
			},
			email: {
				email: '(invalid email)',
				remote: '(invalid email)'
			},
			question: {
				required: '(did you forget your question?)'
			}
		},
		submitHandler: function(){

			// Record event with GA
			_gaq.push(['_trackEvent', 'Forms', 'Have a question?', 'Form submitted']);

			fmInquiry.ajaxSubmit({
				dataType: 'json',
				error: function(){

					// Prompt with error message
					alert('Embarrassing. For some reason your request failed. Please try again or, alternately, call or email us.');

					// Record event with GA
					_gaq.push(['_trackEvent', 'Forms', 'Have a question?', 'Request Failed']);
				},
				success: function(){

					// Prompt with success message
					alert('Thank-you, your request has been submitted. Expect to hear from us soon.');

					// Close form (slide up)
					$('body').animate({ marginTop: -300 }, function(){

						fmInquiry.find('.ribbon').removeClass('opened');

						// Reset the form
						fmInquiry.resetForm();
					});

					// Record event with GA
					_gaq.push(['_trackEvent', 'Forms', 'Have a question?', 'Form submitted successfully']);
				}
			});
		}
	});


	/********************************************/

	// Setup join mailing list form
	var fmList = $('#footer form');

	// Set empty text
	fmList.find('input').bind('focus ', function(){

		// Set variables
		var input = $(this);
		var val = $(this).val();

		// Update value
		if(val == 'name' || val == 'email') input.val('');


	});
	fmList.find('input').bind('blur', function(){

		// Set variables
		var input = $(this);
		var val = $(this).val();

		// Update value

		if(val == '') input.val(input.attr('name'));


	});

	jQuery.validator.addMethod("notEqualTo", function(value, element, param) {
		return this.optional(element) || value != param;
	}, 'This has to be different...');

	// Setup contact us form
	fmList.validate({
		onkeyup: false,
		onfocusout: false,
		onclick: false,
		onsubmit: true,
		rules: {
			name: {
				notEqualTo: "name",
				required: true
			},
			email: {
				notEqualTo: "email",
				required: true,
				email: true,
				remote: '/forms/validate-email-address/'
			}
		},
		messages: {
			name: {
				required: 'Name is required.',
				notEqualTo: 'Name is required.'
			},
			email: {
				notEqualTo: 'Email address is required.',
				required: 'Email address is required.',
				email: 'Invalid email address.',
				remote: 'Invalid email address.'
			}
		},
		submitHandler: function(){

			// Record event with GA
			_gaq.push(['_trackEvent', 'Forms', 'Free insights', 'Form submitted']);

			fmList.ajaxSubmit({
				dataType: 'json',
				error: function(){

					// Prompt with error message
					alert('Embarrassing. For some reason your request failed. Please try again or, alternately, call or email us.');

					// Record event with GA
					_gaq.push(['_trackEvent', 'Forms', 'Have a question?', 'Request Failed']);
				},
				success: function(){

					// Prompt with success message
					alert('Thank-you, your name has been added to our mailing list. Good things to come!');
					
					// Reset the form
					fmList.resetForm();

					// Record event with GA
					_gaq.push(['_trackEvent', 'Forms', 'Free insights', 'Form submitted successfully']);
				}
			});
		}
	});




		$('#are_we_a_good_fit div.content div.quiz li input').click(function(){
			
			// Check if the form has already been submitted
			var results = $('#are_we_a_good_fit div.content div.quiz .columns .column2 .results');
			var instructions = $('#are_we_a_good_fit div.content div.quiz .columns .column2 .instructions');

			if(results.is(':visible'))
			{
				results.hide();
				instructions.show();
			}
			
			if($(this).is(':checked'))
			{
				$(this).parent().addClass('checked');
			}
			else
			{
				$(this).parent().removeClass('checked');
			}
		});

		$('#are_we_a_good_fit div.content div.quiz button').click(function(){

			var score = $('#are_we_a_good_fit div.content div.quiz').find('input:checked').length;
			var results = $('#are_we_a_good_fit div.content div.quiz .columns .column2 .results');
			
			switch(score)
			{
				case 1:
					results.find('.description').html("Thanks for dropping by our site and taking the quiz. It looks like we're not quite ready for each other but hey, relationships take time. We'd love to hear back from you when you're ready.");
				break;
				case 2:
					results.find('.description').html("We're not ones to rush you into anything. Take your time and browse the rest of the site. For fresh insights and ideas please visit <a href=\"/blog/\">our blog</a>.");
				break;
				case 3:
					results.find('.description').html("Perhaps you'd like to learn more about a specific service or just need some direction. Take a couple minutes to check out <a href=\"/association-marketing/\">our services</a> pages or <a href=\"http://twitter.com/flockmarketing\">follow us</a> on twitter for regular tips and updates.");
				break;
				case 4:
					results.find('.description').html("For us to consistently deliver effective work potential clients should select at least 5 of these criteria. Are you sure you didn't miss anything? ");
				break;
				case 5:
					results.find('.description').html("We're warming up to each other already! To request more specific info on how we might be able to help, just <a href=\"/contact/\">drop us a line</a>.");
				break;
				case 6:
					results.find('.description').html("We can't know for sure until we actually learn more about your organization, but it's looking like we may be a great fit! Why not <a href=\"/contact/\">give us a call</a>.");
				break;
				case 7:
					results.find('.description').html("These are good signs. <a href=\"/contact/\">Call us.</a> <a href=\"mailto:info@flock.ca\">Email us.</a> Your choice. We're looking forward to hearing from you and learning more about your organization. ");
				break;
				case 8:
					results.find('.description').html("You're showing definite potential for \"Star Client\" status. Give us a call and lets see if this is for real! Our phone number is 905.331.1850.");
				break;
				case 9:
					results.find('.description').html("We can't wait to meet you! Our phone number is 905.331.1850.");
				break;
				case 10:
					results.find('.description').html("Perfect! Let's get started! Our phone number is 905.331.1850.");
				break;
				default:
					alert('Oops, it appears you forgot to select any statements.');
			}

			// Display score (if above zero)
			if(score > 0)
			{
				$('#are_we_a_good_fit div.content div.quiz .columns .column2 .instructions').hide();
				results.find('.score').html(score + '/10');
				results.show();
			}

			// Record action to GA
			_gaq.push(['_trackEvent', 'Forms', 'Are we a good fit?', 'View Results'], score + '/10');

			// Prevent default action
			return false;
		});








		/******************************/
		var fmComment = $('#comment');
	
		// Setup contact us form
		fmComment.validate({
			onkeyup: false,
			onfocusout: false,
			onclick: false,
			onsubmit: true,
			rules: {
				sName: {
					required: true
				},
				sEmail: {
					email: true,
					required: true,
					remote: '/forms/validate-email-address/'
				},
				sMessage: {
					required: true
				}
			},
			messages: {
				sName: {
					required: '(required)'
				},
				sEmail: {
					email: '(invalid email)',
					required: '(required, don\'t worry, it will not be published)',
					remote: '(this doesn\'t appear to be a valid email)'
				},
				sMessage: {
					required: '(did you forget your message?)'
				}
			},
			submitHandler: function(){

				fmComment.ajaxSubmit({
					dataType: 'json',
					success: function(){
							
						// Reset the form
						fmComment.resetForm();
						
						// Reload the page	
						location.reload(true);
					}
				});
			}
		});



		fmComment.find('a.bold').click(function(){

			// Get current selection
			var selection = fmComment.find('textarea').getSelection();
		
			if(selection.length < 1)
			{
				alert('Please select some text to bold first, thanks!');
			}
			else
			{
				fmComment.find('textarea').replaceSelection('<strong>' + selection.text + '</strong>',true);
			}
		
			return false;
		});

		fmComment.find('a.italicize').click(function(){

			// Get current selection
			var selection = fmComment.find('textarea').getSelection();
		
			if(selection.length < 1)
			{
				alert('Please select some text to italicize first, thanks!');
			}
			else
			{
				fmComment.find('textarea').replaceSelection('<em>' + selection.text + '</em>',true);
			}
		
			return false;
		});

		fmComment.find('a.link').click(function(){

			// Get current selection
			var selection = fmComment.find('textarea').getSelection();
		
			if(selection.length < 1)
			{
				alert('Please select some text to link first, thanks!');
			}
			else
			{
				var url = prompt("Please enter the address you want to link to:", "http://");
				if (url!=null && url!="")
				{
					fmComment.find('textarea').replaceSelection('<a href="' + url + '">' + selection.text + '</a>',true);
				
				}	
			}

			return false;

		});
		
		
		
		
		
		
	
	
	
	
	
	
	
	
});
