// ------------------------------------------------------------
// 
// ------------------------------------------------------------
var g_enableTombstoneAnimation = true;

$(document).ready(
	function() 
	{
		if (g_enableTombstoneAnimation)
		{
			initTombstoneAnimation();
		};
		
		initTombstoneLinks();
		
		initEmailLinks();
	}
);

// ------------------------------------------------------------
// 
// ------------------------------------------------------------
function initEmailLinks ()
{
	$("a.email").click(
		function ()
		{
			var linkEl = $(this);
			
			linkEl.attr("href", "mailto:" + linkEl.attr("recipient") + "@" + "champlainadvisors.com");
			
			return true;
		}
	);
};

// ------------------------------------------------------------
// 
// ------------------------------------------------------------
function initTombstoneLinks ()
{
	$("ol#tombstones li").click(
		function ()
		{
			window.open($("a", $(this)).attr("href"));
		}
	);
};

// ------------------------------------------------------------
// 
// ------------------------------------------------------------
var g_browserWidth = 0;
var g_tombstoneCount = 0;

function initTombstoneAnimation ()
{
	if ($("ol#tombstones li").length)
	{
		positionTombstones();
		
		g_browserWidth = getBrowserWidth();
		
		setInterval(monitorBrowserWindow, 100);
		
		$("ol#tombstones li").hide();
				
		g_tombstoneCount = $.cookie("client_index") ? parseInt($.cookie("client_index")) : 0;
		
		showTombstone($("ol#tombstones li").eq(g_tombstoneCount));
	};
};

function showTombstone (el)
{
	el.addClass("active");
	
	$.cookie("client_index", g_tombstoneCount, { path: "/" });
	
	g_tombstoneCount = (++g_tombstoneCount) % $("ol#tombstones li").length;
		
	el.fadeIn(1000, function ()
	{
		setTimeout(hideTombstone, 3000);
	});
};

function hideTombstone ()
{
	var el = $("ol#tombstones li.active");
	
	el.fadeOut(1000, function ()
	{
		el.removeClass("active");
		el.hide();
	
		if (el.next("li").length)
		{
			showTombstone(el.next("li"));
		}
		else
		{
			showTombstone($("ol#tombstones li").eq(0));
		};
	});
};

function positionTombstones ()
{
	$("ol#tombstones").css({
		top: $("dl#main").offset().top + 269,
		left: $("dl#main").offset().left + 516
	});
};

function getBrowserWidth ()
{
	if (typeof(window.innerWidth) == "number") 
	{
		return window.innerWidth;
	} 
	else 
	{
		return document.documentElement.clientWidth;
	};
};

function monitorBrowserWindow ()
{
	if (g_browserWidth != getBrowserWidth())
	{
		positionTombstones();
	};
};
