$(document).ready(function()
{
	$("#index .column").each(function()
	{
		$("li:even", $(this)).addClass("odd");
	});
	$("form.request-consult").submit(function()
	{
		form = $(this);
		variables = "";
		error = false;
		for (i = 0; i < $("input[type=hidden]", form).length; i++)
		{
			e = $(("input[type=hidden]:eq(" + i + ")"), form);
			variables += "&" + e.attr("name") + "=" + e.val();
			if (e.attr("name") == "type") {
				var form_type = e.val();
			}
		}

		for (i = 0; i < $("input[type=text]", form).length; i++)
		{
			e = $(("input[type=text]:eq(" + i + ")"), form);
//console.log("input[type=text]:eq(" + i + ") Value: " + e.val());
			if (e.val() == "")
			{
				if ((e.attr("name") == "phone" && form_type == "contact")
					|| (e.attr("name") == "existing_site")) {				
				} 
				else {
					e.addClass("error");
					e.prev().addClass("error");
					error = true;
				}
			}
			else
				variables += "&" + e.attr("name") + "=" + e.val();
		}
		for (i = 0; i < $("select", form).length; i++)
		{
			e = $(("select:eq(" + i + ")"), form);
			if (e.val() == "0")
			{
				e.addClass("error");
				e.prev().addClass("error");
				error = true;
			}
			variables += "&" + e.attr("name") + "=" + e.val();
		}
		for (i = 0; i < $("textarea", form).length; i++)
		{
			e = $(("textarea:eq(" + i + ")"), form);
			if (e.val() == "")
			{
				e.addClass("error");
				e.prev().addClass("error");
				error = true;
			} 
			variables += "&" + e.attr("name") + "=" + e.val();
		}
		variables = variables.substring(1) + "&ajax=true";
//console.log(variables);
		if (error)
		{
			$("p.error", form).removeClass('hidden');
		}
		else
		{
			$.ajax(
			{
				beforeSend: function()
				{
					$("input[type=submit]", form).remove();
					$("fieldset", form).append('<span>Sending...</span>');
				},
				type: "POST",
				url: "../includes/contact.php",
				data: variables,
				success: function(result)
				{
//console.log(result);
					$("fieldset", form).html(result);
				}
			});
		}
		return false;
	});
	$("form.request-consult input[type=text], form.request-consult textarea").keypress(function()
	{
		$(this).removeClass("error").prev().removeClass("error");
	});
	$("form.request-consult select").change(function()
	{
		$(this).removeClass("error").prev().removeClass("error");
	});
	$("a.modal").click(function()
	{
		$("body").append('<div id="modal-container"></div>');
		$("#modal-container").load($(this).attr("href"), function()
		{
			$ss = $("#modal .slideshow");
			$ss.cycle(
			{
				fx: "fade",
				speed: 800,
				timeout: 4000
			});
			$("#modal, #modal-layer").fadeIn(600);
		});
		return false;
	});
	$("#modal a.btn_close, #modal-layer").live("click", function()
	{
		$("#modal, #modal-layer").fadeOut(600, function()
		{
			$("#modal-container").remove();
		});
		return false;
	});
});