
$(document).ready( function(){
	
	var html = "", i, elt = $("#zoom")[0];
	for( i = 3; i< 15; i++ )
	{
		if( i == 5 ) html += "<option selected='1'>" + i + "</option>\n";
		else html += "<option>" + i + "</option>\n";
	}
	elt.innerHTML = html;
	
	if( document.getElementById( "state" ))
	{
		html = "<option selected='1' value=''>All States</option>\n";
		var states = Array(
			"AL","AK","AZ","AR","CA","CO","CT","DE","DC","FL","GA","HI","ID","IL","IN","IA","KS","KY","LA","ME","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR","PA","PR","RI","SC","SD","TN","TX","UT","VT","VA","WA","WV","WI","WY"
		);
		elt = $("#state" )[0];
		for( i = 0; i< states.length; i++ )
		{
			html += "<option>" + states[i] + "</option>\n";
		}
		elt.innerHTML = html;
		
		$("#state").change( function(){
			tvr_map.disableAutoUpdates();
			tvr_map.g_limitState = $("#state" )[0].value;
			tvr_map.resetAllData();
			updateEmbedCode();
		});
		
	}
	
	$("#zoom").change( function(){
		tvr_map.updateZoom( $("#zoom")[0].value );
		updateEmbedCode();
	});

	$("#cb_auto").change( function(){
		var f = $("#cb_auto")[0].checked;
		if( f )
		{
			tvr_map.g_autonotice = true;
			tvr_map.restartAuto();
		}
		else
		{
			tvr_map.g_autonotice = false;
			tvr_map.g_auto = false;
		}
		updateEmbedCode();
	});

	$("#width").change( function(){
		var elt = document.getElementById( "#tvr_map" );
		$( "#tvr_map" ).css( "width", $("#width").val() + "px" );
		tvr_map.setEmbeddedSize( "map" ); 
		updateEmbedCode();
	});

	$("#height").change( function(){
		$( "#tvr_map" ).css( "height", $("#height").val() + "px" );
		tvr_map.setEmbeddedSize( "map" ); 
		updateEmbedCode();
	});
	
	updateEmbedCode();
	
	$("#embed").click( function(){
		$("#embed")[0].select();
	});

	$("#embed").keydown( function(){
		return false;
	});
	$("#embed").keyup( function(){
		return false;
	});
	
});

function updateEmbedCode()
{
	var url = document.location.href.replace( /\/[^\/]*$/, "/" ) + "map.html?z=" + $("#zoom")[0].value;
	if( !$("#cb_auto")[0].checked ) url += "&auto=off";
	if( $("#state")[0].value != "" ) url += "&state=" + $("#state")[0].value ;
	
	var s = "<iframe name='tvr_map' id='tvr_map' src='" + url
		+ "' style='border: 1px solid gray;' frameborder='no' width='_W_' height='_H_'></iframe>";
	s = s.replace( /_W_/, $("#width").val());
	s = s.replace( /_H_/, $("#height").val());
	$("#embed")[0].value = s;
}


