// JavaScript Document
// Víctor J. Chamorro	-	Ipdea.com	-	2008

window.document.write('<script type="text/javascript" src="../js/XHConn.js"></script>');
var str_bussy = "<div style='text-align:center; background:#666666;padding:10px;'><img src='../img/loading.gif' border='0' style='border:0px;' /></div>";
var g_texto = "";
var g_categoria = "";
var g_tipo = "";

//_____________________________________________________________________________________

function ListaGaleria(){
	//g_tipo = "imagenes";
	
	g_tipo = document.frmbusquedagaleria.tipo.value;
	g_categoria = document.frmbusquedagaleria.categoria.value;
	if (g_categoria == '-1') g_categoria = '';
	
	
	document.getElementById('resultado').innerHTML = str_bussy;
	
	data('galeria.cod.php', 'accion=listar&pagina=1&busqueda=&categoria='+g_categoria+'&tipo='+g_tipo, ProcesaListaGaleria);
}
//_____________________________________________________________________________________

function Buscar(pag){
	
	document.getElementById('resultado').innerHTML = str_bussy;
	
	if (g_categoria == "-1") g_categoria = "";
	data('galeria.cod.php', 'accion=listar&pagina='+pag+'&busqueda='+g_texto+'&categoria='+g_categoria+'&tipo='+g_tipo, ProcesaListaGaleria);
}
//_____________________________________________________________________________________

function NuevaBusqueda(){
	g_texto = document.frmbusquedagaleria.texto.value;
	g_categoria = document.frmbusquedagaleria.categoria.value;
	g_tipo = document.frmbusquedagaleria.tipo.value;
	Buscar(1);
}
//_____________________________________________________________________________________


function ProcesaListaGaleria(xml){
	
	var resultado;
	var paginado;
	
	try{
		resultado = xml.getElementsByTagName('resultado');
		paginado = xml.getElementsByTagName('paginado')[0];
	}catch(ex){
		alert('err');
		resultado = new Array();
		paginado = "";
	}
	//Pintamos el Resultado 
	
	var text_resultado = "";
	
	for(var i=0;i<resultado.length;i++){
		var res = resultado[i];
		
		var id = res.getElementsByTagName('id')[0].firstChild.data;
		var id_porfolio = res.getElementsByTagName('idporfolio')[0].firstChild.data;
		var img = res.getElementsByTagName('imagen')[0].firstChild.data;
		var titulo = res.getElementsByTagName('titulo')[0].firstChild.data;
		var porfolio = res.getElementsByTagName('porfolio')[0].firstChild.data;
		var valoracion = res.getElementsByTagName('valoracion')[0].firstChild.data;
		var puesto = res.getElementsByTagName('puesto')[0].firstChild.data;
		var archivo = res.getElementsByTagName('archivo')[0].firstChild.data;
		
		text_resultado+='<div class="gal2">\n<div class="mini">\n';
		
		if (g_tipo != 'animaciones'){
			text_resultado+='<a href="porfolio.php?pid='+id_porfolio+'&iid='+id+'">\n';
		}else{
			text_resultado+='<a href="porfolio.php?pid='+id_porfolio+'&aid='+id+'">\n';
		}
		var url = '';
		var i_width = '';
		var i_height = '';

		if (archivo == "none" && g_tipo == 'animaciones'){
			url = 'http://i.ytimg.com/vi/'+img+'/default.jpg';
			i_width = "94";
			i_height = "";
		}else{		
			url = '../uploads/porfolios/'+g_tipo+'/'+id+'/'+img;

			var image = new Image();
			image.src = url;
			image.id = "obj_img_"+id;
			/*
			image.onload = function(){
			
				i_width = this.width;
				if (i_width > 94){	i_width = 94;	}
				i_height = ((this.height * i_width) / this.width);
				if (i_height > 75){
					i_height = 75;
					i_width = ((this.width * i_height) / this.height);
				}
				document.getElementById('img_small_'+this.id.substr(8)).width = Math.round(i_width);
				document.getElementById('img_small_'+this.id.substr(8)).height = Math.round(i_height);
			}
			*/
		}

		
		
		text_resultado+= '<img src="'+url+'" width="94" height="75" title="'+titulo+'" id="img_small_'+id+'" />';

		text_resultado+='</a>\n</div>\n<div class="mini2">';
		//text_resultado+='<a href="porfolio.php?pid='+id_porfolio+'&aid='+id+'">\n';
		
		if (archivo == "none"){
			text_resultado+='<a href="porfolio.php?pid='+id_porfolio+'&iid='+id+'">\n';
		}else{
			text_resultado+='<a href="porfolio.php?pid='+id_porfolio+'&aid='+id+'">\n';
		}
		
		text_resultado+='<strong> '+porfolio+' </strong>';
		text_resultado+='<br /><strong style="color:#000;">'+puesto+'</strong><br />';
		
		for (ii=0;ii<5;ii++) {
			if (valoracion > ii){
				text_resultado+='<img style="border:0px;" src="../img/star2.gif" /> ';
			}else{
				text_resultado+='<img style="border:0px;" src="../img/star.gif" /> ';
			}
		}
		text_resultado+='</div> <div class="clear">&nbsp;</div></div>';
		
		if ((i+1)%3 == 0) text_resultado+="<div class='clear'>&nbsp;</div>";
		
	}
	
	if (resultado.length == 0){
		text_resultado = '<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<strong class="tit">No hay elementos<strong /><br /><br />';
	}
		
	document.getElementById('resultado').innerHTML = text_resultado;
	
	document.getElementById('paginado').style.display = '';
	document.getElementById('paginado').innerHTML = paginado.getElementsByTagName('text')[0].firstChild.data;
	
}
//_____________________________________________________________________________________

function ListaDestacados(){
	
	document.getElementById('destacados').innerHTML = str_bussy;
	
	data('galeria.cod.php', 'accion=destacados', function(xml){
			
			var resultado;
			
			try{
				resultado = xml.getElementsByTagName('resultado');				
			}catch(ex){
				alert('err');
				resultado = new Array();				
				
			}
			//Pintamos el Resultado 
			
			var text_resultado = "";
			
			var i = 0;
			for(i=0;i<resultado.length;i++){
				var res = resultado[i];
				
				var id = res.getElementsByTagName('id')[0].firstChild.data;
				var img = res.getElementsByTagName('imagen')[0].firstChild.data;
				var porfolio = res.getElementsByTagName('porfolio')[0].firstChild.data;
				var puesto = res.getElementsByTagName('puesto')[0].firstChild.data;
				
				
				
				var clase = ((i%2 == 0) ? 'left' : 'right' );
				
				
				text_resultado+='<div class="'+clase+'" style="width:285px;height:219px;">\n';
				text_resultado+='<div><div style="background:url(../uploads/porfolios/'+id+'/'+img+') center ;width:259px;height:140px; border:10px solid #DEDEDE;"><a href="porfolio.php?pid='+id+'" style="display:block;width:269px;height:150px;">&nbsp;</a>\n</div><div class="opac" style="width:260px;">';
				text_resultado+=porfolio+'<br />\n'+puesto;
				text_resultado+='</div>\n</div></div>';
				if (i+1%2 == 0) text_resultado+='<br clear="all">\n';
				
			}
			if ((i+1)%2 != 0) text_resultado+='<br clear="all">\n';
			if (resultado.length == 0) text_resultado+='No hay porfolios destacados en este momento.';
			
			document.getElementById('destacados').innerHTML = text_resultado;
			
		});
	
}
//_____________________________________________________________________________________
