// SETUP INITIAL VARIBLES


// jQUERY DOM READY FUNTION
$(document).ready(function() {
	// HIDE ALL ELEMENTS WITH THE HIDE CLASS
	$('.hide').hide();
	
	//STYLE NAV
	$('div.main_nav ul li.current_page_item').append('<span id="current_page_item"></span>');
	
	///////////////////////////////////////
	// SLIDER
	///////////////////////////////////////	
	$("div#slider_viewer").easySlider({
		auto: true,
		continuous: true,
		speed: 800,
		pause: 6000,
		numeric: true,
		nextId: 'next_slide',
		prevId: 'prev_slide',
		nextText: '',
		prevText: '',
		numeric: false
		//numericId: 'post_controls'
	});

	
	///////////////////////////////////////
	// CONTACT FORM
	///////////////////////////////////////	
	options = {
		//target:        '#contact_response',   // target element(s) to be updated with server response 
        beforeSubmit:  showRequest,  // pre-submit callback 
        success:       showResponse  // post-submit callback 
 
        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        //clearForm: true        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 
 
        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
	};
	
	// bind to the form's submit event 
	$('#contact_form').submit(function() { 
		// inside event callbacks 'this' is the DOM element so we first 
		// wrap it in a jQuery object and then invoke ajaxSubmit		
		$(this).ajaxSubmit(options);
	
		// !!! Important !!! 
		// always return false to prevent standard browser submit and page navigation 
		return false; 
	});
	
	// FORM INTERACTIVITY
	$('#contact_name').focus(function(){
		if(!$(this).val() || $(this).val() == "YOUR NAME."){
			$(this).val("");
		};
		return false;
	});
	
	$('#contact_name').focusout(function(){
		if(!$(this).val()){
		$(this).val("YOUR NAME.");
		};
		return false;
	});
	
	$('#contact_email').focus(function(){
		if(!$(this).val() || $(this).val() == "YOUR EMAIL."){
			$(this).val("");
		};
		return false;
	});
	$('#contact_email').focusout(function(){
		if(!$(this).val()){
		$(this).val("YOUR EMAIL.");
		};
		return false;
	});
	
	$('#contact_message').focus(function(){
		if(!$(this).val() || $(this).val() == "ENQUIRY."){
			$(this).val("");
		};
		return false;
	});
	$('#contact_message').focusout(function(){
		if(!$(this).val()){
		$(this).val("ENQUIRY.");
		};
		return false;
	});
	
});



///////////////////////////////////////
// CONTACT FORM PRE + POST FUNCTIONS
///////////////////////////////////////

// pre-submit callback 
function showRequest(formData, jqForm, options) { 
    // formData is an array; here we use $.param to convert it to a string to display it 
    // but the form plugin does this for you automatically when it submits the data 
    var queryString = $.param(formData); 
 
    // jqForm is a jQuery object encapsulating the form element.  To access the 
    // DOM element for the form do this: 
    // var formElement = jqForm[0]; 
 	
	var form = jqForm[0];
	alert(queryString);
	
	if(form.form_name == "contact"){
		if(form.name.value == "YOUR NAME." || !form.name.value || form.name.value == " "){
			$('#signup_response').html('<h3>Please insert your name.</h3>');
			showSignupResponse();
			return false;	
		}
	}
	if(!validateEmail(form.email.value)){
		$('#signup_response').html("<h3>Sorry. The email <span class=\"red\">" + form.email.value + "</span> isn't valid. Please check it through or try a different one.</h3>");
		showSignupResponse();
		return false;	
	}
    //alert('About to submit: \n\n' + queryString);
 
    // here we could return false to prevent the form from being submitted; 
    // returning anything other than false will allow the form submit to continue 
    return true; 
} 
 
// post-submit callback 
function showResponse(responseText, statusText, xhr, $form)  { 
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 
    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 
    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 
	responseMessage = responseText;
	$('#signup_response').html(responseMessage);
	
 	$.fancybox({
		'href'				: '#signup_response',
		'padding'			: 0,
		'autoScale'			: true,
		'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'overlayColor'		: '#000'
	});
	
    //alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + '\n\nThe output div should have already been updated with the responseText.'); 
	//$('#contact_response').html(responseText);
}

function validateEmail(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}
