var timeout = 1;
var errors;

$(document).ready(function() {
	fadeNav();
	fadeBackground();
	watchForResize();
	checkForHash();
});

function fadeNav() {
	$('header nav li').not('.active').find('.default').css({
		"opacity": 0,
		"display": "block"
	});
	$('header nav li').not('.active').mouseover(function(){
		var div = $(this).find(".default");
		$(div).stop();
		$(div).animate({opacity: 1}, {queue: false, duration: 200});
	});
	$('header nav li').not('.active').mouseout(function(){
		var div = $(this).find(".default");
		$(div).stop();
		$(div).animate({opacity: 0}, {queue: false, duration: 200});
	});
}

function fadeBackground() {
	var img = $(".background img");
	if ($("body").hasClass("mobile") == false) {
		if (($(img).attr("complete") || timeout > 1000)) {
			$(".background").fadeIn("slow");
			resizeBackground();
		}
		else {
			setTimeout("fadeBackground()", timeout);
			timeout = 2 * timeout;
		}
	}
}
function resizeBackground() {
	
	var img = $(".background img");
	
	if ($("body").hasClass("mobile") == false) {
				
			var image_width = $(img).width();
			var image_height = $(img).height();
			var image_ratio = (image_width / image_height);
		
			var window_width = $(window).width();
			var window_height = $(window).height();
			var window_ratio = (window_width / window_height);
			
			if (window_ratio > image_ratio) {
				$(img).width(window_width);
				$(img).height(Math.floor(window_width/image_ratio));
				var offset_top = ($(img).height() - window_height)/2;
				$(img).css("margin-top", -offset_top);
				$(img).css("margin-left", 0);
			}
			else {
				$(img).height(window_height);
				$(img).width(Math.floor(window_height*image_ratio));
				var offset_left = ($(img).width() - window_width)/2;
				$(img).css("margin-top", 0);
				$(img).css("margin-left", -offset_left);
			}	
		}

}

function watchForResize() {
	$(window).resize(function(){			
		resizeBackground();
	});
}


/**** VALIDATE FORM *****/
function validateForm() {
	var boxes = document.contact_form;
	errors = 0;

	for (var i = 0; i<boxes.length; i++) {
		if (!boxes[i].value) {
			errors++;
			addWarning(boxes[i]);
		}
		else if (boxes[i].name == "email") {
			checkEmail(boxes[i]);
		}
		else {
			removeWarning(boxes[i]);
		}
	}
	
	if (errors == 0) {
		return true;
	}
		
	return false;
}

function addWarning(element) {
	// add warning to element
	if ( !$(element).hasClass("warning") ) {
		$(element).addClass("warning");
	}
	
	// add warning to label
	var parent = $(element).parent().get(0);
	var dt = $(parent).prev().get(0);
	var label = $(dt).children().get(0);
	if ( !$(label).hasClass("warning") ) {
		$(label).addClass("warning");
	}

	// add req element
	var req = $(label).children(".req");
	if ($(req).html() == "") {
		$(req).html("*");
	}
}

function removeWarning(element) {
	// remove warning from element
	if ( $(element).hasClass("warning") ) {
		$(element).removeClass("warning");
	}
	
	// remove warning from label
	var parent = $(element).parent().get(0);
	var dt = $(parent).prev().get(0);
	var label = $(dt).children().get(0);
	if ( $(label).hasClass("warning") ) {
		$(label).removeClass("warning");
	}

	// add req element
	var req = $(label).children(".req");
	if ($(req).html() == "*") {
		$(req).html("");
	}
}

function checkEmail(element) {
	var myregex = /@.*\./;
	var mymatch = myregex.exec(element.value);
	if (mymatch == null) {
		addWarning(element);
		errors++;
	} 
	else {
		removeWarning(element);
	}
}
/***************************/

function checkForHash() {
	var url = window.location.href;
	
	var message = "<p class='feedback'>Thanks for contacting Salt of the Earth. Someone from the restaurant will be in touch with you soon.</p>";
	
	var myregex = /#/;
	var mymatch = myregex.exec(url);
	if (mymatch) {
		$(".secondary_content .section_liner").prepend(message);
		$(".feedback").fadeIn("slow");
	} 	
}


