// ------------------------------------------------------------
// 
// ------------------------------------------------------------
var g_headerConfig = {

	"home": {
		fullSize: { w: 394, h: 113 },
		// fullPos: { x: 9, y: 80 },
		fullPos: { x: 9, y: 59 },
		scaleMiniSize: { w: 199, h: 57 },
		miniSize: { w: 164, h: 57 },
		// miniPos: { x: 0, y: 21 },
		miniPos: { x: 0, y: 0 },
		headerImg: "head_services.gif"
	},

	"services": {
		fullSize: { w: 295, h: 103 },
		fullPos: { x: 162, y: 101 },
		miniSize: { w: 295, h: 103 },
		miniPos: { x: 181, y: 7 },
		headerImg: "head_services.gif"
	},

	"solutions": {
		fullSize: { w: 283, h: 91 },
		fullPos: { x: 349, y: 108 },
		miniSize: { w: 283, h: 91 },
		miniPos: { x: 349, y: 7 },
		headerImg: "head_solutions.gif"

	},

	"tom-brakke": {
		fullSize: { w: 349, h: 105 },
		fullPos: { x: 251, y: 104 },
		miniSize: { w: 349, h: 105 },
		miniPos: { x: 508, y: 6 },
		headerImg: "head_tom_brakke.gif"

	},

	"log-in": {
		fullSize: { w: 232, h: 102 },
		fullPos: { x: 190, y: 106 },
		miniSize: { w: 232, h: 102 },
		miniPos: { x: 181, y: 41 },
		headerImg: "head_log_in.gif"

	},

	"register": {
		fullSize: { w: 264, h: 98 },
		fullPos: { x: 237, y: 108 },
		miniSize: { w: 264, h: 98 },
		miniPos: { x: 295, y: 41 },
		headerImg: "head_register.gif"

	},

	"contact": {
		fullSize: { w: 253, h: 100 },
		fullPos: { x: 292, y: 111 },
		miniSize: { w: 253, h: 100 },
		miniPos: { x: 420, y: 38 },
		headerImg: "head_contact.gif"

	},

	"disclosures": {
		fullSize: { w: 334, h: 106 },
		fullPos: { x: 258, y: 102 },
		miniSize: { w: 334, h: 106 },
		miniPos: { x: 550, y: 38 },
		headerImg: "head_disclosures.gif"
	}

};

// ------------------------------------------------------------
// 
// ------------------------------------------------------------
$(document).ready(
	function ()
	{	
		$("dl#main dd.nav-main ul li a").click(handleNavLink);
		$("dl#main dd.footer ul li:not(.blog) a").click(handleNavLink);

		$("dl#main dt.logo a").click(function ()
		{
			activateSection("home");
			
			$(this).blur();
			
			return false;
		});
		
		// if (self.location.hash)
		// {
		activateSection(self.location.hash ? self.location.hash.substring(1) : "home", true);
		// }
		// else
		// {
		// $("dl#main dd.content-wrapper-home").show();
		// $("dl#main dd.content-wrapper-home").addClass("content-wrapper-active");
		// }
	}
);

// ------------------------------------------------------------
// 
// ------------------------------------------------------------
function handleNavLink ()
{
	this.blur();
	
	activateSection($(this).parent()[0].className);
	
	return false;
};

// ------------------------------------------------------------
// 
// ------------------------------------------------------------
function activateSection (section, fast)
{
	if (section == "home")
	{
		if ($("dl#main").is(".home"))
		{
			return false;
		};
	
		maximizeLogo(fast);
		
		minimizeActiveHeader(fast);

		loadContent("home", fast);
	}
	else
	{
		var navEl = $("dl#main dd.nav-main li." + section);
		
		if (navEl.is(".active"))
		{
			return false;
		};
		
		self.scrollTo(0, 0);
	
		// console.log($("li.active", navEl.parent()).length);
	
		if ($("li.active", navEl.parent()).length)
		{
			minimizeActiveHeader(fast);
		}
		else
		{
			minimizeLogo(fast);
		};
	
		// var section = $(this).parent()[0].className;
		
		maximizeHeader(section, fast);
		
		loadContent(section, fast);
	
		navEl.addClass("active");
	};
};

