// for normal tunnel creation
document.domain="tunnelbroker.net";
var delay_count = 0;

function display_closest_tunnelbroker() {
	var found = false;
	// This code is "special" in that it allows the code to be called twice
	// and yet only execute the second time around
	//var content = window.frames('best_location').document.body.innerHTML;
	if (delay_count++ < 2) {
	// delay 2 seconds
		display_delay_id = setTimeout("display_closest_tunnelbroker();", 1 * 1000);

		if (delay_count == 1)
			$('#best').html('<img style="vertical-align: middle" src="./images/loader.gif" />');
		return;
	}
	var content = window.frames['best_location'].document.body.innerHTML;
	var ipv4 = content.match(/\b(?:\d{1,3}\.){3}\d{1,3}\b/);
	
	$('#select_best input').each(function() {
		if($(this).attr('checked') == 'checked') {
			$(this).removeAttr('checked');
		}
		if($(this).val() == ipv4) {
			$(this).attr('checked', 'checked');
			found = true;
		}
	});

	if(content == null)
		$('#best').html('unknown - pick a nearest location');
	else
		$('#best').html(content);
}
	
$(document).ready(function() {
	if($('#best')) {
		display_closest_tunnelbroker();
	}
});

