var urlFiguras = "index.php?acao=ajaxPegarFigura&id=";
var funcionando = false;
var httpFiguras = getHTTPObject();

function montarTag(id, tipo, nome, legenda) {
	var tag = "";
	
	if (legenda.length)
		tag += "<p class='p_legenda'>" + legenda + "</p>";	
	
	if (tipo == "imagem") {
		if (id == -1) {
			tag += "<p><img width='500' height='400' src='" + nome + "' /></p>";			
		} else {
			tag += "<p><a href='ampliar-imagem' onClick='ampliar(\"index.php?acao=ampliar&fig=" + id + "\");return false;'><img width='500' height='400' src='" + nome + "' /></a></p>";			
		}
		
	} else if (tipo == "director") {
		
		tag += "<p>";
		tag += "<object classid='clsid:166B1BCA-3F9C-11CF-8075-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=8,5,0,0' width='500' height='400'>"
		tag += "<param name='src' value='" + nome + "'>"
		tag += "<embed src='" + nome + "' pluginspage='http://www.macromedia.com/shockwave/download/' width='500' height='400'></embed>"
		tag += "</object>"
		tag += "</p>";	
		
	} else if (tipo == "flash") {
		
		tag += "<p>";
		tag += "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0' width='480' height='360'>";
		tag += "<param name='movie' value='" + nome + "' />";
		tag += "<param name='quality' value='high' />";
		tag += "<param name='base' value='figs/' />";
		tag += "<param name='menu' value='false'>";
		tag += "<embed src='" + nome + "' quality='high' menu='false' base='figs/' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width='480' height='360'></embed>";		
		tag += "</object>";
		tag += "</p>";
		
	} else {
		
		tag += "<p>Desculpe, mas este tipo de figura ainda nao foi cadastrado em nosso sistema</p>";	
	}
	
	return tag;
}


function handleHttpResponseFiguras() {
	if (httpFiguras.readyState == 4) {
		var xmlDocument = httpFiguras.responseXML; 
		var figura = xmlDocument.getElementsByTagName('figura');

		var id = figura[0].getElementsByTagName("id")[0].firstChild.nodeValue;
		var nome = figura[0].getElementsByTagName("nome")[0].firstChild.nodeValue;
		var tipo = figura[0].getElementsByTagName("tipo")[0].firstChild.nodeValue;
		var legenda = figura[0].getElementsByTagName("legenda")[0].firstChild.nodeValue;

		tagFigura = montarTag(id, tipo, nome, legenda);
	
		var holder = document.getElementById("holder_figuras");
		holder.innerHTML = tagFigura;
		funcionando = false;
	}
}

function ajaxCarregarFigura(id) {
	if (funcionando) {
		httpFiguras.abort();
		funcionando = false;
		// alert('abortei');
	}		
	var holder = document.getElementById("holder_figuras");	
	holder.innerHTML = "<p id='carregando'>carregando...</p>";

	httpFiguras.open("GET", urlFiguras + escape(id), true);
	funcionando = true;
	httpFiguras.onreadystatechange = handleHttpResponseFiguras;
	httpFiguras.send(null);
}