// ------------------------------------------------------------
// 
// ------------------------------------------------------------
var g_loadTime = null;
var g_content = null;
var g_loadDelay = 1500;

function loadContent (section, fast)
{
	// set loading
	$("#loading").show();
	
	/*
	$("dl#main").removeClass("home interior services solutions tom-brakke log-in register contact disclosures");

	$("dl#main").addClass(section);
	
	if (section != "home")
	{
		$("dl#main").addClass("interior");
	};

	self.location.hash = section;
	*/

	g_loadTime = (new Date()).getTime() + g_loadDelay;
	
	// console.log(fast);
	// console.log(section);
	
	if (fast)
	{
		$("dl#main dd.content-wrapper-active").hide();
		$("dl#main dd.content-wrapper-" + section).show();
		$("dl#main dd.content-wrapper-" + section).addClass("content-wrapper-active");
		
		updateSection(section);
		
		// undo loading
		$("#loading").hide();
	}
	else
	{
		setTimeout(function ()
		{
			$("dl#main dd.content-wrapper-active").hide();
			$("dl#main dd.content-wrapper-" + section).show();
			$("dl#main dd.content-wrapper-" + section).addClass("content-wrapper-active");
			
			updateSection(section);
			
			// undo loading
			$("#loading").hide();
		}, g_loadDelay);
	};
	
	/*
	$.get("content.php", { section: section, d: (new Date()).getTime() }, 
		function (data)
		{
			var now = (new Date()).getTime();
			
			if (fast || now > g_loadTime)
			{
				updateSection(section);
				updateContent(data, section);
			}
			else
			{
				setTimeout(function ()
				{
					updateSection(section);
					updateContent(data);
				}, g_loadTime - now);
			};
		}
	);
	*/
};

// ------------------------------------------------------------
// 
// ------------------------------------------------------------
function updateContent (content)
{
	// content = content.replace(/\n|\r/g, "");
	
	// console.log(content);
	// console.log($(content).html());
	
	// var a = $("<div><h2>sup</h2><p>ohhai</p></div>");
	// console.log($("p", a).length);

	var update = $(content);
	
	/*
	$("dl#main dd.content").html($("div#content", update).html());
	$("dl#main dd.sidebar").html($("div#sidebar", update).length ? $("div#sidebar", update).html() : "");

	// hack to get ie to behave
	$("dl#main dd.content").width($("dl#main dd.content").width());
	*/
	
	$("dl#main dd.content-wrapper").html(update);
	
	// undo loading
	$("#loading").hide();
	
};

// ------------------------------------------------------------
// 
// ------------------------------------------------------------
function updateSection (section)
{
	$("dl#main").removeClass("home interior services solutions tom-brakke log-in register contact disclosures");

	$("dl#main").addClass(section);
	
	if (section != "home")
	{
		$("dl#main").addClass("interior");
	};

	// self.location.hash = section == "home" ? "" : section;
	self.location.hash = section;
};

// ------------------------------------------------------------
// 
// ------------------------------------------------------------
function minimizeLogo (fast)
{
	var logoEl = $("dl#main dt.logo");
	var logoImg = $("img", logoEl);
	var logoLinkEl = $("a", logoEl);
	var config = g_headerConfig["home"];

	logoEl.css("z-index", 200);
	
	if (fast)
	{
		logoLinkEl.css({
			left: config.miniPos.x,
			top: config.miniPos.y
		});

		logoLinkEl.width(config.miniSize.w);
		logoLinkEl.height(config.miniSize.h);
		
		logoImg.hide();

		logoEl.css("z-index", 102);
	}
	else
	{
		logoLinkEl.animate({
			left: config.miniPos.x,
			top: config.miniPos.y
			// height: 103
		}, 600, "swing", function ()
		{
	
		});
		
		logoImg.animate({
			width: config.scaleMiniSize.w,
			height: config.scaleMiniSize.h
		}, 600, "swing", function ()
		{
			// logoImg.width(config.miniSize.w);
			// logoImg.height(config.miniSize.h);
			// logoImg.attr("src", "_resources/img/global/img_logo_small.gif");
	
			logoLinkEl.width(config.miniSize.w);
			logoLinkEl.height(config.miniSize.h);
			
			logoImg.hide();
	
			logoEl.css("z-index", 102);
		});
	};
};

