// Initiate mouse glow, and testimonial/banner rotator
$(document).ready(function(){
	$('body').mousemove(function(e){
      $('.follow').css('top', e.clientY - 136).css('left', e.clientX - 136);
	});
	$('.follow').live('selectstart dragstart', function(evt){ evt.preventDefault(); return false; });
	
	setTimeout("window.testimonialObj.startTimer()",5000);
	setTimeout("window.bannerObj.startTimer()",5000);
});


/*
** This code manages the testimonial and banner 
** rotators. This needs to be updated, along with
** the background images for each, when any slides
** are added/removed.
*/

var testimonialObj = {};

testimonialObj.counter = 1;
testimonialObj.slideCount = 4;

testimonialObj.startTimer = function() {

	if( testimonialObj.counter < testimonialObj.slideCount ) {
		$('.testimonials div').animate({opacity:'0'}, 500, function() {
			var deltaY = testimonialObj.counter * -160;
			$(this).css({backgroundPosition: "0px " + deltaY + "px"});
			$(this).animate({opacity:'1'}, 500, function(){
				testimonialObj.counter++;
				var t = setTimeout("testimonialObj.startTimer()", 5000);
			});

		});
	} else if( testimonialObj.counter == testimonialObj.slideCount ) {
		$('.testimonials div').animate({opacity:'0'}, 500, function() {
			$(this).css({backgroundPosition: "0px 0px"});
			$(this).animate({opacity:'1'}, 500, function(){
				testimonialObj.counter = 1;
				var t = setTimeout("testimonialObj.startTimer()", 5000);
			});

		});
	}
}

var bannerObj = {};

bannerObj.counter = 1;
bannerObj.slideCount = 4;

bannerObj.startTimer = function() {

	if( bannerObj.counter < bannerObj.slideCount ) {
		$('.team_banner div').animate({opacity:'0'}, 500, function() {
			var deltaY = bannerObj.counter * -336;
			$(this).css({backgroundPosition: "0px " + deltaY + "px"});
			$(this).animate({opacity:'1'}, 500, function(){
				bannerObj.counter++;
				setTimeout("bannerObj.startTimer()", 5000);
			});

		});
	} else if( bannerObj.counter == bannerObj.slideCount ) {
		$('.team_banner div').animate({opacity:'0'}, 500, function() {
			$(this).css({backgroundPosition: "0px 0px"});
			$(this).animate({opacity:'1'}, 500, function(){
				bannerObj.counter = 1;
				setTimeout("bannerObj.startTimer()", 5000);
			});

		});
	}
}

// Google analytics hook ripped from old site
function log_client_in() {
	// Enable Google tracking
	pageTracker._trackPageview('http://callview360.dial800.com/Pages/Login.aspx');
	return true;
}



// General form handling
$(function(){
    // On document ready, check form fields
    $(':input').each(function() {
        if($(this).val() === "") {
            $(this).siblings('span').animate({fontSize:16},'fast');
        }
    });

    // On form focus, adjust colors
    $(':input').focus(function() {
        $(this).addClass('focus');
        $(this).siblings('span').addClass('focus');
    });

    // On keypress, remove label
    $(':input').keypress(function() {
        $(this).siblings('span').animate({fontSize:0},'fast',function(){
            $(this).hide();
        });
    });


    // On form blur, restore colors and label
    $(':input').blur(function() {
        $(this).removeClass('focus');
        $(this).siblings('span').removeClass('focus');
        if($(this).val() == '') {
            $(this).siblings('span').show().animate({fontSize:16},'fast');
        }
    });

    // Pass span 'click' event to input
    $('form span').click(function() {
        $(this).prev('input').trigger('focus');
    });
});


