﻿function selectCollection(name, noHistory)
{
	__targetCollection = null;

	var con = $('#collection_body');

	// hide all tabs
	con.find(".tab_collection").css("display", "none");
	con.find(".tab_collection[colname='" + name + "']").css("display", "block");

	// load the sliding buttons if needed
	toggleCollectionArrows()

	// force it to recalculate map position
	fixNavMapPosition();

	// set the new history path
	if (!noHistory) setHistoryPath('new-homes/' + name);
}

function restoreCollection()
{
	// first thing, reset the flag
	__restoreCollection = false;

	// if no anchor in the URL dont do anything
	if (window.location.hash == '') return;

	var colName = getHistoryPath(1);
	var pageIndex = getHistoryPath(2);
	if (colName == '') return;

	// make sure we have the collection loaded now
	if ($("#collection_body").length == 0) return;

	// select the requested collection sub-tab (if requested)
	if (colName != '')
	{
		selectCollection(colName, true);

		// force collection to scroll to proper page if past page 1
		if (pageIndex != '' && parseInt(pageIndex) > 1)
		{
			slideCollectionRight(parseInt(pageIndex), true);
		}
	}
}

function getActiveCollection()
{
	return $("#collection_body .tab_collection:visible");
}

function getActiveCollectionPath()
{
	return getActiveCollection().attr('colname');
}

function enableCollectionArrow(el, visible)
{
	el.css("visibility", (visible ? 'visible' : 'hidden'));
}

function toggleCollectionArrows()
{
	// get the visible collection. if none, we are on different tab
	var col = getActiveCollection();
	if (!col || col.length == 0) return;

	var left = col.find(".collection_leftarrow");
	var right = col.find(".collection_rightarrow");
	var slider = col.find(".collection_slider");
	var plans = slider.find("table:first").find("tr:first").children("td");

	// figure out how many pages there are
	var pages = parseInt(plans.length / 5);
	if (pages < (plans.length / 5)) pages++;

	// set the page index if not already set
	var pageIndex = slider.data("pageIndex");
	if (!pageIndex) pageIndex = 1;

	// configure sliding buttons based on our page position
	enableCollectionArrow(left, (pageIndex > 1));
	enableCollectionArrow(right, (pageIndex < pages));

	// store paging settings
	slider.data("pages", pages);
	slider.data("pageIndex", pageIndex)
}

function slideCollectionLeft()
{
	// get the visible collection. if none, we are on different tab
	var col = getActiveCollection();
	if (!col || col.length == 0) return;

	var outer = col.find(".collection_plans");
	var inner = col.find(".collection_slider");
	var table = inner.find("table:first");
	var plans = table.find("tr:first").children("td");

	// measure the slider and its container
	var innerWidth = table.outerWidth();
	var outerWidth = outer.width();

	// determine the current left position
	var top = inner.position().top;
	var fromLeft = inner.position().left;

	// go back to the previous page
	var pageIndex = inner.data("pageIndex");
	pageIndex--;
	if (pageIndex < 1) pageIndex = 1;
	inner.data("pageIndex", pageIndex);

	// the position to move to is the first plan on the next page
	var toLeft = plans.eq((pageIndex - 1) * 5).position().left * -1;
	if (fromLeft == toLeft) return;

	// hide the left border on subsequent plans
	if (pageIndex > 1) toLeft -= 1;

	if (col.data("sliding")) return;
	col.data("sliding", true);

	// set the new history path
	var path = getActiveCollectionPath()
	setHistoryPath('new-homes/' + path + '/' + pageIndex);
	
	// do the slide animation
	inner.animate({ left: toLeft }, "slow", finishCollectionSlide);
}

function slideCollectionRight(page, noHistory)
{
	// get the visible collection. if none, we are on different tab
	var col = getActiveCollection();
	if (!col || col.length == 0) return;

	var outer = col.find(".collection_plans");
	var inner = col.find(".collection_slider");
	var table = inner.find("table:first");
	var plans = table.find("tr:first").children("td");

	// measure the slider and its container
	var innerWidth = table.outerWidth();
	var outerWidth = outer.width();

	// determine the current left position
	var top = inner.position().top;
	var fromLeft = inner.position().left;

	// get the page settings
	var pages = inner.data("pages");
	var pageIndex = inner.data("pageIndex");
	
	if (page != null)
	{
		// advance to specific page if requested
		pageIndex = parseInt(page);
	}
	else
	{
		// advance to the next page
		pageIndex += 1;
	}
	if (pageIndex > pages) pageIndex = pages;
	inner.data("pageIndex", pageIndex);

	// the position to move to is the first plan on the next page
	var toLeft = plans.eq((pageIndex - 1) * 5).position().left * -1;
	var maxLeft = plans.eq((plans.length - 5)).position().left * -1;
	if (toLeft < maxLeft) toLeft = maxLeft;
	if (fromLeft == toLeft) return;

	// hide the left border on subsequent plans
	if (pageIndex > 1) toLeft -= 1;

	if (!noHistory)
	{
		// set the new history path
		var path = getActiveCollectionPath()
		setHistoryPath('new-homes/' + path + '/' + pageIndex);
	}
	
	if (page != null)
	{
		// if specific page requested, dont do animation
		inner.css("left", toLeft);
		toggleCollectionArrows();
	}
	else
	{
		if (col.data("sliding")) return;
		col.data("sliding", true);
		inner.animate({ left: toLeft }, "slow", finishCollectionSlide);
	}
}

function finishCollectionSlide()
{
	window.setTimeout('finishCollectionSlideAsync();', 50);
}

function finishCollectionSlideAsync()
{
	// get the visible collection. if none, we are on different tab
	var col = getActiveCollection();
	if (!col || col.length == 0) return;

	// reset the sliding flag
	col.data("sliding", false);

	// toggle the buttons based on the slide
	toggleCollectionArrows();
}