function maximizeLogo (fast)
{
	var logoEl = $("dl#main dt.logo");
	var logoImg = $("img", logoEl);
	var logoLinkEl = $("a", logoEl);
	var config = g_headerConfig["home"];

	// logoImg.attr("src", "_resources/img/global/img_logo.gif");

	logoImg.show();

	logoEl.css("z-index", 200);

	logoLinkEl.width("auto");
	logoLinkEl.height("auto");
	
	if (fast)
	{
		logoLinkEl.css({
			left: config.fullPos.x,
			top: config.fullPos.y
			// height: 103
		});
		
		logoImg.width(config.fullSize.w);
		logoImg.height(config.fullSize.h);
		
		logoEl.css("z-index", 102);
	}
	else
	{
		logoLinkEl.animate({
			left: config.fullPos.x,
			top: config.fullPos.y
			// height: 103
		}, 600, "swing", function ()
		{
	
		});
	
		logoImg.animate({
			width: config.fullSize.w,
			height: config.fullSize.h
		}, 600, "swing", function ()
		{
			logoEl.css("z-index", 102);
		});
	};
};

// ------------------------------------------------------------
// 
// ------------------------------------------------------------
function minimizeActiveHeader (fast)
{
	var activeEl = $("dl#main dd.page-title span:visible");
	
	if (!activeEl.length) return;
	
	var activeImg = $("img", activeEl);
	var activeSection = activeEl[0].className.replace(" active", "");
	var activeConfig = g_headerConfig[activeSection];
						
	activeImg.css("top", 0);
	
	if (fast)
	{
		activeEl.hide();
		$("dl#main dd.nav-main li." + activeSection).removeClass("active");
	
		activeImg.width(Math.round(activeConfig.fullSize.w / 2));
		activeImg.height(activeConfig.fullSize.h);
	}
	else
	{
		activeEl.animate({
			left: activeConfig.miniPos.x,
			top: activeConfig.miniPos.y,
			height: Math.round(activeConfig.fullSize.h / 2) - 5
		}, 600, "swing", function ()
		{
			activeEl.hide();
			
			$("dl#main dd.nav-main li." + activeSection).removeClass("active");
			
		});
	
		activeImg.animate({
			width: Math.round(activeConfig.fullSize.w / 2),
			height: activeConfig.fullSize.h
		}, 600, "swing", function ()
		{
			
		});
	};
};

function maximizeHeader (section, fast)
{
	var titleEl = $("dl#main dd.page-title span." + section);
	var titleImg = $("img", titleEl);
	var config = g_headerConfig[section];

	titleImg.attr("src", "_resources/img/page_titles/" + config.headerImg);
	
	titleImg.css("top", 0);
	
	titleEl.css({
		top: config.miniPos.y,
		left: config.miniPos.x,
		width: config.fullSize.w,
		height: Math.round(config.fullSize.h / 2) - 5
	});
	
	titleImg.css({
		width: Math.round(config.fullSize.w / 2),
		height: Math.round(config.fullSize.h)
	});
	
	titleImg.show();
			
	if (fast)
	{
		titleEl.show();
	
		titleEl.css({
			left: config.fullPos.x,
			top: config.fullPos.y
		});
		
		titleEl.height(config.fullSize.h);
	
		titleImg.width(config.fullSize.w);
		titleImg.height(config.fullSize.h * 2);
		
		titleImg.css("top", -1 * config.fullSize.h);
	}
	else
	{		
		titleEl.animate({
			left: config.fullPos.x,
			top: config.fullPos.y,
			height: config.fullSize.h
		}, 600, "swing", function ()
		{
	
		});
	
		titleImg.animate({
			width: config.fullSize.w,
			height: config.fullSize.h * 2
		}, 600, "swing", function ()
		{
			$(this).css("top", -1 * config.fullSize.h);
		});
	};
};

// ------------------------------------------------------------
// 
// ------------------------------------------------------------
/*
function blurLink ()
{
	this.blur();
};
*/
