var steam_activepopup_ip;
var steam_activepopup_port;
var steam_activepopup_timer;
var steam_activepopup_retries = -1;

function show_steam_popup(ip, port, obj) {
	var coords = $(obj).offset();
	
	$("#steampopup").css("left", coords.left - 20);
	$("#steampopup").css("top", coords.top + $(obj).height());
	
	if (ip != steam_activepopup_ip || port != steam_activepopup_port) {
		//if (steam_activepopup_retries != -1) $clear(steam_activepopup_timer);
		
		$("#steampopup_icon").attr("src", "/img/steam-32x32.png");
		$("#steampopup_host").html(ip + ":" + port);
		$("#steampopup_name").html("<img src='/img/ajax/01.gif' width='16' height='16'/>");
		$("#steampopup_mapimage").attr("src", "/img/mapthumbs/none.gif");
		
		$("#steampopup_info").css("display", "none");
		$("#steampopup_mapimage").css("display", "none");
		$("#steampopup").css("display", "");
		
		steam_activepopup_ip = ip;
		steam_activepopup_port = port;
		
		steam_retry();
	}
}

function steam_retry() {
	$.get("/ajax/hldsinfo?ip=" + steam_activepopup_ip + "&port=" + steam_activepopup_port, steam_popup_response);
}

function steam_popup_response(response) {
	var a = response.split("\n");
	
	if (a[0] != steam_activepopup_ip + ":" + steam_activepopup_port) return;
	
	if (a[1] == "error") {
		$("#steampopup_icon").attr("src", "/img/steamdead.png");
		if (a[2] == "invalid_host") $("#steampopup_name").html("<span class='error'>Ogiltig host/IP</span>");
		else if (a[2] == "invalid_port") $("#steampopup_name").html("<span class='error'>Ogiltig port</span>");
	}
	else if (a[1] == "ok") {
		$("#steampopup_name").html(a[2]);
		
		$("#steampopup_info_game").html(a[4]);
		$("#steampopup_info_map").html(a[5]);
		$("#steampopup_info_players").html(a[6] + "/" + a[7]);
		
		$("#steampopup_icon").attr("src", a[8]);
		$("#steampopup_mapimage").attr("src", a[9]);
		
		$("#steampopup_info").css("display", "");
		$("#steampopup_mapimage").css("display", "");
	}
	else if (a[1] == "notfound") {
		if (steam_activepopup_retries < 10) {
			steam_activepopup_retries++;
			steam_activepopup_timer = setTimeout("steam_retry();", 25 + steam_activepopup_retries * 50);
		}
		else {
			steam_activepopup_retries = -1;
			$("#steampopup_icon").attr("src", "/img/steamdead.png");
			$("#steampopup_name").html("<span class='error'>Ingen server hittades på " + steam_activepopup_ip + ":" + steam_activepopup_port + "</span>");
		}
	}
}

function hide_steam_popup() {
	//if (steam_activepopup_retries != -1) $clear(steam_activepopup_timer);
	$("#steampopup").css("display", "none");
	steam_activepopup_ip = "";
	steam_activepopup_port = 0;
	steam_activepopup_retries = -1;
}