// This handles the pricing form's "Basic" quote, and tooltip
$(function(){
	$('#basic').change(function(){
		if ($('#basic').is(':checked')) {
        $('.getQuote input[type=checkbox]').not('#basic').each(function(){
        	$(this).attr('disabled', true).next('label').css({color:'#999999',cursor:'default'});
        });
    } else {
        $('.getQuote input[type=checkbox]').not('#basic').each(function(){
        	$(this).removeAttr('disabled').next('label').css({color:'#333',cursor:'pointer'});
        });
    } 
	});

	$('.tooltip div').hover(function(){
		$(this).siblings('p').css({opacity:'0'}).show().stop().animate({bottom:'45px',opacity:'1'},250);
	}, function(){
		$(this).siblings('p').css({bottom:'35px',opacity:'0'}).hide();
	});
});

// This controls the FAQ accordian on the contact page
$(function(){
	$('.faq h3').click(function(){
		if($(this).siblings('p').hasClass('current')) {
			$(this).siblings('p').removeClass('current').slideUp();
		} else {
			$('p.current').slideUp().removeClass('current');
			$(this).siblings('p').addClass('current').slideDown();
		}
	});
});

// This handles the client side form block
$( function () {
	
	$('#price_quote, #get_started').submit( function(e) {

		// 1. Check fields, and return pass/fail value
		var pass = check_all_fields();


		// If failed, prevent form submissions
		if ( pass == 0 ) {
			e.preventDefault();
		}
	});

});

function check_all_fields() {
	
	// Initialize form fail code: 0 = fail, 1 = pass
	var code = 1;

	// 1. Check each form for errors (validation)
	$(':input').each(function(){
		
		// 2. Set keyword variables
		var name = $(this).attr('name');
		var value = $(this).val();
		var errors = new Array();
		
		switch( name ) {
		
			case "first_name":
				
				// Validate input value
				if( /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/.test(value) ) {
					
					// If the value is longer than 3 characters
					if( value.length > 2 ) {

						errors[name] = '';

						$(this).removeClass('input_error').siblings('p').slideUp().html(errors[name]);

						code = 1;

						break;

					} 

					// If the value is shorter than 3 characters
					else if( value.length < 3 ) {

						errors[name] = "We need at least 3 letters";

						$(this).addClass('input_error').siblings('p').html(errors[name]).slideDown();

						code = 0;

						break;

					}

				} 

				// If Valudation Failed
				else {

					errors[name] = "Please enter a valid first name";

					$(this).addClass('input_error').siblings('p').html(errors[name]).slideDown();

					code = 0;

					break;
				} // End of case "First_Name"


			case "last_name":
				
				// Validate input value
				if( /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/.test(value) ) {
					
					// If the value is longer than 3 characters
					if( value.length > 2 ) {

						errors[name] = '';

						$(this).removeClass('input_error').siblings('p').slideUp().html(errors[name]);

						code = 1;

						break;

					} 

					// If the value is shorter than 3 characters
					else if( value.length < 3 ) {

						errors[name] = "We need at least 3 letters";

						$(this).addClass('input_error').siblings('p').html(errors[name]).slideDown();

						code = 0;

						break;

					}

				} 

				// If Valudation Failed
				else {

					errors[name] = "Please enter a valid last name";

					$(this).addClass('input_error').siblings('p').html(errors[name]).slideDown();

					code = 0;

					break;
				} // End of case "Last_Name"


			case "email":

				// Validate input value
				if( /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/i.test(value) ) {

					errors[name] = '';

					$(this).removeClass('input_error').siblings('p').slideUp().html(errors[name]);

					code = 1;

					break;

				} 

				// If Valudation Failed
				else {

					errors[name] = "Please enter a valid email address";

					$(this).addClass('input_error').siblings('p').html(errors[name]).slideDown();

					code = 0;

					break;
				} // End of case "Email"


			case "company":

				// Validate input value
				if( value.length > 2 ) {

					errors[name] = '';

					$(this).removeClass('input_error').siblings('p').slideUp().html(errors[name]);

					code = 1;

					break;

				} 

				// If Valudation Failed
				else {

					errors[name] = "Please enter a valid company name";

					$(this).addClass('input_error').siblings('p').html(errors[name]).slideDown();

					code = 0;

					break;

				} // End of case "Company"


			case "phone":

		// Validate input value
				if( value.length > 9 ) {

					errors[name] = '';

					$(this).removeClass('input_error').siblings('p').slideUp().html(errors[name]);

					code = 1;

					break;

				} 

				// If Valudation Failed
				else {

					errors[name] = "Please try again, eg. 515-555-5000";

					$(this).addClass('input_error').siblings('p').html(errors[name]).slideDown();

					code = 0;

					break;
				} // End of case "Phone"


			case "is_human":

				// Validate input value
				if( /six/i.test(value) ) {

					errors[name] = '';

					$(this).removeClass('input_error').siblings('p').slideUp().html(errors[name]);

					code = 1;

					break;

				} 

				else if ( value == "6" ) {
					
					errors[name] = '';

					$(this).removeClass('input_error').siblings('p').slideUp().html(errors[name]);
 
					code = 1;

					break;

				}

				// If Valudation Failed
				else {

					errors[name] = "That's not 2 x 3! Please try again";

					$(this).addClass('input_error').siblings('p').html(errors[name]).slideDown();

					code = 0;

					break;
				} // End of case "Phone"

		} // End Switch

	});

	return code;
}

