// ------------------------------------------------------------
// 
// ------------------------------------------------------------
var g_animationTimeoutId = null;

// ------------------------------------------------------------
// 
// ------------------------------------------------------------
$(document).ready(
	function() 
	{
		initBios();
		
		initFontSizeControls();
	}
);

// ------------------------------------------------------------
// 
// ------------------------------------------------------------
function initFontSizeControls ()
{
	$("dl#main dd.nav-main li.increase-font-size a, dl#main dd.nav-main li.decrease-font-size a").click(handleFontSizeAdjust);
};

function handleFontSizeAdjust ()
{
	if ($("dl#main dd.content-wrapper div.content p").length == 0) return false;

	var fontSize = parseInt($("dl#main dd.content-wrapper div.content p").eq(0).css("font-size"));
	var lineHeight = parseInt($("dl#main dd.content-wrapper div.content p").eq(0).css("line-height"));
	var d = 2;
	
	if ($(this).parent().is(".increase-font-size"))
	{
		$("dl#main dd.content-wrapper div.content p").css("font-size", (fontSize + d) + "px");
		$("dl#main dd.content-wrapper div.content p").css("line-height", (lineHeight + d) + "px");
	}
	else
	{
		$("dl#main dd.content-wrapper div.content p").css("font-size", (fontSize - d) + "px");
		$("dl#main dd.content-wrapper div.content p").css("line-height", (lineHeight - d) + "px");
	};
	
	return false;
};

// ------------------------------------------------------------
// 
// ------------------------------------------------------------
var g_bioDelay = 4000;
var g_bioTransitionSpeed = 500;

function initBios ()
{
	$("dl#main dd.nav-meet-the-people ul li a.label").click(handleBio);
};

function handleBio ()
{
	if (!$(this).parent().is(".active"))
	{
		clearTimeout(g_animationTimeoutId);
		
		loadBio($(this));
	};
	
	return false;
};

function loadBio (personEl)
{
	var duration = 750;
			
	$("img", personEl.parent())
		.attr("src", "_resources/img/nav_meet_the_people/img_" + personEl.parent().attr("name") + "_1.jpg");
		
	$("a.image", personEl.parent()).css("opacity", 1).removeAttr("href");
	
	for (var i = 2; i <= 4; i++)
	{
		if (!$("#preload-" + personEl.parent().attr("name") + "-" + i).length)
		{
			$("body").append("<img id=\"preload-" + personEl.parent().attr("name") + "-" + i + "\" src=\"_resources/img/nav_meet_the_people/img_" + personEl.parent().attr("name") + "_" + i + ".jpg\" class=\"preload\" />");
		};
	};
	
	personEl.parent().prevAll(":not(.expanded)").addClass("expanded").animate({
		left: "-=802"
	}, {
		duration: duration,
		easing: "swing"
	});

	$("dl#main dd.nav-meet-the-people ul li.active").removeClass("active").animate({
		width: "-=802"
	}, {
		duration: duration,
		easing: "swing",
		queue: false
	});

	var activeElementSettings = {
		width: "+=802"
	};
	
	if (!(personEl.parent().is(".expanded")))
	{
		activeElementSettings.left = "-=802";
	};
	
	personEl.parent().animate(activeElementSettings, {
		duration: duration,
		easing: "swing",
		queue: false,
		complete: function () { startBioTransition(personEl); }
	});

	personEl.parent().nextAll(".expanded").removeClass("expanded").animate({
		left: "+=802"
	}, {
		duration: duration,
		easing: "swing",
		queue: false
	});
	
	personEl.parent().addClass("active").addClass("expanded");
};

function startBioTransition (personEl)
{
	g_animationTimeoutId = setTimeout(function () { updateBioImage(personEl); }, g_bioDelay);
};

function updateBioImage (personEl)
{
	var imageEl = $("img", personEl.parent());
	var linkEl = $("a.image", personEl.parent());
		
	linkEl.animate({
		opacity: 0
	}, {
		duration: g_bioTransitionSpeed,
		easing: "swing",
		complete: function ()
		{
			var imageSrc = imageEl.attr("src");
			var imageIndex = parseInt(imageSrc.charAt(imageSrc.length - 5));
			
			imageEl.attr("src", imageSrc.replace("_" + imageIndex + ".jpg", "_" + (imageIndex + 1) + ".jpg"));
			
			linkEl.animate({
				opacity: 1
			}, {
				duration: g_bioTransitionSpeed,
				easing: "swing",
				complete: function ()
				{
					if (imageIndex < 3)
					{
						g_animationTimeoutId = setTimeout(function () { updateBioImage(personEl); }, g_bioDelay);
					}
					else
					{
						var labelEl = $("a.label", personEl.parent());
					
						linkEl.attr("href", labelEl.attr("href"));
					};
				}
			});
		}
	});
};







