$("document").ready(function() {

// Home Feature Rotate
var slideCount = ($("#home_feature li").length);
var slideNum = 0;
$("#home_feature_0").animate({opacity:1.0}, 3000, slideBg).fadeOut("fast", slideRotate);

function slideRotate(){
	if(slideNum == slideCount){
		slideNum = 0;
	}
	$("#home_feature_" + slideNum + "").fadeIn("medium").animate({opacity:1.0}, 3000, slideBg).fadeOut("fast", slideRotate);
	slideNum++;
}

function slideBg(){
	var slideImage = $(this).children("img").attr("src");
	$("#home_feature").css("background-image", "url(" + slideImage + ")");
}

// MPS Update Rotate
var updateCount = ($("#mps_updates li").length);
var updateNum = 1;
$("#mps_updates_0").animate({opacity:1.0}, 5000).fadeOut("fast", updateRotate);

function updateRotate(){
	if(updateNum == updateCount || $("#mps_updates_" + updateNum + "").text() == ""){
		updateNum = 0;
	}
	$("#mps_updates_" + updateNum + "").fadeIn("medium").animate({opacity:1.0}, 5000, slideBg).fadeOut("fast", updateRotate);
	updateNum++;
}


// Snow Report conversions
	Usage: jQuery.fn.snowDepth = function() {
		var snow_value = $(this).html();
		var snow_equation = Math.round(snow_value * 0.3937);
		var snow_answer = snow_value + " cm / " + snow_equation + " in";
		$(this).html(snow_answer);    
	}
	Usage: jQuery.fn.temperature = function() {
		var temp_value = $(this).html();
		var temp_equation = Math.round((temp_value * 1.8) + 32);
		var temp_answer = temp_value +" C / " + temp_equation +" F";
		$(this).html(temp_answer);    
	} 
							 
	$("#snow_last_week").snowDepth();
	$("#snow_last_day").snowDepth();
	$("#snow_lodge").snowDepth();
	$("#snow_alpine").snowDepth();
	$("#temperature").temperature();

// Float Image Spacing
$("#content_container img").each(function() {
	var imgTitle = $(this).attr("title");
	var imgWidth = $(this).outerWidth();
	if($(this).css("float") == "left"){
		$(this).css("margin-right", "20px");
		if($(this).attr("title") != ""){
			$(this).after("<div class='image_alt_left' style='width:" + imgWidth + "px;'>" + imgTitle + "</div>");
		}
	}
	if($(this).css("float") == "right"){
		$(this).css("margin-left", "20px");
		if($(this).attr("title") != ""){
			$(this).after("<div class='image_alt_right' style='width:" + imgWidth + "px;'>" + imgTitle + "</div>");
		}
	}
});

// Faq
$("#faq").children("li").children("h4").toggle(function(){
		$(this).siblings().show();
	},
	function(){
		$(this).siblings().hide();
	});

// Hide Empty Testimonials
if(!$("#cta_testimonials blockquote").text()){
	$("#cta_testimonials").css("display", "none");
}

// SITE FORMS
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 regNumb = /^\d+([\.]?)\d+$|^\d+$/;

var inputAlert = " is required"; //Alert message

$("input").mouseover(inputValidation);
$("textarea").mouseover(inputValidation);

// VALIDATE CONTACT FORM
$("#contact_form input[id=submit]").click(function () {
	if($("#name").val() == "" || $("#name").val() == "name" + inputAlert){
		$("#name").val("name" + inputAlert);
		$("#name").css(notValid);
		return false;
	}
	else if(!$("#email").val().match(regEmail)){
		$("#email").val("email" + inputAlert);
		$("#email").css(notValid);
		return false;
	}
	else if(!$("#phone_number").val().match(regPhone)){
		$("#phone_number").val("phone number" + inputAlert);
		$("#phone_number").css(notValid);
		return false;
	}
	else if($("#comment").val() == "" || $("#comment").val() == "comment" + inputAlert){
		$("#comment").val("comment" + inputAlert);
		$("#comment").css(notValid);
		return false;
	}
	return true;
});

// Hover Blur Validation Input
function inputValidation(){
	$("input").focus(function(){
		var inputTitle = $(this).attr("title");
		
		if($(this).attr("type") == "submit" || $(this).attr("name") == "location_name" || $(this).attr("name") == "store_name" || $(this).attr("name") == "guide_name" || $(this).attr("name") == "city" || $(this).attr("name") == "length" || $(this).attr("name") == "girth"){
			//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");
				}
			});
		}
	});
}
});
