var divPopup = null;
var divPopupWrapper = null;
var divPopupBackground = null;
var divPopupTitle = null;
var divPopupTabs = null;
var divPopupContent = null;
var m_intPopupOpacity = 0;
var m_intPopupFadeHandle = 0;
var m_intBackgroundOpacity = 0;
var m_intBackgroundFadeHandle = 0;
var m_intMaxWidth = 900;

//initialise the popup structure
function initPopup()
{
	try
	{
		divPopup = document.getElementById("popup");
		divPopupWrapper = document.getElementById("popupWrapper");
		divPopupBackground = document.getElementById("popupBackground");
		divPopupTitle = document.getElementById("title");
		divPopupTabs = document.getElementById("popupTabs");
		divPopupContent = document.getElementById("popupContent");

		if (divPopup == null)
			alert("Popup window cannot be accessed.");

		if (divPopupContent == null)
			alert("Popup content cannot be accessed.");

		if (divPopupWrapper == null)
			alert("Popup wrapper cannot be accessed.");

		if (divPopupBackground == null)
			alert("Popup background cannot be accessed.");

		if (divPopupTitle == null)
			alert("Popup title cannot be accessed.");

		if (divPopupTabs == null)
			alert("Popup tabs cannot be accessed.");
	}
	catch (exc)
	{
		alert(exc.message);
	}
}

//open the popup window
function openPopup(srcElement, strTitle, strTabs, strContent)
{
	try
	{
		initPopup();

		var intTop = eval(srcElement.offsetTop);
		var intHeight = eval(srcElement.offsetHeight) - 6; //6 = (2 * 3px borders, 1 top, 1 bottom)
		var intWidth = eval(srcElement.offsetWidth) - 6; //6 = (2 * 3px borders, 1 left, 1 right)

		divPopup.style.marginTop = intTop + "px";
		divPopup.style.height = intHeight + "px";
		divPopup.style.width = intWidth + "px";

		m_intPopupOpacity = -20;
		m_intBackgroundOpacity = 0;
		
		var aTab = divPopupTabs.firstChild;
		var aClose = null;
		
		while (aTab != null)
		{
			var nextTab = aTab.nextSibling;

			if (aTab.tagName)
			{
				if (aTab.className != "close" && aTab.tagName != undefined && aTab.tagName.toUpperCase() == "A")
					divPopupTabs.removeChild(aTab);

				if (aTab.className == "close" && aTab.tagName != undefined && aTab.tagName.toUpperCase() == "A")
					aClose = aTab;
			}
				
			aTab = nextTab;
		}
		
		if (strTabs != undefined && strTabs != "" && strTabs != null)
		{
			var arrTabs = strTabs.split('|');

			if (arrTabs.length > 0)
			{
				for (var intIndex = 0; intIndex < arrTabs.length; intIndex++)
				{
					var strTab = arrTabs[intIndex];
					var arrTab = strTab.split(':');
					var strText = arrTab[0];
					var strCommand = arrTab[1];

					aTab = document.createElement("A");
					aTab.innerHTML = strText;
					aTab.href = "javascript:" + strCommand;

					divPopupTabs.insertBefore(aTab, aClose);
				}

				divPopupTabs.style.display = "";
			}
			else
				divPopupTabs.style.display = "none";
		}
		else
			divPopupTabs.style.display = "none";
		
		if (m_intPopupFadeHandle != 0)
			window.clearInterval(m_intPopupFadeHandle);
		m_intPopupFadeHandle = window.setInterval("fadeInPopup()", 1);
		
		if (m_intBackgroundFadeHandle != 0)
			window.clearInterval(m_intBackgroundFadeHandle);
		m_intBackgroundFadeHandle = window.setInterval("fadeInBackground()", 1);
		
		fadeInBackground();
		fadeInPopup();
		divPopupWrapper.style.display = "block";
		divPopupBackground.style.display = "block";
		divPopupTitle.innerHTML = strTitle;
		
		showPopupContent(strContent);
	}
	catch (exc)
	{
		alert(exc.message);
	}
}

