﻿function NewPopup(divPopup, divBackground)
{
	if (divPopup == null)
		throw new Error("Parent element for NewPopup must be supplied");

	if (divBackground == null || divBackground + "" == "undefined")
		divBackground = document.getElementById("popupBackground");

	var divParent = divPopup.parentElement ? divPopup.parentElement : divPopup.parentNode;
	var divShadow = null;
	if (divParent.className != "popupShadow")
	{
		divShadow = document.createElement("DIV");
		divShadow.className = "popupShadow";
		divParent.insertBefore(divShadow, divPopup);
		divShadow.appendChild(divPopup);
	}
	else
		divShadow = divParent;

	var THIS = this;
	var intFadeHandle = 0;
	this.FadingIn = null;
	this.Id = NewPopup.NextPopupId();
	NewPopup.Elements[this.Id] = this;
	this.PopupOpacity = 0;
	this.BackgroundOpacity = 0;
	this.OnFadeIn = null;
	this.OnPopupClosed = null;
	this.Shadow = divShadow;

	this.FadeInBackground = NewPopup.FadeInBackground;
	this.FadeInPopup = NewPopup.FadeInPopup;
	this.TargetBackgroundOpacity = NewPopup.BackgroundOpacity;
	this.PopupFadeThreshold = NewPopup.PopupFadeThreshold;
	this.FadeSpeed = NewPopup.FadeSpeed;
	this.ExcludeBackground = false;
	this.IsOpen = false;

	this.Shadow = divShadow;
	this.Popup = divPopup;
	this.Background = divBackground;

	this.Increment = 0;
	var intIncrements = 0;
	var dblBackgroundOpacityMultiplier = 0;

	SetPopupOpacity(this.PopupFadeThreshold);
	//SetBackgroundOpacity(0);

	//====================================================
	// The fade-in has completed, close/stop and running faders
	//====================================================
	this.CompleteFadeIn = function CompleteFadeIn()
	{
		THIS.Popup.style.display = "block";
		THIS.Shadow.style.display = "block";

		THIS.CancelFade();

		SetPopupOpacity(100);
		SetBackgroundOpacity(this.TargetBackgroundOpacity);

		this.IsOpen = true;
	}

	//====================================================
	// Set the color for the workshop loading control based on the color of the background
	//====================================================
	function UpdateWSLoadingColor()
	{
		try
		{
			/*var xamlWorkshopLoading = document.getElementById("xamlWorkshopLoading");
			if (xamlWorkshopLoading != null && xamlWorkshopLoading.content != null && xamlWorkshopLoading.content.LoadingCanvas != null)
			{
				var intOpacity = THIS.BackgroundOpacity / 100;
				intOpacity = intOpacity * 256;
				xamlWorkshopLoading.content.LoadingCanvas.SetColor(intOpacity, 0, 0, 0);
			}*/
		}
		catch (exc) { }
	}

	//====================================================
	// The fade-out has completed, close/stop and running faders
	//====================================================
	this.CompleteFadeOut = function CompleteFadeOut()
	{
		THIS.CancelFade();

		if ((WaitWindow.Visible == true || NewPopup.OpenPopups > 1) && THIS.Background == document.getElementById("popupBackground"))
			THIS.Background.style.display = "block";
		else
			THIS.Background.style.display = "";

		THIS.Popup.style.display = "";
		THIS.Shadow.style.display = "";
		SetPopupOpacity(100);

		if (NewPopup.OpenPopups == 1)
		{
			SetBackgroundOpacity(this.ThisBackgroundOpacity);

			/*try
			{
				var xamlWorkshopLoading = document.getElementById("xamlWorkshopLoading");
				if (xamlWorkshopLoading != null && xamlWorkshopLoading.content != null && xamlWorkshopLoading.content.LoadingCanvas != null)
					xamlWorkshopLoading.content.LoadingCanvas.SetColor(255, 255, 255, 255);
			}
			catch (exc) { }*/
		}

		this.IsOpen = false;

		if (THIS.OnPopupClosed != null)
		{
			try
			{
				THIS.OnPopupClosed(THIS);
			}
			catch (exc) { }
		}

		if (this.Background == document.getElementById("popupBackground"))
			NewPopup.OpenPopups--;
		else
			NewPopup.DynamicPopups--;
	}

	//====================================================
	// Cancel the fade-in/out
	//====================================================
	this.CancelFade = function CancelFade()
	{
		this.Increment = 0;
	
		if (intFadeHandle != 0)
			window.clearInterval(intFadeHandle);
		intFadeHandle = 0;

		this.FadingIn = null;
	}

	//====================================================
	// Invoke the requested method
	//====================================================
	this.Marshal = function Marshal(strMethod)
	{
		eval(strMethod);
	}

	//====================================================
	// Update the size of this popup and its controls
	//====================================================
	this.UpdateSize = function UpdateSize()
	{
		if (divPopup.offsetWidth != 0)
		{
			divShadow.style.width = divPopup.offsetWidth + "px";
			divShadow.style.marginLeft = 0 - divPopup.offsetWidth / 2 + "px";
		}
		
		if (divPopup.offsetHeight != 0)
		{
			divShadow.style.height = divPopup.offsetHeight + "px";
			divShadow.style.marginTop = 0 - divPopup.offsetHeight / 2 + "px";
		}
		
		divPopup.style.marginLeft = "0px";
		divPopup.style.marginTop = "0px";
	}

	//====================================================
	// Open and fade-in the popup
	//====================================================
	this.Open = function Open()
	{
		if (this.IsOpen || this.FadingIn == true)
			return;

		intIncrements = (100 - this.PopupFadeThreshold) / 10;
		dblBackgroundOpacityMultiplier = (this.TargetBackgroundOpacity / intIncrements);

		this.CancelFade();

		this.FadingIn = true;
		if (this.Background == document.getElementById("popupBackground"))
			NewPopup.OpenPopups++;
		else
			NewPopup.DynamicPopups++;

		if (NewPopup.FadeInBackground == true)
			THIS.BackgroundOpacity = 0;
		else
			THIS.BackgroundOpacity = this.TargetBackgroundOpacity;

		if (NewPopup.FadeInPopup == true)
			THIS.PopupOpacity = this.PopupFadeThreshold;
		else
			THIS.PopupOpacity = 100;

		SetPopupOpacity(this.PopupFadeThreshold);
		SetBackgroundOpacity(this.BackgroundOpacity);

		THIS.Popup.style.display = "block";
		THIS.Shadow.style.display = "block";
		if (THIS.ExcludeBackground == false)
			THIS.Background.style.display = "block";

		if (this.FadeInBackground == true || this.FadeInPopup == true)
			intFadeHandle = window.setInterval("NewPopup.Marshal(" + this.Id + ", \"IncrementFadeIn()\")", this.FadeSpeed);
		else
			this.CompleteFadeIn();

		this.UpdateSize();
	}

	//====================================================
	// Close the popup, but dont hide the background
	//====================================================
	this.CloseOnly = function CloseOnly()
	{
		if (this.IsOpen == false || this.FadingIn == false)
			return;

		this.CancelFade();
		this.CompleteFadeOut();
		THIS.Background.style.display = "block";
	}

	//====================================================
	// Dispose this popup
	//====================================================
	this.Dispose = function Dispose()
	{
		if (this.IsOpen == false || this.FadingIn == false)
			return;

		this.CancelFade();
		this.CompleteFadeOut();
	}

	//====================================================
	// Close the popup, but dont hide the background
	//====================================================
	this.OpenOnly = function OpenOnly()
	{
		if (this.IsOpen || this.FadingIn == true)
			return;

		this.CancelFade();
		this.CompleteFadeIn();
		if (this.Background == document.getElementById("popupBackground"))
			NewPopup.OpenPopups++;
		else
			NewPopup.DynamicPopups++;
	}

	//====================================================
	// Close the popup, fading it out and the background
	//====================================================
	this.Close = function Close()
	{
		if (this.IsOpen == false || this.FadingIn == false)
			return;

		intIncrements = (100 - this.PopupFadeThreshold) / 10;
		dblBackgroundOpacityMultiplier = (this.TargetBackgroundOpacity / intIncrements);

		this.CancelFade();

		this.FadingIn = false;

		if (this.FadeInBackground == true || this.FadeInPopup == true)
			intFadeHandle = window.setInterval("NewPopup.Marshal(" + this.Id + ", \"IncrementFadeOut()\")", this.FadeSpeed);
		else
			this.CompleteFadeOut();
	}

	//====================================================
	// Increase the opacity of the popup
	//====================================================
	function IncrementFadeIn()
	{
		THIS.Increment++;
		if (THIS.FadeInPopup == true)
		{
			THIS.PopupOpacity += 10;

			if (THIS.PopupOpacity > 100)
				THIS.PopupOpacity = 100;
			
			SetPopupOpacity(THIS.PopupOpacity);
		}

		if (THIS.FadeInBackground == true)
		{
			THIS.BackgroundOpacity += dblBackgroundOpacityMultiplier;

			if (THIS.BackgroundOpacity > 100)
				THIS.BackgroundOpacity = 100;

			SetBackgroundOpacity(THIS.BackgroundOpacity);
		}

		if (THIS.OnFadeIn != null)
		{
			try
			{
				THIS.OnFadeIn(THIS, THIS.PopupOpacity, (THIS.Increment >= intIncrements));
			}
			catch (exc) { }
		}

		if (THIS.Increment >= intIncrements)
			THIS.CompleteFadeIn();
	}

	//====================================================
	// Decrease the opacity of the popup
	//====================================================
	function IncrementFadeOut()
	{
		THIS.Increment++;
		if (THIS.FadeInPopup == true)
		{
			THIS.PopupOpacity -= 10;

			if (THIS.PopupOpacity < 0)
				THIS.PopupOpacity = 0;
			
			SetPopupOpacity(THIS.PopupOpacity);
		}

		if (THIS.FadeInBackground == true)
		{
			THIS.BackgroundOpacity -= dblBackgroundOpacityMultiplier;

			if (THIS.BackgroundOpacity < 0)
				THIS.BackgroundOpacity = 0;
			
			SetBackgroundOpacity(THIS.BackgroundOpacity);
		}

		if (THIS.Increment >= intIncrements)
			THIS.CompleteFadeOut();
	}

	//====================================================
	// Set the opacity of the popup
	//====================================================
	function SetPopupOpacity(intOpacity)
	{
		if (intOpacity == undefined)
			return;

		THIS.Shadow.style.opacity = (intOpacity / 100);
		THIS.Shadow.style.filter = "alpha(opacity=" + intOpacity + ")";

		if (intOpacity >= 100)
		{
			THIS.Shadow.style.filter = "";
		}
	}

	//====================================================
	// Set the opacity of the background
	//====================================================
	function SetBackgroundOpacity(intOpacity)
	{
		if (intOpacity == undefined)
			return;
	
		THIS.Background.style.opacity = (intOpacity / 100);
		THIS.Background.style.filter = "alpha(opacity=" + intOpacity + ")";

		if (intOpacity >= 100)
			THIS.Background.style.filter = "";

		UpdateWSLoadingColor();
	}
}

