﻿var productIDs = new Array();
var productTextID;
var productFaderID;
var productContainerID;
var productScrollToID;
var productFilterTopTextID;
var productFilterBottomTextID;
var windowLocationHash = '';
var locked = false;

$(document).ready(function()
{
	setInterval(function(){CheckWindowLocationHash()}, 100);
});

function CheckWindowLocationHash()
{
	if(!locked && windowLocationHash != window.location.hash)
	{
		windowLocationHash = window.location.hash;

		if(windowLocationHash.length > 1)
		{
			var elementID = windowLocationHash.substring(1, windowLocationHash.length);

			if(getById(elementID))
			{
				ProductDisplayStart(elementID);
			}
		}
	}
}

function ProductInitialise(elementID)
{
	getById(elementID).style.width = getById(elementID).offsetWidth + 'px';
	getById(elementID).style.height = getById(elementID).offsetHeight + 'px';
	getById(elementID).style.position = 'absolute';
	getById(elementID).style.display = 'none';
	productIDs.push(elementID);
}

function ProductFaderInitialise(elementID)
{
	getById(elementID).style.opacity = 1.0;
	productFaderID = elementID;
}

function ProductTextInitialise(elementID)
{
	getById(elementID).innerHTML = '<p>Please choose a product from the list above.</p>';
	productTextID = elementID;
}

function ProductContainerInitialise(elementID)
{
	productContainerID = elementID;
}

function ProductScrollToInitialise(elementID)
{
	productScrollToID = elementID;
}

function ProductFilterTextInitialise(topElementID, bottomElementID, filterText)
{
	getById(topElementID).innerHTML = '<div class="info"><span>These results have been filtered by ' + filterText + ' solutions.</span></div>';
	getById(bottomElementID).innerHTML = '<p><strong><a href="" onclick="ProductRemoveFilter(); return false;">To see the full list of core products offered in this category, click here</a>.</strong></p>';
	productFilterTopTextID = topElementID;
	productFilterBottomTextID = bottomElementID;
}

function ProductRemoveFilter()
{
	var count;

	for(count in productIDs)
	{
		getById('li' + productIDs[count]).style.display = 'block';
	}

	getById(productFilterTopTextID).style.display = 'none';
	getById(productFilterBottomTextID).style.display = 'none';
}

function ProductDisplayStart(elementID)
{
	locked = true;
	getById(productTextID).style.display = 'none';

	var count;

	for(count in productIDs)
	{
		getById('li' + productIDs[count]).className = '';
	}
	
	getById('li' + elementID).className = 'current';

	if(getById(productFaderID).style.opacity != 1.0)
	{
		getById(productFaderID).style.visibility = 'visible';
		ProductDisplayFade(true, elementID);
	}
	else
	{
		ProductDisplayResize(elementID);
	}
}

function ProductDisplayFade(fadeOutFirst, elementID)
{
	if(fadeOutFirst)
	{
		var opacity = Math.min(100, getById(productFaderID).style.opacity * 100 + 25);
		getById(productFaderID).style.opacity = opacity / 100.0;
		getById(productFaderID).style.filter = 'alpha(opacity = ' + opacity + ')';

		if(opacity < 100)
		{
			setTimeout(function(){ProductDisplayFade(true, elementID)}, 30);
		}
		else
		{
			var count;

			for(count in productIDs)
			{
				getById(productIDs[count]).style.display = 'none';
			}

			getById(productFaderID).style.visibility = 'hidden';
			ProductDisplayResize(elementID);
		}
	}
	else
	{
		var opacity = Math.max(0, getById(productFaderID).style.opacity * 100 - 25);
		getById(productFaderID).style.opacity = opacity / 100.0;
		getById(productFaderID).style.filter = 'alpha(opacity = ' + opacity + ')';

		if(opacity > 0)
		{
			setTimeout(function(){ProductDisplayFade(false, elementID)}, 30);
		}
		else
		{
			getById(productFaderID).style.visibility = 'hidden';
			locked = false;
		}
	}
}

function ProductDisplayResize(elementID)
{
	var height = parseInt(getById(productContainerID).style.height);
	
	if(isNaN(height))
	{
		height = 0;
	}

	if(height < parseInt(getById(elementID).style.height))
	{
		height = Math.min(height + 60, parseInt(getById(elementID).style.height));
	}
	else
	{
		height = Math.max(height - 60, parseInt(getById(elementID).style.height));
	}

	getById(productContainerID).style.height = height + 'px';

	if(height != parseInt(getById(elementID).style.height))
	{
		setTimeout(function(){ProductDisplayResize(elementID)}, 30);
	}
	else
	{
		getById(productFaderID).style.width = getById(elementID).style.width;
		getById(productFaderID).style.height = getById(elementID).style.height;
		getById(productFaderID).style.visibility = 'visible';
		getById(elementID).style.display = 'block';
		ProductScrollTo(elementID);
	}
}

function ProductScrollTo(elementID)
{
	var scrollTo = Math.min(offsetTop(productScrollToID) - 8, getById('shell').offsetHeight - offsetHeight()) - 1;

	if(scrollTop() < scrollTo)
	{
		window.scrollTo(scrollLeft(), Math.min(scrollTo, scrollTop() + 60));
		setTimeout(function(){ProductScrollTo(elementID)}, 30)
	}
	else
	{
		ProductDisplayFade(false, elementID);
	}
}

function getById(elementID)
{
	return document.getElementById(elementID);
}

function scrollLeft()
{
	if(document.body.parentElement)
	{
        return document.body.parentElement.scrollLeft;
	}
	else
	{
        return window.pageXOffset;
	}
}

function scrollTop()
{
	if(IEVersion() != -1)
	{
        return document.body.parentElement.scrollTop;
	}
	else
	{
        return window.pageYOffset;
	}
}

function offsetHeight()
{
	if(IEVersion() != -1)
	{
        return document.documentElement.clientHeight;
	}
	else
	{
        return window.innerHeight;
	}
}

function offsetTop(elementID)
{
	if(IEVersion() == -1 || IEVersion() >= 8)
	{
		return getById(elementID).offsetTop;
	}
	else
	{
		var element = getById(elementID);
		var offsetTop = 0;

		do
		{
			offsetTop += element.offsetTop;
			element = element.parentElement;
		}
		while(element);

		return offsetTop;
	}
}

