// --------------------------------------------------------
// 
// --------------------------------------------------------
$(document).ready(
	function ()
	{
		$("#contact-us").submit(handleContactSubmit);

		$("#tell-a-friend").submit(handleTellAFriendSubmit);
		
		initExpandableMenus();
		
		initSidebarAnimation();
		
		initSearch();
	}
);

// --------------------------------------------------------
// 
// --------------------------------------------------------
function initSearch ()
{
	$("form#search input.text").focus(
		function ()
		{
			if ($(this).val() == $(this).attr("defaultvalue"))
			{
				$(this).val("");
			};
		}
	);

	$("form#search input.text").blur(
		function ()
		{
			if ($(this).val() == "")
			{
				$(this).val($(this).attr("defaultvalue"));
			};
		}
	);
	
	$("form#search input.text").val($("form#search input.text").attr("defaultvalue"));
};

// --------------------------------------------------------
// 
// --------------------------------------------------------
var g_sidebarImageCount = 4;
var g_sidebarImageIndex = 0;

function initSidebarAnimation ()
{
	if ($("body").is(".home"))
	{
		setTimeout(updateSidebarImage, 4000);
	};
};

function updateSidebarImage ()
{
	var activeImage = $("#sidebar-right img.intro");
	
	activeImage.css("z-index", 11);
	
	var nextImageIndex = (++g_sidebarImageIndex % g_sidebarImageCount) + 1;
	
	var nextImage = $("<img src="http://acrossworld.com/""" + g_contentRoot + "/static/_resources/img/home/AW_homepage_" + nextImageIndex + ".jpg\" class=\"intro\" />").css({
		zIndex: 10
	}).appendTo("#sidebar-right");
	
	activeImage.animate({
		opacity: 0
	}, {
		duration: 3000,
		easing: "swing",
		complete: function () 
		{ 
			$(this).remove();
		}
	});

	nextImage.animate({
		opacity: 1
	}, {
		duration: 3000,
		easing: "swing",
		complete: function () 
		{
			setTimeout(updateSidebarImage, 4000);
		}
	});
};

// --------------------------------------------------------
// 
// --------------------------------------------------------
function initExpandableMenus ()
{
	$("dl.expandable dt a").click(handleExpandableMenuToggle);
};

function handleExpandableMenuToggle () 
{
	var el = $(this).parent().next("dd");

	if ($(this).parent().next("dd").is(":visible"))
	{
		el.animate({
			height: 0
		}, {
			duration: 400,
			complete: function ()
			{
				el.hide();

				el.height("auto");
			}
		});
		
		// $("span", $(this)).text("[+]");
	}
	else
	{
		// el.height(0).show();
		
		/*
		el.css({
			position: "absolute",
			visibility: "hidden",
			display: "block",
			overflow: "hidden",
			width: 180
		});
		*/
		
		var h = el.outerHeight();
		
		/*
		el.css({
			position: "relative",
			visibility: "visible",
			display: "block",
			height: 0
		});
		*/
		
		// el.show(); return false;
		
		// console.log(h);
		
		el.height(0).animate({
			height: h
		}, {
			duration: 400,
			complete: function ()
			{
			
			}
		});

		// $("span", $(this)).html("[&ndash;]");
	};
	
	return false;
};

// --------------------------------------------------------
// 
// --------------------------------------------------------
function handleTellAFriendSubmit ()
{
	var formEl = $("#tell-a-friend");
	var isValid  = true;
	
	$("div.error", formEl).hide();

	var val = $("input.name", formEl).eq(0).val().replace(/ /g, "");
								
	if (!val.length)
	{
		$("input.name", formEl).next(".error").show();
					
		isValid = false;
	};

	var val = $("input.email-address", formEl).eq(0).val().replace(/ /g, "");

	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if (!filter.test(val))
	{
		$("input.email-address", formEl).next(".error").show();

		isValid = false;
	};
	
	return isValid;
};

// --------------------------------------------------------
// 
// --------------------------------------------------------
function handleContactSubmit ()
{
	var formEl = $("#contact-us");
	var isValid  = true;
	
	$("div.error", formEl).hide();

	var val = $("input.name", formEl).eq(0).val().replace(/ /g, "");
								
	if (!val.length)
	{
		$("input.name", formEl).next(".error").show();
					
		isValid = false;
	};

	var val = $("input.phone", formEl).eq(0).val().replace(/ /g, "");
								
	if (!val.length)
	{
		$("input.phone", formEl).next(".error").show();
					
		isValid = false;
	};

	var val = $("textarea.message", formEl).eq(0).val().replace(/ /g, "");
				
	if (!val.length)
	{
		$("textarea.message", formEl).next(".error").show();

		isValid = false;
	};

	var val = $("input.email-address", formEl).eq(0).val().replace(/ /g, "");

	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if (!filter.test(val))
	{
		$("input.email-address", formEl).next(".error").show();

		isValid = false;
	};
	
	return isValid;
};
