﻿var __skipAnimationNext = false;
var __restoreCollection = false;
var __targetCollection = null;
var __isDirectTabLink = false;

function startupTabs(async)
{
	// hook up the event handler to size the scrollable area on window resize
	$(window).resize(function()
	{
		autoSizeScrollableArea()
		fixNavMapPosition()
	});

	// if no anchor in the URL dont do anything
	if (window.location.hash != '')
	{
		// parse out just the tab name (if there is a history path)
		var tabName = getHistoryPath(0);

		// fix the naming discrepancy for pricing guide
		if (tabName == 'pricing-guide') tabName = 'price-guide';

		// if a collection is specified for collection, set flag to switch
		if (tabName == 'new-homes' && getHistoryPath(1) != '')
		{
			__restoreCollection = true;
		}

		// make sure this is loaded first
		setupPageManager();

		// do the tab switch now
		if (!selectTab(tabName, true))
		{
			// if there was no postback because we are already on the tab, do the scroll now
			if (__restoreCollection) scrollToTabs();
		}
	}
	else
	{
		if (__isDirectTabLink) scrollToTabs();
		__isDirectTabLink = false;
	}
}

function selectTab(tabName, skipAnimation)
{
	if (skipAnimation) __skipAnimationNext = true;

	// if a specific tab was requested, we need to force an
	// ajax postback by simulating a postback from the tab
	// links in the tab bar. First we need to find the right tab
	var links = $get('tabs_bar').getElementsByTagName('A');
	var target = null;
	for (var iLink = 0; iLink < links.length; iLink++)
	{
		if (links[iLink].getAttribute('tabpath') == tabName)
		{
			target = links[iLink];
			break;
		}
	}

	// if the tab was found and is not already selected, select it now
	if (target != null && target.className.toLowerCase().indexOf(' selected') == -1)
	{
		// run the async postback now
		var postback = replaceAll(target.href.replace('javascript:', ''), '%20', ' ');
		eval(postback);
		return true;
	}
	return false;
}

function scrollToTabs()
{
	// force the browser to recalc map position
	fixNavMapPosition();

	// get the top and bottom of the tabs bar so we can make scroll decisions
	var tabsBarTop = CommonToolkitScripts.getLocation($get('tabs_bar')).y;

	scrollPageTo("#tabs_bar", -60, function()
	{
		// reset the skip link for next action
		__skipAnimationNext = false;

		// if the history got erased from the url somehow, restore it
		checkHistoryPath();

		// check to see if there is a pending collection scroll operation
		if (__restoreCollection) restoreCollection();
		if (__targetCollection != null) selectCollection(__targetCollection);
	});
}

function autoSizeScrollableArea()
{
	// get the top of the tabs bar so we can calculate minimum height starting from here
	var tabsBarTop = CommonToolkitScripts.getLocation($get('tabs_bar')).y;

	// explicitly set the body's height based on the size of the viewport plus the height of the 
	// header area. This fixes the problem with having enough room to scroll when switching tabs.
	var minheight = (tabsBarTop - 60) + viewableHeight();
	var curheight = CommonToolkitScripts.getBounds($get('body')).height;

	if (Sys.Browser.agent == Sys.Browser.InternetExplorer && Sys.Browser.version <= 6)
	{
		if (curheight < minheight) { $get('body').style.height = minheight + 'px' };
	}
	else
	{
		$get('body').style.minHeight = minheight + 'px';
	}
}