 $(document).ready(function() {
	// Apply Rollover Logic to Secondary Navigation
	$("#rollovers a").each(function() {
		var image = $(this).children("img");
		var imgsrc = $(image).attr("src");

		$(this).mouseover(function() {
			if (imgsrc.indexOf("_active") == -1) {
				var on = imgsrc.replace(/.gif$/gi,"_hover.gif").replace(/.jpg$/gi,"_hover.jpg");
				$(image).attr("src",on);
			}
		});

		$(this).mouseout(function() {
			$(image).attr("src",imgsrc);
		});
	});

	// Apply Rollover Logic to Submit Button
	$("#rollovers input").each(function() {
		var imgsrc = $(this).attr("src");

		$(this).mouseover(function() {
			if (imgsrc.indexOf("_active") == -1) {
				var on = imgsrc.replace(/.gif$/gi,"_hover.gif").replace(/.jpg$/gi,"_hover.jpg");
				$(this).attr("src",on);
			}
		});

		$(this).mouseout(function() {
			$(this).attr("src",imgsrc);
		});
	});

	// Apply Rollover Logic to SitePlan Navigation
	$("#siteplan area").each(function() {

		var menu = "menu" + $(this).attr("id");
		var plan = "plan" + $(this).attr("id");
	
		$(this).mouseover(function() {
			for (i=1;i<=11;i++) {
				hide("menu"+i);
				hide("plan"+i);
			}

			display(menu);
			display(plan);
		});

		$(this).mouseout(function() {
		});
	});

		
	$("#commentForm").validate({	
		submitHandler: function(form) {
		
			$("#contactusError").hide();
			var validationResult = validateContactUsForm(form);
			if (validationResult['isValid']) {			
			
				$.ajax({
					type: "post", url: "./emailer/send.php"
				,	data: $("#commentForm").serialize()
				,	success: function (data, text) {
						if (text == "success" && data == "true") {
							$("#error_message").html("Thank you for your interest in Kahala Nui. A representative will contact you directly.");
							$("#contactusError").show();								
							form.reset();
						}
						else {
							$("#error_message").html("Sorry, there was an error processing your request.  Please try again.");
							$("#contactusError").show();								
						}
					}
				,	error: function (request, status, error) {
						$("#error_message").html("Sorry, there was an error processing your request.  Please try again.");
						$("#contactusError").show();								
					}
				});		
				
			}
			else {
				$("#error_message").html(validationResult['message']);
				$("#contactusError").show();			
			}
			
			return false;
		}
	 });
	 
	 // Reset Text Size
	 try {
	 	textSize = parseInt(readCookie("textSize"));
	 	if (isNaN(textSize)) {
	 		textSize = 0;
	 	}
	 }
	 catch(err) {
	 	textSize = 0;
	 }

	 setTextSize(textSize);	 
 });



function validateContactUsForm(form) {

	$("#commentForm").find('#error').hide();

	if (!(validateNotEmpty($("#FirstName").val()) && validateNotEmpty($("#LastName").val()))) {

		if (!validateNotEmpty($("#LastName").val())) {
			$("#LastName").siblings('#error').show();
			$("#LastName").focus();
		}

		if (!validateNotEmpty($("#FirstName").val())) {
			$("#FirstName").siblings('#error').show();
			$("#FirstName").focus();
		}

		return	{
			message:"Please provide us with a first and last name."
		,	isValid:false
			};
	}

	if (!(validateNotEmpty($("#Address1").val()) || validateNotEmpty($("#City").val()) || validateNotEmpty($("#ZipCode").val()) || validateNotEmpty($("#Email").val()) || validateNotEmpty($("#Telephone").val()))) {

		if (!validateNotEmpty($("#Telephone").val())) {
			$("#Telephone").siblings('#error').show();
			$("#Telephone").focus();
		}

		if (!validateNotEmpty($("#Email").val())) {
			$("#Email").siblings('#error').show();
			$("#Email").focus();
		}

		if (!validateNotEmpty($("#ZipCode").val())) {
			$("#ZipCode").siblings('#error').show();
			$("#ZipCode").focus();
		}

		if (!validateNotEmpty($("#City").val())) {
			$("#City").siblings('#error').show();
			$("#City").focus();
		}

		if (!validateNotEmpty($("#Address1").val())) {
			$("#Address1").siblings('#error').show();
			$("#Address1").focus();
		}

		return	{
			message:"Please provide us with either a complete address, a telephone number, or an email address."
		,	isValid:false
			};
	}

	if (validateNotEmpty($("#Address1").val()) || validateNotEmpty($("#City").val()) || validateNotEmpty($("#ZipCode").val())) {
		if (!(validateNotEmpty($("#Address1").val()) && validateNotEmpty($("#City").val()) && validateUSZip($("#ZipCode").val()))) {

			if (!validateUSZip($("#ZipCode").val())) {
				$("#ZipCode").siblings('#error').show();
				$("#ZipCode").focus();
			}

			if (!validateNotEmpty($("#City").val())) {
				$("#City").siblings('#error').show();
				$("#City").focus();
			}

			if (!validateNotEmpty($("#Address1").val())) {
				$("#Address1").siblings('#error').show();
				$("#Address1").focus();
			}


			return	{
				message:"Please provide us with a complete address."
			,	isValid:false
				};
		}
	}

	if (validateNotEmpty($("#Telephone").val()) && !validateUSPhone($("#Telephone").val())) {

		if (!validateUSPhone($("#Telephone").val())) {
			$("#Telephone").siblings('#error').show();
			$("#Telephone").focus();
		}


		return	{
			message:"Please provide us with a valid telephone number, including area code."
		,	isValid:false
			};
	}

	if (validateNotEmpty($("#Email").val()) && !validateEmail($("#Email").val())) {

		if (!validateEmail($("#Email").val())) {
			$("#Email").siblings('#error').show();
			$("#Email").focus();
		}

		return	{
			message:"Please provide us with a valid email address."
		,	isValid:false
			};			
	}

	return	{
		message:""
	,	isValid:true
		};
}
	 
function display(id) {
	$('#' + id).css("display", "inline");
}

function hide(id) {
	$('#' + id).css("display", "none");
}

var textSize = 0, maxTextSize = 2, minTextSize = 0;
function enlargeText() {
	if (textSize < maxTextSize) {
		setTextSize(1);
		createCookie("textSize", ++textSize);
	}
}

function reduceText() {
	if (textSize > minTextSize) {
		setTextSize(-1);
		createCookie("textSize", --textSize);
	}
}

function setTextSize(amount) {	
	if (isNaN(amount))
		return;
		
	$('#primary_content').find('#headline, #subhead, #main, #rule_lines, #residence_info').each(function(i,val) {		
		$(val).css('font-size', parseFloat($(val).css('font-size'))+amount+'px');		
	});
	
	$('.resizable').each(function(i,val) {		
		$(val).css('font-size', parseFloat($(val).css('font-size'))+amount+'px');		
	});

}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=535,height=630');");
}

