var lastLightboxId = null;
var joinFrame = null;
var lastJoinUrl = null;
var isSteamOverlay = false;
var isAvailable = true;
var lightboxHideRef = null;

function ShowLightbox(id)
{
	if (lastLightboxId == id)
		return;
		
	lastLightboxId = id;

	document.getElementById(id).style.display = 'block';
	document.getElementById('LightboxOverlay').style.display = 'block';
}

function HideLightbox()
{
	if (!lastLightboxId)
		return;
		
	$(lastLightboxId).hide();
	$('LightboxOverlay').hide();

	lastLightboxId = null;

	if (lightboxHideRef) {
		window.clearTimeout(lightboxHideRef);
		lightboxHideRef = null;
	}
}

function CreateAjaxRequest()
{
	var req = null;

	if (window.XMLHttpRequest)
	    req = new XMLHttpRequest();
	else if (window.ActiveXObject)
	    req = new ActiveXObject("Microsoft.XMLHTTP");

	return req;
}

function ViewServers()
{
	$('ViewServersLink').hide();
	
	var req = CreateAjaxRequest();
	req.onreadystatechange = function() {
		if (req.readyState != 4)
			return;
		
		$('ServerTable').innerHTML = req.responseText;
	};
	
	req.open("POST", "?serverdata=1", true);
	req.send(null);
}

function GetRandomNumber()
{
	return parseInt(Math.random()*99999999);	
}

function ShowJoinRetryInfo()
{
	return true;
}

function JoinServerAndUpdateUI(url)
{
	PlaySound('connect');
	
	if (GetQueryStringVar("nojoin") != 1)
		JoinServer(url);
		
	HideLightboxWithDelay(ShowJoinRetryInfo() ? 30000 : 3000);
}

function HideLightboxWithDelay(time)
{
	if (lightboxHideRef)
		window.clearTimeout(lightboxHideRef);
		
	lightboxHideRef = window.setTimeout('HideLightbox()', time);
}	

function RetryServerJoin()
{
	if (lastJoinUrl)
		JoinServer(lastJoinUrl);
}

function GetQueryStringVar(key)
{
	var query = window.location.search.substring(1);
	
	if (query == '')
		return null;

  	var vars = query.split("&");

  	for (var i = 0; i <vars.length; i++) {
		var pair = vars[i].split("=");
	
	    if (pair[0] == key)
			return pair[1];
	}
	
	return null;
}


function PlaySound(name) {
	// Sounds cannot be played within the Steam overlay
	if (IsSteamOverlay())
		return;
	
	soundManager.createSound(name, '/sounds/' + name + '.mp3');
	soundManager.play(name);
}

function IsSteamOverlay()
{
	return isSteamOverlay;	
}

function IsAvailable()
{
	return isAvailable;	
}

function JoinServer(url)
{
	lastJoinUrl = url;
	
	if (joinFrame == null) {
        joinFrame = document.createElement('div');
     	joinFrame.style.display = 'none';

        document.body.appendChild(joinFrame);
	}
	
    joinFrame.innerHTML = "<iframe src='" + url + "' width='10' height='10' scrolling='no' frameborder='0' border='0'></iframe>";
}

function CopyInviteUrl()
{
	if (!clipboardData)
		return;

	// Copy Url to Clipboard
	var inviteUrl = $('PartyInviteUrl').value;
	clipboardData.setData("Text", inviteUrl);	
	
	// Pulsate Copy link to confirm action
	Effect.Pulsate('PartyInviteUrlCopyLink', {
		duration: 0.25,
		pulses: 1
	});
}

function GetCompleteSearchCommand(command, gameVersion)
{
	return command + GetAdditionalSearchArgs(gameVersion);
}

function GetAdditionalSearchArgs(gameVersion)
{
	var args = '';

	// Map Filter
	var campaign = $('Campaign');
	var chosenCampaign = campaign.options[campaign.selectedIndex];
	var bits = chosenCampaign.value.split('--');
	
	// Only use a map filter if one has been chosen for the current game version
	if (bits.length == 2) {
		var game = parseInt(bits[0]);
		var mapSearch = bits[1];
		
		if (game == gameVersion)
			args += ' any ' + mapSearch;
	}
	
	// Server Filter
	var serverFilter = $('ServerProvider');
	if (serverFilter.value != '')
		args += ' -server ' + serverFilter.value;

	return args;
}

function ShowMapMatrix(setVisible, gameVersion)
{
	var allVersions = [1, 2, 3];
	
	for(var i = allVersions[0]; i <= allVersions.length; i++) {
		if (setVisible && i == gameVersion) {
			$('MatrixTable' + i).show();
			$('ShowMapMatrix' + i).addClassName("Selected");
		} else {
			$('MatrixTable' + i).hide();
			$('ShowMapMatrix' + i).removeClassName("Selected");
		}
	}

	Cookie.set('showtable', gameVersion, 86400 * 365);
}

function ShowHelpText(id)
{
	$(id).show();
}

function HideHelpText(id)
{
	$(id).hide();
}

function ShowUnavailableLightbox()
{
	ShowLightbox('RegionUnavailableLightbox');
}