// This handles client side form validation
$(function(){
	var errors = new Array();
	
	$(':input').blur(function() {
		var name = $(this).attr('name');
		var value = $(this).val();
		
		if(!value == '') 
		{
			switch( name )
			{
				case "first_name":
					if( /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/.test(value) ) {
						if( value.length > 2 ) {
							errors[name] = '';
							$(this).removeClass('input_error').siblings('p').slideUp().html('');
						} else if( value.length < 2 ) {
							errors[name] = "We need at least 2 letters";
						}
						break;
					} else {
						errors[name] = "Please enter a valid first name";
						break;
					}

				case "last_name":
					if( /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/.test(value) ) {
						if( value.length > 2 ) {
							errors[name] = '';
							$(this).removeClass('input_error').siblings('p').slideUp().html('');
						} else if( value.length < 2 ) {
							errors[name] = "We need at least 2 letters";
						}
						break;
					} else {
						errors[name] = "Please enter a valid last name";
						break;
					}

				case "email":
					if( /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/i.test(value) ) {
						errors[name] = '';
						$(this).removeClass('input_error').siblings('p').slideUp().html('');
						break;
					} else {
						errors[name] = "Please enter a valid email address";
						break;
					}

				case "company":
						if( value.length < 3 ) {
							errors[name] = "We need at least 3 letters";
						} else {
							errors[name] = '';
							$(this).removeClass('input_error').siblings('p').slideUp().html('');
						}
						break;

				case "phone":
					if( value.length > 9 ) {
						errors[name] = '';
						$(this).removeClass('input_error').siblings('p').slideUp().html('');
						break;
					} else {
						errors[name] = "Try again, eg. 515-555-5000";
						break;
					}

				case "is_human":
					if( /six/i.test(value) ) {
						errors[name] = '';
						$(this).removeClass('input_error').siblings('p').slideUp().html('');
						break;
					} else if ( value == "6" ) {
						errors[name] = '';
						$(this).removeClass('input_error').siblings('p').slideUp().html('');
						break;
					} else {
						errors[name] = "That's not 2 x 3!";
						break;
					}
				
				case "user":
						if( value.length > 2 ) {
							errors[name] = '';
							$(this).removeClass('input_error').siblings('p').slideUp().html('');
						} else if( value.length < 3 ) {
							errors[name] = "We need a little more...";
						}
						break;

				case "pass":
						if( value.length > 2 ) {
							errors[name] = '';
							$(this).removeClass('input_error').siblings('p').slideUp().html('');
						} else if( value.length < 3 ) {
							errors[name] = "We need a little more...";
						}
						break;
			}
			if(!errors[name] == '') {
				$(this).addClass('input_error').siblings('p').html(errors[name]).slideDown();
			}
		}
	});
	
	
});
