$("document").ready(function() {

// ----- HOME FEATURE
var maxNum = $("#home_feature img").length;
var bannerNum = 0;

$("#home_feature").css("background-image", "none");
$("#home_feature img[src*=home_feature_" + bannerNum + "]").fadeIn("fast", bannerRotate);

$("#home_feature").append("<ul></ul>");
$("#home_feature img").each(function(i){
	$("#home_feature ul").append("<li id='" + i + "'><span></span></li>");
});

function bannerRotate(){
	
	if(bannerNum == maxNum){
		bannerNum = 0;
	}
	
	$("#home_feature img[src*=home_feature_" + bannerNum + "]").fadeIn("medium").animate({opacity:1.0}, 5000).fadeOut("fast", bannerRotate);
	$("#home_feature ul").children("#" + bannerNum + "").children("span").css("display", "block").animate({opacity:1.0}, 5000).fadeOut("fast");
	bannerNum++;
}

$("#home_feature li").click(function(evt){
	$("#home_feature img").stop();
	$("#home_feature span").stop();
	bannerNum = evt.target.id;
});



// ----- FORM VALIDATION CODE
var regEmail = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
var regPhone = /^((\+\d{1,3}(-| )?\(?\d\)?(-| |.)?\d{1,5})|(\(?\d{2,6}\)?))(-| |.)?(\d{3,4})(-| |.)?(\d{4})(( x| ext)\d{1,5}){0,1}$/;
var inputAlert = " is required";

$("input").mouseover(inputValidation);
$("textarea").mouseover(textareaValidation);

// VALIDATE CONTACT FORM
$("#contact_form input[id=button_submit]").click(function () {
	if($("#name").val() == "" || $("#name").val() == $("#name").attr("title") + inputAlert){
		$("#name").val($("#name").attr("title") + inputAlert);
		$("#name").addClass("not_valid");
		return false;
	}
	else if(!$("#email").val().match(regEmail)){
		$("#email").val($("#email").attr("title") + inputAlert);
		$("#email").addClass("not_valid");
		return false;
	}
	else if(!$("#phone_number").val().match(regPhone)){
		$("#phone_number").val($("#phone_number").attr("title") + inputAlert);
		$("#phone_number").addClass("not_valid");
		return false;
	}
	else if($("#comment").val() == "" || $("#comment").val() == $("#comment").attr("title") + inputAlert){
		$("#comment").val($("#comment").attr("title") + inputAlert);
		$("#comment").addClass("not_valid");
		return false;
	}
	return true;
});

// Hover Blur Validation Input
function inputValidation(){
	$("input").focus(function(){
		var inputTitle = $(this).attr("title");
		
		if($(this).attr("type") == "submit"){
			//Don't do anything
		}else if($(this).val() == "" || $(this).val() == inputTitle){
			$(this).attr({value: ''});
			$(this).blur(function(){
				if ($(this).attr("name").indexOf("email") != -1 || $(this).attr("name").indexOf("EMAIL") != -1 ){
					if ($(this).val().match(regEmail)){
						$(this).removeClass("not_valid");
						$(this).addClass("valid");
					}else{
						$(this).removeClass("valid");
						$(this).addClass("not_valid");
						$(this).val(inputTitle + inputAlert);
					}
				}else {
					if ($(this).val() == "" || $(this).val() == inputTitle || $(this).val() == inputTitle + inputAlert ){
						$(this).removeClass("valid");
						$(this).addClass("not_valid");
						$(this).val(inputTitle + inputAlert);
					}else{
						$(this).removeClass("not_valid");
						$(this).addClass("valid");
					}
				}
			});
		}else if($(this).val() == inputTitle + inputAlert){
			$(this).attr({value: ''});
			$(this).blur(function(){
				if($(this).val() == "" || $(this).val() == inputAlert || $(this).val() == inputTitle + inputAlert ){
					$(this).val(inputTitle + inputAlert);
					$(this).removeClass("valid");
					$(this).addClass("not_valid");
				}else{
					$(this).removeClass("not_valid");
					$(this).addClass("valid");
				}
			});
		}
	});
}

// Hover Blur Validation textfield
function textareaValidation(){
	$("textarea").focus(function(){
		var inputTitle = $(this).attr("title");
		
		if($(this).val() == inputTitle + inputAlert){
			$(this).attr({value: ''});
			$(this).blur(function(){
				if($(this).val() == "" || $(this).val() == inputTitle + inputAlert ){
					$(this).val(inputTitle + inputAlert);
					$(this).removeClass("valid");
					$(this).addClass("not_valid");
				}else{
					$(this).removeClass("not_valid");
					$(this).addClass("valid");
				}
			});
		}
	});
}
});
