// JavaScript Document
function resizeimagen(){
	var debug=false;
	if (debug && (window.console && window.console.firebug)) console.debug('resizeimagen: '+this);
	var obj=this;

	var anchopadre = getStyle(obj.parentNode,'width');
	anchopadre=anchopadre.replace('px', '');
	var altopadre = getStyle(obj.parentNode,'height');
	altopadre=altopadre.replace('px', '');

	//if (obj.width > anchopadre) obj.width = anchopadre;
	if (obj.height > altopadre) obj.height = altopadre;
	obj.style.visibility='visible';
}
function getStyle(el,styleProp)
{
	var x = el;
	if (x.currentStyle)
		var y = x.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
	return y;
}

function refresh_plantilla(){
	setInterval('buscar_plantilla()',2000);
}	
function rotativofoto(id,foto){
	var debug=false;
	if (debug && (window.console && window.console.firebug)) console.debug('Rotativofoto para: '+id+' para poner foto '+foto);//console.debug('Elementodiv: '+elementodiv);
	
	var i = document.createElement("IMG");
	i.setAttribute("alt",'Foto');
	i.setAttribute("title",'Foto');
	i.setAttribute("id","img_pl"+id);
	i.setAttribute("idName","img_pl"+id);
	i.style.visibility="hidden";
	document.getElementById(id).innerHTML='';
	document.getElementById(id).appendChild(i);
	var imagen = document.getElementById('img_pl'+id);
	if (debug && (window.console && window.console.firebug)) console.debug(imagen);
	imagen.onload = resizeimagen;
	imagen.setAttribute("src",foto);
	if (debug && (window.console && window.console.firebug)) console.debug(imagen);
}
function buscar_plantilla(){
	var codatr = VAR1;
	var cadena = VAR2;
	var cantidad = VAR3;
	var divDestino = document.getElementById('cuadroplantilla');

	if (codatr!="")	{
		var ajax=nuevoAjax();

		var par = "fold="+_CARPETAUSUARIO+"&codatr="+codatr+"&cadena="+cadena+"&cantidad="+cantidad;
		var url = _BASEFILES+"modules/plantilla/ajax/plantillarndsearch.php";
		
		ajax.open("POST",url, true);
		ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=iso-8859-1");
	
		ajax.onreadystatechange=function() { 
			if (ajax.readyState==1)	{
				// Mientras carga
				//mostrar_ico(0,divDestino);
			}
			if (ajax.readyState==4)
			{
				var result = eval('(' + ajax.responseText + ')');
				if(result.err=='0'){
					var r = result.campos[0];
					document.getElementById('nombre').innerHTML=r.titulo;
					//document.getElementById('foto').innerHTML='<img style="visibility:hidden;" src="'+_BASE+_CARPETAUSUARIO+"img/"+r.nomfoto+'" border="0" alt="" onload="resizeimagen(this)" />';
					var i = document.createElement("IMG");
					//i.setAttribute("id","imagen_"+divDestino.id+'_'+numeroidx);
					i.setAttribute("alt",'Foto');
					i.setAttribute("title",'Foto');
					i.setAttribute("id","img_pl");
					i.setAttribute("idName","img_pl");
					i.style.visibility="hidden";
					document.getElementById('foto').appendChild(i);
					var imagen = document.getElementById('img_pl');
					imagen.onload = resizeimagen;
					imagen.setAttribute("src",_BASE+(_ROOT_MODE_MENU?"":_CARPETAUSUARIO)+"img/"+r.nomfoto);

					document.getElementById('texto').innerHTML=r.valor.substr(0,r.valor.indexOf(".")+1);
					document.getElementById('link').href="main.php?sec=planlist&c="+r.codatr+"&v="+r.codatrval;
				} else {
					mostrar_ico(10,divDestino);
					divDestino.innerHTML=result.err;
				}
				//setInterval('buscar_plantilla()',4000);
			} 
		}
		ajax.send(par);
	}
}