var timerDownloadrunning = false;

function myOnError(msg, url, lno)
{
	ZI_ErrorInstall();
	return true;
}

// This function is triggered by ZIntro, it creates a progressive progress bar.
function ZI_DownloadProgress(progress, totalBytes, isFake)
{
	if(typeof(isFake) == "undefined")
	{
		refToDownloadNotWorking.style.display = "none";
	}
	var highPoint = 0;
	ZI_CalcDownloadTime(progress, totalBytes, downloadSpeed);
	var segmentTotal = (totalBytes/10);
	for(i = 0; progress > (segmentTotal * i); i++)
	{
		if(progress > 0)
		{
		    document.getElementById("LoadBlock" + i).className = "LoadBlockHot";
			highPoint = i;
		}
	}
	
	document.getElementById("ProgressPct").innerText = (i*10) + "%";
}

//This event is sent by ZIntro
function ZI_DownloadComplete(Success, fakeSuccess)
{
	if(Success)
	{
		// Fill the progress bar, just for forms sake.
		ZI_DownloadProgress(10, 10);
		// Write a session cookie saying that the download is done, don't do it again.
		var currentCookieValue = GetCookie("ZInstall");
		if (currentCookieValue == null)
			SetNoEscapeCookie("ZInstall", gameCookie + "=1", null, "/", cookieDomain, false);
		else if(currentCookieValue.indexOf(gameCookie) < 0)
				SetNoEscapeCookie("ZInstall", currentCookieValue + "&" + gameCookie + "=1", null, "/", cookieDomain, false);
		DeleteSubkey("ZMisc", "InstallTry", null, null, cookieDomain);
		
		if(!fakeSuccess)
			LogCTag("InstallEndSuccess");
		else
			LogCTag("InstallEndFailContinued");
		window.location = returnUrl;
	}
	else
	{
		ZI_ErrorInstall();
	}
}
function ZI_ErrorInstall()
{

		//For now any errors we just forward them on to the game.
		if(!g_InstallRequired)
		{
			ZI_DownloadComplete(true, true);
			return;
		}

		//Whenever there is a whoops error track it to the omniture.
	    var s=s_gi(s_account);
        s.linkTrackVars="events";
        s.linkTrackEvents="event6";
        s.events="event6";
        s.tl(this,"o","Whoops");
        s.linkTrackVars="None";
        s.linkTrackEvents="None";
        
		var count = GetSubkey("ZMisc", "InstallTry");
		if(count == null || count == "")
			count = 0;
		count++;
			SetNoEscapeSubkey("ZMisc","InstallTry",count,null,null,cookieDomain, false)

		if(count < 3)
		{
			LogCTag("InstallErrorRetry");
			ZI_ShowDownloadFailed();
		}
		else
		{
			LogCTag("InstallEndError");
			document.getElementById("downloadFailed").style.display = "none";
			document.getElementById("DownloadProgress").style.display = "none";
			document.getElementById("downloadFailedOver").style.display = "inline";
			DeleteSubkey("ZMisc", "InstallTry", null, null, cookieDomain);
		}
}

function ZI_DownloadHelp()
{
	LogCTag("TroubleHelp");
	window.open(helpLink);
}


function ZI_PageOnLoad()
{
	gPageLoaded = true;
	try
	{
		refToDownloadNotWorking = document.getElementById("DownloadNotWorking");
	
		var count = GetSubkey("ZMisc", "InstallTry");
		if(count == null || count == "")
			LogCTag("InstallStart");
		//When the page is loaded we fire ZIntro off to go download everything.
		ZI_DownloadProgress(0, 100, true);
		// If the broadband settings of the ZMiscPerm cookie are not present, do bandwidth testing
		var bwCookieValue = GetSubkey("ZMiscPerm","MSNGamesBroadband");
		
	}		
	catch(e)
	{

		if (servicePack == "true")
		{
			refToDownloadNotWorking.style.display = "inline";
		}
		myOnError();
	}
		
	
	if (bwCookieValue == null || bwCookieValue == "")
	{
		try
		{
			// Do testing
			ZIntro.TimeDownload("TimedDownloadTest", domainRoot + "/bingame/speedtest.dll", 5000);
			timerDownloadrunning = true;
		}
		catch(e0)
		{

			try
			{
				ZI_StartZIntroDownload();
			}
			catch(e1)
			{
				if (servicePack == "true")
				{
					refToDownloadNotWorking.style.display = "inline";
				}
				myOnError();
			}
		}
	}
	else
	{
		try
		{
			downloadSpeed = GetSubkey("ZMisc","speed");
			ZI_StartZIntroDownload();
		}
		catch(e)
		{
		
			if (servicePack == "true")
			{
				refToDownloadNotWorking.style.display = "inline";
			}
			myOnError();
		}
	
	}

}

function ZI_CalcDownloadTime(progress, totalBytes, downloadSpeed)
{
	var output;
	
	try
	{
		if(downloadSpeed == null)
		{
			output = "Your download should only take a few minutes.";
		}
		else if(progress == null || totalBytes == null || totalBytes == 0)
		{
			output = "Registering components.";
		}
		else
		{
			var bytes2Download = totalBytes - progress;
			var totalSeconds2Download = 0;
			var min2Download = 0;
			if(bytes2Download == 0)
			{
				output = "Registering components.";
			}
			else if(progress == totalBytes)
			{
				output = "Registering components."
			}
			else
			{
				output = "Your estimated download time is "
			
				totalSeconds2Download = bytes2Download / downloadSpeed;
				min2Download = Math.ceil(totalSeconds2Download / 60);
				if(min2Download <= 1)
					output += "less than a minute.";
				else
					output += min2Download + " minutes.";
			}
		}
	}
	catch(e)
	{
		output = "Registering components.";
	}
	
	
	document.getElementById("EstDownloadTime").innerText = output;
}

// Things have gone horribly wrong, go to the game page.
function ZI_ShowDownloadFailed(){
	var currentCookieValue = GetCookie("ZInstall");
	
	document.getElementById("downloadFailed").style.display = "inline";
	document.getElementById("DownloadProgress").style.display = "none";
	
}

function ZI_RestartDownload()
{
	window.location.reload(true);
}
function ZI_ADownloadComplete(Name, Result, Succeeded, Total)
{
	if(Succeeded>=0)
		LogCTag(Name + "_ADownload_Fail");
}

function RedirZPAOpener()
{
	if(typeof(window.opener)=="object" && (openerRedirUrl != "" && openerRedirUrl!= null))
	{
		window.opener.location = openerRedirUrl;
	}
}

function LogCTag(tagName)
{
	if(servicePack == 'true')
	{
		tagName += "_SP2";
	}
	var fullPageUrl = ctagTrackPath + gameCookie + "/" + tagName + ".htm";
	var referPageUrl = document.URL;
	if (returnUrl != null)
	    referPageUrl = returnUrl;	
	try
	{
		fullPageUrl = encodeURIComponent(fullPageUrl);
		referPageUrl = encodeURIComponent(referPageUrl);
	}
	catch(e)
	{
	}
	fullPageUrl = fullPageUrl.toLowerCase();
	referPageUrl = referPageUrl.toLowerCase();
	if(typeof(WriteCTag) == "function")
	{
	    WriteCTag(referPageUrl,null,null,null,null,null,fullPageUrl);
	}
}