NewPopup.CurrentId = 0;
NewPopup.Elements = new Object();

NewPopup.PopupFadeThreshold = 50;
NewPopup.BackgroundOpacity = 50;
NewPopup.FadeSpeed = 25;
NewPopup.FadeInBackground = false;
NewPopup.FadeInPopup = BrowserDetect.browser != "Explorer";
NewPopup.DynamicPopups = 0;
NewPopup.OpenPopups = 0;

var objBrochurePopup = null;

NewPopup.NextPopupId = function NewPopup_NextPopupId()
{
	return ++NewPopup.CurrentId;
}

NewPopup.Marshal = function NewPopup_Marshal(intPopupId, strMethod)
{
	NewPopup.Elements[intPopupId].Marshal(strMethod);
}

NewPopup.OpenPopup = function NewPopup_OpenPopup(divPanel, strTitle, strTabs, intContentId)
{
	if (objBrochurePopup == null)
	{
		objBrochurePopup = new NewPopup(document.getElementById("popup"));
		objBrochurePopup.OnFadeIn = NewPopup.RepositionBrochurePopup;
		objBrochurePopup.OnPopupClosed = NewPopup.BrochurePopupClosed;
		objBrochurePopup.FadeInBackground = true;
		objBrochurePopup.FadeInPopup = true;
		objBrochurePopup.ExcludeBackground = true;
		//objBrochurePopup.FadeSpeed = 250;

		document.getElementById("btnClosePopup").onclick = objBrochurePopup_Close;
	}

	h2PopupTitle = document.getElementById("title");
	h2PopupTitle.innerHTML = strTitle;

	//remove any existing tabs
	var divPopupTabs = document.getElementById("popupTabs");
	var divClearBothTabs = divPopupTabs.getElementsByTagName("DIV")[0];

	var aTab = divPopupTabs.firstChild;
	var aClose = null;

	var popupContent = document.getElementById("popupContent");
	popupContent.innerHTML = "";

	while (aTab != null)
	{
		var nextTab = aTab.nextSibling;

		if (aTab.tagName)
		{
			if (aTab.className != "close" && aTab.tagName != undefined && aTab.tagName.toUpperCase() == "A")
				divPopupTabs.removeChild(aTab);
		}

		aTab = nextTab;
	}

	//add any new tabs

	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, divClearBothTabs);
			}

			divPopupTabs.style.display = "";
			h2PopupTitle.className = "tabsPresent";
			var intContentHeightOffset = divPopupTabs.offsetHeight == 0 ? 24 : divPopupTabs.offsetHeight;
			objBrochurePopup["ContentHeightOffset"] = intContentHeightOffset;
		}
		else
		{
			divPopupTabs.style.display = "none";
			h2PopupTitle.className = "";
			objBrochurePopup["ContentHeightOffset"] = 0;
		}
	}
	else
	{
		divPopupTabs.style.display = "none";
		h2PopupTitle.className = "";
		objBrochurePopup["ContentHeightOffset"] = 0;
	}

	var intLeft = divPanel.offsetLeft;
	var intTop = divPanel.offsetTop;
	var intWidth = divPanel.offsetWidth - 3;
	var intHeight = divPanel.offsetHeight;

	var intTitleHeight = h2PopupTitle.offsetHeight == 0 ? 22 : h2PopupTitle.offsetHeight;
	var intButtonHeight = 51;

	intHeight -= intTitleHeight + objBrochurePopup["ContentHeightOffset"] + intButtonHeight + 3;

	objBrochurePopup["OriginalHeight"] = intHeight;
	var divContentHeading = document.getElementById("contentHeading");

	if (BrowserDetect.browser == "Explorer" && BrowserDetect.version != "8")
	{
		//calculate based on its parents;

		var divParent = divContentHeading;
		var intTop = 0;

		if (divPanel != null)
		{
			intTop = divParent.offsetTop;

			while (divParent != null)
			{
				intTop += divParent.offsetTop;

				divParent = divParent.parentElement;
			}
		}

		objBrochurePopup["TargetTop"] = intTop;
	}
	else
	{
		if (divContentHeading != null)
			objBrochurePopup["TargetTop"] = divContentHeading.offsetTop;
	}

	objBrochurePopup["TargetLeft"] = document.getElementById("wrapper").offsetLeft;
	objBrochurePopup["FadeTopMovement"] = null;
	objBrochurePopup["FadeLeftMovement"] = null;
	objBrochurePopup["FadeWidthMovement"] = null;
	objBrochurePopup["FadeHeightMovement"] = null;

	objBrochurePopup["TargetWidth"] = document.getElementById("wrapper").offsetWidth - 4;

	var blnGetHeightFromBody = BrowserDetect.browser == "Explorer" || BrowserDetect.browser == "Chrome" || BrowserDetect.browser == "Safari";

	var intWindowHeight = blnGetHeightFromBody ? document.documentElement.clientHeight : window.innerHeight;

	objBrochurePopup["TargetHeight"] = intWindowHeight - (objBrochurePopup["TargetTop"] + 112 + Number(objBrochurePopup["ContentHeightOffset"]));

	if (BrowserDetect.browser == "Explorer")
		objBrochurePopup["TargetHeight"] += 5;

	if (BrowserDetect.browser == "Explorer" && BrowserDetect.version == 6)
		objBrochurePopup["TargetHeight"] += 30;

	if (BrowserDetect.browser == "Explorer" && BrowserDetect.version != "8")
	{
		//calculate based on its parents;

		var divParent = divPanel.parentElement;

		while (divParent != null)
		{
			intLeft += divParent.offsetLeft;
			intTop += divParent.offsetTop;

			divParent = divParent.parentElement;
		}

		intLeft -= 2;

		objBrochurePopup["TargetLeft"] -= 1;
	}

	if (intHeight < 0)
		intHeight = 0;

	objBrochurePopup.Shadow.style.left = intLeft + "px";
	objBrochurePopup.Shadow.style.top = intTop + "px";
	objBrochurePopup.Shadow.style.width = intWidth + "px";
	popupContent.style.height = intHeight + "px";

	var intCMSContentId = Number(intContentId);

	if (isNaN(intCMSContentId))
	{
		intContentId = intContentId.replace(/&gt;/gi, '>');
		intContentId = intContentId.replace(/&lt;/gi, '<');
		popupContent.innerHTML = intContentId;
	}
	else
	{
		NewPopup.RequestContent(intCMSContentId);
	}

	popupContent.style.overflow = "hidden";

	objBrochurePopup.Open();

	objBrochurePopup.Shadow.style.marginLeft = "0px";
	objBrochurePopup.Shadow.style.marginTop = "0px";

	if (objBrochurePopup.FadeInPopup == false)
	{
		objBrochurePopup.Shadow.style.width = objBrochurePopup["TargetWidth"] + "px";
		popupContent.style.height = objBrochurePopup["TargetHeight"] + "px";
		objBrochurePopup.Shadow.style.left = objBrochurePopup["TargetLeft"] + "px";
		objBrochurePopup.Shadow.style.top = objBrochurePopup["TargetTop"] + "px";
	}
}

