// INDEX PAGE FUNCTIONS

$(document).ready(function() {
	
	// Insert the navigation div and run the jquery cycle script
	$('.gallery').after('<div id="projectNav"></div>').cycle({
		fx: 'fade',
		pager: '#projectNav',
		pause: 1
	});
    
    // Function to send Ajax form submit
    $('#contact').submit(function() {
    	
    	// Remove any existing status messages
    	$('#contact .sendMessage').remove();
    	
    	// Disable and dim the submit button
    	$submitButton = $('#contact #submit').attr('disabled', 'disabled');
    	$submitButton.css('opacity',0.5);
    	
    	// Insert the status message span and get the documents language attribute
    	$statusMsg = $('<span class="sendMessage"></span>').insertAfter('#contact #submit');
    	docLang = $('html').attr("lang");
    	
    	// Check to see if the form validates
    	if (validateFormInputs()) {
    		
    		// Set the status message to 'sending'
    		$statusMsg.removeClass('error').addClass('busy');
    		if (docLang == "es") {
    			$statusMsg.text('Enviando Mensaje');
    		} else {
    			$statusMsg.text('Sending Message');
    		} 
    		
    		// Serialize the form data
    		var dataString = $('#contact').serialize();
    		
    		$.ajax({
    			type: "POST",
    			url: "services/indexForm.cfm",
    			data: dataString,
    			dataType: "json",
    			success: function(data) {
    				
    				// Fade out the sending message
    				$statusMsg.fadeOut(1000, function() {
    					
    					if(data.status == false) {
    					
	    					// Something has gone wrong within the CFML file, send back an error
	    					$statusMsg.removeClass('busy').addClass('error');
	    					if (docLang == "es") {
				    			$statusMsg.text(data.error);
				    		} else {
				    			$statusMsg.text(data.error);
				    		}
				    		
	    				} else {
	    					
	    					// Everything has worked. Send back a sent message and reset the form
	    					$statusMsg.removeClass('busy error');
	    					if (docLang == "es") {
				    			$statusMsg.text('Mensaje Enviado');
				    		} else {
				    			$statusMsg.text('Message Sent');
				    		}
				    		resetForm();
	    				}
	    				
	    				$statusMsg.fadeIn(1000);
    					
    				});
    				
    				
    			}
    		});
			
    	} else {
    		
    		// Use the example function to repopulate the form fields with thier titles
			$('input[type=text],textarea').example(function() {
				return $(this).attr('title');
			}, {className: 'blank'});
    		
    		// Set the status message to show user error
    		$statusMsg.removeClass('busy').addClass('error');
			if (docLang == "es") {
    			$statusMsg.text('Por favor, revise los campos obligatorios');
    		} else {
    			$statusMsg.text('Please review all of the required fields');
    		}
    		
    	}
    	
    	// Re-enable and re-set opacity on the submit button
    	$submitButton.removeAttr('disabled');
    	$submitButton.css('opacity',1.0);
    	
    	return false;
    	
    });
	
});

// Contact form validation function
function validateFormInputs() {
	
	// Start validation
	$.validity.start();
	
	$('#name').require("Error");
	$('#email').require("Error").match("email","Error");
	$('#message').require("Error").nonHtml("Error");
	
	// End validation and get result
	var result = $.validity.end();
	
	// Return the result of the validation
	return result.valid;
}

// Reset contact form function
function resetForm() {
	
	$('#name').val("");
	$('#email').val("");
	$('#telephone').val("");
	$('#message').val("");
	
	// Use the example function to repopulate the form fields with thier titles
	$('input[type=text],textarea').example(function() {
		return $(this).attr('title');
	}, {className: 'blank'});
	
} 

