// download city to user

var scriptPath = "/scape/teleport/down/dl_manager.php"
var downloadTimerId = 0
// The following 3 variables, download_regionId, download_tileId, and download_turnId,
// are used to hold the values while the Legalese dialog box is displayed.
var download_regionId = -1
var download_tileId = -1
var download_turnId = -1 // the server interprets a turnId of -1 to mean "most recent turn"
var download_cityVersion = ""

// Call this function to cause the Legalese box to appear
function BeginDownloadCity( theRegionId, theTileId, theTurnId, theCityVersion )
{
	if ( !document.Teleporter.IsSimCity4There())
	{
		showErrorMsg( "You must have SimCity4 installed to use this feature." );
		return
	}

	download_regionId = theRegionId
	download_tileId = theTileId
	download_turnId = theTurnId
	download_cityVersion = theCityVersion
	setEltVisibility(getElt("legaleseBox"),"visible");
}

function doDownloadCity( regionID, tileNo, turn, cityVersion )
{
	// If turn is passed in as -1, it tells the server to return the latest turn
	// for a city. If turn is 0 or greater, the server will return that turn if
	// it is found. Always simply pass the turn through to the server; the server
	// will interpret any value passed through.

	// We assume document.Teleporter.Initialize() was called already!
	
	//alert("downloadTimerId: " + downloadTimerId);
	// If downloadTimerId is greater than zero, a download is already occurring.
	// I return true because it is less inaccurate than returning false.
	if ( 0 < downloadTimerId ) { return true }
	
	//alert("Version of city you're downloading: " + cityVersion);
	
	if ( !document.Teleporter.IsSimCity4There())
	{
		showErrorMsg( "You must have SimCity4 installed to use this feature." );
		return false;
	}

	// If city isn't v1.0 (i.e. was saved using expansion packs) and downloader lacks expansion packs, reject download
	if ( cityVersion.substring(0,3) != "1.0" && (! (document.Teleporter.IsRushHourThere() || document.Teleporter.IsDeluxeThere())))
	{
		showErrorMsg( "You must have Rush Hour or SimCity Deluxe installed to download this city." );
		return false;
	}

	var regionsDir = document.Teleporter.GetSimCity4RegionPath();
	if ( !regionsDir || regionsDir.length < 3 )
	{
		showErrorMsg( "Your installation of SimCity4 is incomplete.  Please reinstall SimCity4." );
		return false;
	}

	setEltWidth( getElt("downloadProgress"), 0 );
	setEltVisibility(getElt("downloadBox"),"visible");
	setEltContent(getElt("downloadMsg1"),"Contacting SimCityscape server, please wait...",false);

	// APP - broke this function into two because otherwise the downloadBox doesn't show up
	// immediately... some kind of javascript quirk.  for some reason, calling the rest of the
	// function with timeOut solves the problem (even with 0 as the wait time)
	setTimeout("doDownloadCity2( " + regionID + "," + tileNo + "," + turn + ");", 0);
}

function doDownloadCity2( regionID, tileNo, turn )
{
	var down = document.Teleporter.StartDownloadCity( scriptPath, regionID, tileNo, turn );
	if ( down )
	{
//		alert("setInterval()");
		downloadTimerId = setInterval( "DownloadProgressing()", 500 );
	}
	else
	{
		setEltContent(getElt("downloadMsg1"),"Download failed.",false);
	}
}

function DownloadProgressing()
{
	// alert("DownloadProgressing, downloadTimerId = " + downloadTimerId);
	
	// I put the check below to resolve the "save_as dialog" infinite loop bug...
	// we shouldn't execute downloadProgressing if downloadTimerId is 0, because that means we've 
	// already called clearInterval.  if we enter this loop after that it must be due to some quirk
	// of execution order or timing -- APP 
	if (downloadTimerId == 0) return; 

	var sComplete = document.Teleporter.GetPercentCompleteCity();
	var iComplete = parseInt( sComplete );
	updateDownloadProgress(iComplete);

	if ( iComplete >= 100 )
	{
		//alert("at 100 now!");
		clearInterval( downloadTimerId );
		downloadTimerId = 0;
		updateDownloadProgress(100);
		var outcome = document.Teleporter.GetOutcomeCity();
		if ( outcome != "Successful download." )
		{
			var text = outcome
			setEltContent( getElt("downloadMsg1"), text, false )
			return
		}
		var text = "You have <font size=\"2\" face=\"Arial,Helvetica,sans-serif\" color=\"#3f4967\"><strong>successfully downloaded</strong></font> this city to "
		text += document.Teleporter.GetSimCity4RegionPath()
		text += "/Downloads"
		setEltContent( getElt("downloadMsg2"), text, false )

		text = "After opening SimCity&trade; 4, import the city into a region of your choosing."
		setEltContent( getElt("downloadMsg3"), text, false );
	}
}

function updateDownloadProgress(pct){
	//alert("updateDownloadProgress called, pct = " + pct);
	if ( pct >= 100 ) { pct = 100 }
	setEltWidth( getElt("downloadProgress"), pct*2 );
	setEltContent( getElt("downloadMsg1"), "Download " + pct + "% complete...", false );
}

function CloseDownloadBox()
{
	if ( downloadTimerId != 0 ) { return }
	setEltContent( getElt("downloadMsg1"), "", false );
	setEltContent( getElt("downloadMsg2"), "", false );
	setEltContent( getElt("downloadMsg3"), "", false );
	setEltVisibility(getElt("downloadBox"),"hidden");
}

function Accept()
{
	setEltVisibility(getElt("legaleseBox"),"hidden");
	// APP - broke this function into two because otherwise the legaleseBox doesn't go away
	// immediately... some kind of javascript quirk.  for some reason, calling the rest of the
	// function with timeOut solves the problem (even with 0 as the wait time)
	setTimeout("Accept2();", 0);
}

function Accept2()
{
	if ( download_regionId < 0 || download_tileId < 0 ) { return }
	doDownloadCity( download_regionId, download_tileId , download_turnId, download_cityVersion )
	download_regionId = -1
	download_tileId = -1
	download_turnId = -1
	download_cityVersion = ""
}

function Decline()
{
	setEltVisibility(getElt("legaleseBox"),"hidden");
}