NewPopup.RequestContent = function NewPopup_RequestContent(intCMSContentId)
{
	var popupContent = document.getElementById("popupContent");
	popupContent.innerHTML = "";
	popupContent.className = "loading";

	var params = new SOAPClientParameters();
	params.add("intCMSContentId", intCMSContentId);

	strHTML = SOAPClient.invoke("/webservices/newDesign.asmx", "GetContent", params, true, NewPopup.DisplayBrochureContent, popupContent);
}

NewPopup.DisplayBrochureContent = function NewPopup_DisplayBrochureContent(strXML, docXML, popupContent)
{
	popupContent.innerHTML = strXML;
	popupContent.className = "";
}

function objBrochurePopup_Close()
{
	objBrochurePopup.Close();
}

NewPopup.BrochurePopupClosed = function NewPopup_BrochurePopupClosed(objPopup)
{
	var popupContent = document.getElementById("popupContent");
	popupContent.style.height = "";
	objBrochurePopup.Popup.style.width = "";
	objBrochurePopup.Shadow.style.width = "";
}

NewPopup.RepositionBrochurePopup = function NewPopup_RepositionBrochurePopup(objPopup, intOpacity, blnFinished)
{
	try
	{
		if (objPopup.FadeInPopup == false)
			return;

		var intTargetLeft = objBrochurePopup["TargetLeft"];
		var intTargetTop = objBrochurePopup["TargetTop"];
		var intTargetWidth = objBrochurePopup["TargetWidth"];
		var intTargetHeight = objBrochurePopup["TargetHeight"];
		var intOriginalHeight = objBrochurePopup["OriginalHeight"];
		var intContentHeightOffset = objBrochurePopup["ContentHeightOffset"];

		var intIncrements = (100 - NewPopup.PopupFadeThreshold) / 10;
		var intCurrentLeft = Number(objBrochurePopup.Shadow.style.left.replace("px", ""));
		var intCurrentTop = Number(objBrochurePopup.Shadow.style.top.replace("px", ""));
		var intCurrentWidth = Number(objBrochurePopup.Shadow.style.width.replace("px", ""));
		var intCurrentHeight = Number(document.getElementById("popupContent").style.height.replace("px", ""));

		var intFadeLeftMovement = objBrochurePopup["FadeLeftMovement"];
		var intFadeTopMovement = objBrochurePopup["FadeTopMovement"];
		var intFadeWidthMovement = objBrochurePopup["FadeWidthMovement"];
		var intFadeHeightMovement = objBrochurePopup["FadeHeightMovement"];

		if (objBrochurePopup["FadeTopMovement"] == null)
		{
			var intDistance = intTargetTop - intCurrentTop;
			intFadeTopMovement = intDistance / intIncrements;

			objBrochurePopup["FadeTopMovement"] = intFadeTopMovement;
		}

		if (objBrochurePopup["FadeLeftMovement"] == null)
		{
			var intDistance = intTargetLeft - intCurrentLeft;
			intFadeLeftMovement = intDistance / intIncrements;

			objBrochurePopup["FadeLeftMovement"] = intFadeLeftMovement;
		}

		if (objBrochurePopup["FadeWidthMovement"] == null)
		{
			var intDistance = intTargetWidth - intCurrentWidth;
			intFadeWidthMovement = intDistance / intIncrements;

			objBrochurePopup["FadeWidthMovement"] = intFadeWidthMovement;
		}

		if (objBrochurePopup["FadeHeightMovement"] == null)
		{
			var intDistance = intTargetHeight - intOriginalHeight - intContentHeightOffset;
			intFadeHeightMovement = intDistance / intIncrements;

			if (intContentHeightOffset != 0)
				intFadeHeightMovement += 5;

			objBrochurePopup["FadeHeightMovement"] = intFadeHeightMovement;
		}

		var popupContent = document.getElementById("popupContent");

		objBrochurePopup.Shadow.style.left = intCurrentLeft + intFadeLeftMovement + "px";
		objBrochurePopup.Shadow.style.top = intCurrentTop + intFadeTopMovement + "px";
		objBrochurePopup.Popup.style.width = intCurrentWidth + intFadeWidthMovement + "px";
		popupContent.style.height = intCurrentHeight + intFadeHeightMovement + "px";

		objBrochurePopup.Shadow.style.height = objBrochurePopup.Popup.offsetHeight + "px";
		objBrochurePopup.Shadow.style.width = objBrochurePopup.Popup.offsetWidth + "px";

		if (blnFinished)
		{
			popupContent.style.overflow = "";

			objBrochurePopup.Shadow.style.left = intTargetLeft + "px";
			objBrochurePopup.Shadow.style.top = intTargetTop + "px";

			objBrochurePopup.Popup.style.width = intTargetWidth + "px";

			popupContent.style.height = intTargetHeight + "px";

			objBrochurePopup.Shadow.style.height = objBrochurePopup.Popup.offsetHeight + "px";
			objBrochurePopup.Shadow.style.width = objBrochurePopup.Popup.offsetWidth + "px";
		}
	}
	catch (exc)
	{
		alert("Error repositioning popup;\n" + exc.message);
	}
}

//====================================================
// Contact a new NewPopup object in the context of the current window
//====================================================
NewPopup.Construct = function NewPopup_Construct(divContainer)
{
	return new NewPopup(divContainer);
}
