//Author: Parker Hutchinson
//custom jquery code for: Arranging Pixels

$(document).ready(function(){
	//tabs
	$('#tabs').tabs({fx: { opacity: 'toggle' }});
	
	
	$(function() {

		var $tabs = $('#tabs').tabs();

		$(".ui-tabs-panel").each(function(i){

		  var totalSize = $(".ui-tabs-panel").size() - 1;

			  prev = i;
	   		  $(this).append("<a href='#' class='prev' rel='" + prev + "'>&#171; Prev Page</a>");
	
		      next = i + 2;
	   		  $(this).append("<a href='#' class='next' rel='" + next + "'>Next Page &#187;</a>");

		});

		$('.next, .prev').click(function() {
	           $tabs.tabs('select', $(this).attr("rel"));
	           return false;
	    });

	});
	
	
	
	//expand collapse
	$('.expand_wrapper a').click(function expandClick(event){
		event.preventDefault();
		var $this = $(this);
		
		if($this.is ('.bluebar')){
			
			$(this).removeClass("bluebar")
			
		}else{
			
			$(this).addClass('bluebar')
			
		}
		
		$(this).next().animate({height: "toggle"});
		
	});
	
	//focus blur effect for the contact form
	$('#contact_form input.input, textarea').focus(function() {  
	    $(this).css({'background' : '#D6D6D6' , 'color' : '#000'});  
	     
	    if(this.value != this.defaultValue){  
	        this.select();  
	    }  
	});  
	
	$('#contact_form input.input, textarea').blur(function() {  
	    $(this).css({'background' : 'white' , 'color' : '#000'}); 
	 	
	    if ($.trim(this.value == '')){  
	        this.value = (this.defaultValue ? this.defaultValue : '');  
	    }  
	});
	
	//textareaCheck checks to see if the user has written anything
	function textareaCheck(){
		
		var textareaInput = $('#contact_form textarea').val();
		var requiredText = "This Field is Required";
		if(textareaInput == '' || textareaInput == requiredText){
			
		
			$('#contact_form textarea').attr('value', requiredText);
			$('#contact_form textarea').css({'background' : '#FFD8D9','color' : '#D62400'});
			return(false);
						
		}else{
			
			return(true);
			
		}	
				
	}
	//nameCheck checks for a name
	function nameCheck(){
		
		var nameInput = $('#contact_form input#name').attr('value');
		var requiredText = "This Field is Required";
		if(nameInput == '' || nameInput == requiredText){
			
			
			$('#contact_form input#name').attr('value', requiredText);
			$('#contact_form input#name').css({'background' : '#FFD8D9','color' : '#D62400'});
			return(false);	
					
		}else{
			
			return(true);
		
		}	
				
	}
	//emailCheck checks for a proper email address
	function emailCheck(){
		
		//regular expression for email validation returns null if false
		var emailValid = $("input#email").val().match(/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/);
		
		var emailInValid = "Enter a Proper Email Address";
		
		if(emailValid == null){
			
			$('#contact_form input#email').attr('value', emailInValid);
			$('#contact_form input#email').css({'background' : '#FFD8D9','color' : '#D62400'});
			return(false);		
			
		}else if(emailValid == emailInValid){
			
			return(false);		
			
		}else{
			
			return(true);
			
		}	
				
	}
	
	//phoneCheck checks for a valid phone number
	function phoneCheck(){
		
		//regular expression for email validation returns null if false
		var phoneValid = $("input#phonenumber").val().match(/^\D?(\d{3})\D?\D?(\d{3})\D?(\d{4})$/);
		
		var phoneInValid = "Enter a Proper Phone Number";
		
		if(phoneValid == null){
			
			$('#contact_form input#phonenumber').attr('value', phoneInValid);
			$('#contact_form input#phonenumber').css({'background' : '#FFD8D9','color' : '#D62400'});
			return(false);	
					
		}else{
			
			return(true);
			
		}	
				
	}
	
	//on click event for the submit button that init all of the error checking

	$('#submit').click(function formcheck(event){
	
		textareaCheck();
		nameCheck();
		emailCheck();
		//phoneCheck();
		
		//if all fields are valid, then adds regular functionality back to the 
		//submit button and submits the form
		if(nameCheck() == true && emailCheck() == true && textarea() == true){	
	
			$('#submit').unbind('click', formcheck);
				
		}else{
			
			//if any of the forms are not valid then dont submit (removes click functionality from button)
			event.preventDefault();
			
		}
	
	});
	
});
