var slideshowSpeed = 3000;

var photos = [ {
		"image" : "DSC_0224.jpg",
	}, {
		"image" : "DSC_0227.jpg",
	}, {
		"image" : "DSC_0197.jpg",
	}, {
		"image" : "DSC_0198.jpg",
	}, {
		"image" : "DSC_0202.jpg",
	}, {
		"image" : "DSC_0210.jpg",
	}, {
		"image" : "DSC_0212.jpg",
	}, {
		"image" : "DSC_0220.jpg",
	}, {
		"image" : "DSC_0223.jpg",
	}, {
		"image" : "DSC_0183.jpg",
	}, {
		"image" : "DSC_0190.jpg",
	}
	
];



$(document).ready(function() {
		
	
	var activeContainer = 1;	
	var currentImg = 0;
	var animating = false;
	var navigate = function(direction) {
		// Check if no animation is running. If it is, prevent the action
		if(animating) {
			return;
		}
		
		// Check which current image we need to show
		if(direction == "next") {
			currentImg++;
			if(currentImg == photos.length + 1) {
				currentImg = 1;
			}
		} else {
			currentImg--;
			if(currentImg == 0) {
				currentImg = photos.length;
			}
		}
		
		// Check which container we need to use
		var currentContainer = activeContainer;
		if(activeContainer == 1) {
			activeContainer = 2;
		} else {
			activeContainer = 1;
		}
		
		showImage(photos[currentImg - 1], currentContainer, activeContainer);
		
	};
	
	var currentZindex = 5;
	var showImage = function(photoObject, currentContainer, activeContainer) {
		animating = true;
		
		// Make sure the new container is always on the background
		//currentZindex--;
		
		// Set the background image of the new active container
		$("#headerimg" + activeContainer).css({
			"background-image" : "url(/images/header/" + photoObject.image + ")",
			"display" : "block",
			"z-index" : currentZindex
		});
		
		
		
		// Fade out the current container
		// and display the header text when animation is complete
		$("#headerimg" + currentContainer).fadeOut(function() {
			setTimeout(function() {
				animating = false;
			}, 500);
		});
		$("#headerimg" + currentContainer).css({
            "z-index" : "6"
		});
	};
	
	
	// We should statically set the first image
	navigate("next");
	
	// Start playing the animation
	interval = setInterval(function() {
		navigate("next");
	}, slideshowSpeed);
	
});

function chkContFrm(frm){
	if(frm.fname.value.length == 0){
		alert("Please enter your First Name");
		frm.fname.focus();
		return false;
	}
	if(frm.lname.value.length == 0){
		alert("Please enter your Last Name");
		frm.lname.focus();
		return false;
	}
	if(frm.phone.value.length == 0){
		alert("Please enter your Phone Number");
		frm.phone.focus();
		return false;
	}
	if(!validatePhone(frm.phone.value)){
		alert("The Phone Number you entered is not valid. Please enter your phone number including the area code");
		frm.phone.select();
		frm.phone.focus();
		return false;
	}
	if(frm.email.value.length == 0){
		alert("Please enter your Email Address");
		frm.email.focus();
		return false;
	}
	if(!validateEmail(frm.email.value)){
		alert("The Email Address you entered is not valid");
		frm.email.select();
		frm.email.focus();
		return false;
	}
	if(frm.subject.value.length == 0){
		alert("Please enter a Subject for your message");
		frm.subject.focus();
		return false;
	}
	if(frm.message.value.length == 0){
		alert("Please enter your Message");
		frm.message.focus();
		return false;
	}
	return true;
}

function validatePhone(strIn){
    strIn = strIn.replace("(","").replace(")","").replace(" ","").replace("-","").replace("-","");
	if(isNaN(strIn) || strIn.length < 10){
	  return false;
	}else{
	  return true;
	}
}
  
function validateEmail(strIn){
	var re = 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]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\]?)$/i );
	if(re.test(strIn) == false){
	  return false;
	}else{
	  return true;
	}
}