//perform a single fade-in increment
function fadeInPopup()
{
	m_intPopupOpacity = m_intPopupOpacity + 10;
	var intMarginTop = eval(divPopup.style.marginTop.replace("px", ""));
	var intHeight = eval(divPopup.style.height.replace("px", ""));
	var intWidth = eval(divPopup.style.width.replace("px", ""));
	
	var intMinWidth = intWidth;
	var intChange = Math.round(intMarginTop / 8);
	intHeight += intChange;
	intMarginTop -= intChange;
	intWidth += (m_intPopupOpacity * 2);
	var intContentHeight = intHeight - 67;

	if (divPopupTabs.style.display == "none")
		intContentHeight += 25;
	
	if (m_intPopupOpacity >= 100)
	{
		m_intPopupOpacity = 100;
		intMarginTop = 30;
		intWidth = 700;

		if (m_intPopupFadeHandle != 0)
			window.clearInterval(m_intPopupFadeHandle);
	}
	
	if (intWidth < intMinWidth)
		intWidth = intMinWidth;

	if (intWidth > m_intMaxWidth)
		intWidth = m_intMaxWidth;
	
	if (intMarginTop >= 30)
		divPopup.style.marginTop = intMarginTop + "px";
		
	divPopup.style.height = intHeight + "px";
	divPopup.style.width = intWidth + "px";
	divPopupTitle.style.width = eval(intWidth - 32) + "px";
	//divPopupWrapper.style.filter = 'alpha(opacity=' + m_intPopupOpacity + ')';
	//divPopupWrapper.style.opacity = m_intPopupOpacity / 100;
	divPopupContent.style.height = intContentHeight + "px";

	if (m_intPopupOpacity >= 100)
		divPopupContent.style.overflowY = "scroll";
	else
		divPopupContent.style.overflow = "hidden";
}

//perform a single fade-in increment
function fadeInBackground()
{
	//m_intBackgroundOpacity = m_intBackgroundOpacity + 10;
	
	m_intBackgroundOpacity = m_intBackgroundOpacity + 100;
	
	if (m_intBackgroundOpacity >= 50)
	{
		m_intBackgroundOpacity = 50;

		if (m_intBackgroundFadeHandle != 0)
			window.clearInterval(m_intBackgroundFadeHandle);
	}
	
	divPopupBackground.style.filter = 'alpha(opacity=' + m_intBackgroundOpacity + ')';
	divPopupBackground.style.opacity = m_intBackgroundOpacity / 100;
}

//perform a single fade-out increment
function fadeOutPopup()
{
	m_intBackgroundOpacity = m_intBackgroundOpacity - 10;
	m_intPopupOpacity = m_intPopupOpacity - 10;
	
	if (m_intBackgroundOpacity <= 0)
	{
		if (m_intBackgroundFadeHandle != 0)
			window.clearInterval(m_intBackgroundFadeHandle);

		divPopupWrapper.style.display = "";
		divPopupBackground.style.display = "";

		divPopupBackground.style.filter = "alpha(opacity=" + 50 + ")";
		divPopupBackground.style.opacity = 0.5;
		return;
	}
	
	//divPopupWrapper.style.filter = 'alpha(opacity=' + m_intPopupOpacity + ')';
	//divPopupWrapper.style.opacity = m_intPopupOpacity / 100;
	divPopupBackground.style.filter = "alpha(opacity=" + m_intBackgroundOpacity + ")";
	divPopupBackground.style.opacity = m_intBackgroundOpacity / 100;
	divPopupContent.style.overflow = "hidden";
}

//close the popup window, use fadeout
function closePopup()
{
	try
	{
		initPopup();

		if (m_intBackgroundFadeHandle != 0)
			window.clearInterval(m_intBackgroundFadeHandle);
		m_intBackgroundFadeHandle = window.setInterval("fadeOutPopup()", 5);
		
		fadeOutPopup();
	}
	catch (exc)
	{
		alert(exc.message);
	}
}

//get and show the content for the given key
function showPopupContent(strKey)
{
	var intCMSContentId = Number(strKey);
	
	if (isNaN(intCMSContentId))
		divPopupContent.innerHTML = strKey;
	else
		divPopupContent.innerHTML = getContent(strKey);
}

//get the html content for the given key
function getContent(intCMSContentId)
{
	var strHTML = "";

	var params = new SOAPClientParameters();
	params.add("intCMSContentId", intCMSContentId);
	
	strHTML = SOAPClient.invoke(TruckfileWebservice, "GetContent", params, false, null, null);

	return strHTML;
}
