// JavaScript Document
// Víctor J. Chamorro - Ipdea.com

window.document.write("<script type='text/javascript' src='../js/XHConn.js'></script>"); 
window.document.write("<script type='text/javascript' src='../js/compruebacampos.js'></script>");
/*window.document.write("<script type='text/javascript' src='js/lightwindow.js'></script>");
window.document.write("<script type='text/javascript' src='js/prototype.js'></script>");
window.document.write("<script type='text/javascript' src='js/js/scriptaculous.js?load=effects'></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_iid=0;
var g_aid=0;

//___________________________________________________________________________________________________________

function VerImagenes(){

	document.getElementById('insertacomentario').style.display = 'none';
	document.getElementById('resultadoscomentarios').style.display = 'none';
	document.getElementById('miniaturas').style.display="";
	document.getElementById('miniaturas').innerHTML = str_bussy;	
	var pid = document.frmporfolio.pid.value;
	data('galeria.cod.php', 'accion=verimagenes&pagina=1&id_porfolio='+pid, ImprimeResultadosImagenes);
	
	
	
}
//___________________________________________________________________________________________________________

function VerAnimaciones(){
	
	
	document.getElementById('insertacomentario').style.display = 'none';
	document.getElementById('resultadoscomentarios').style.display = 'none';
	document.getElementById('miniaturas').style.display="";
	document.getElementById('miniaturas').innerHTML = str_bussy;
	var pid = document.frmporfolio.pid.value;
	data('galeria.cod.php', 'accion=veranimaciones&pagina=1&id_porfolio='+pid, ImprimeResultadosAnimaciones);
	
}
//___________________________________________________________________________________________________________

function VerPerfil(){
	document.getElementById('resultadoscomentarios').style.display = 'none';
	document.getElementById('insertacomentario').style.display = 'none';
	document.getElementById('miniaturas').style.display="";
	document.getElementById('miniaturas').innerHTML = str_bussy;
	var pid = document.frmporfolio.pid.value;
	data('galeria.cod.php', 'accion=verperfil&pagina=1&id_porfolio='+pid, ImprimeResultadosPerfil);
	
}
//___________________________________________________________________________________________________________

function VerComentarios(){

	document.getElementById('miniaturas').style.display="";
	document.getElementById('insertacomentario').style.display = '';
	//document.getElementById('miniaturas').innerHTML = str_bussy;
	var pid = document.frmporfolio.pid.value;
	
	functionlater="GLOBAL_formulario = document.frmporfolio; CargaEventoCampos();";
	data('galeria.cod.php', 'accion=vercomentarios&pagina=1&iid='+g_iid+'&aid='+g_aid+'&id_porfolio='+pid, ImprimeResultadosComentarios);
	
}
//___________________________________________________________________________________________________________

function ImprimeResultadosImagenes(xml){
	
	var resultado;
	var paginado;
	
	try{
		resultado = xml.getElementsByTagName('resultado');
		paginado = xml.getElementsByTagName('paginado')[0];
	}catch(ex){
		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 img = res.getElementsByTagName('img')[0].firstChild.data;
		var titulo = res.getElementsByTagName('titulo')[0].firstChild.data;
		
		text_resultado+= '<a href="javascript:MuestraDatosImagen('+id+');">';
		text_resultado+= '<img src="../uploads/porfolios/imagenes/'+id+'/'+img+'" width="94" height="65" title="'+titulo+'" />';
		text_resultado+= '</a>';
		
		if ((i+1)%5 == 0) text_resultado+="<br /><br />";
		
	}
	
	if (resultado.length == 0){
		text_resultado = '<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<strong class="tit">No hay elementos<strong /><br /><br />';
	}
		
	document.getElementById('miniaturas').innerHTML = text_resultado;
	
	document.getElementById('modulo-buscar').style.display = '';
	document.getElementById('modulo-buscar').innerHTML = paginado.getElementsByTagName('text')[0].firstChild.data;
	
	
}
//___________________________________________________________________________________________________________

function ImprimeResultadosPerfil(xml){
	
	var resultado;
		
	try{
		resultado = xml.getElementsByTagName('resultado');
	}catch(ex){
		resultado = new Array();
	}
	//Pintamos el Resultado 
	
	var text_resultado = "";
	
	
	
	for(var i=0;i<resultado.length;i++){
		try{
		var res = resultado[i];
		
		var id = res.getElementsByTagName('id')[0].firstChild.data;
		var nick = res.getElementsByTagName('nick')[0].firstChild.data;
		var nombre = res.getElementsByTagName('nombre')[0].firstChild.data;
		var apellidos = res.getElementsByTagName('apellidos')[0].firstChild.data;
		var dni = res.getElementsByTagName('dni')[0].firstChild.data;
		var direccion = res.getElementsByTagName('direccion')[0].firstChild.data;
		var provincia= res.getElementsByTagName('provincia')[0].firstChild.data;
		var localidad= res.getElementsByTagName('localidad')[0].firstChild.data;
		var cp= res.getElementsByTagName('cp')[0].firstChild.data;
		var email = res.getElementsByTagName('email')[0].firstChild.data;
		var pais = res.getElementsByTagName('pais')[0].firstChild.data;
		var mostrar_email = res.getElementsByTagName('mostrar_email')[0].firstChild.data;

		text_resultado+='<label>Nick</label>';
		text_resultado+='<p>'+nick+'</p>';
		text_resultado+='<label>Nombre</label>';
		text_resultado+='<p>'+nombre+' '+apellidos+'</p>';
		text_resultado+='<label>Localidad</label>';
		text_resultado+='<p>'+localidad+'</p>';
		text_resultado+='<label>Provincia</label>';
		text_resultado+='<p>'+provincia+'</p>';
		if (mostrar_email ==1) text_resultado+='<label>E-mail</label><p>'+email+'</p>';
		
		
		}catch(ex){
			alert(ex.description);
		}
		
	}
	
	if (resultado.length == 0){
		text_resultado = '<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<strong class="tit">No hay elementos<strong /><br /><br />';
	}
		
	document.getElementById('miniaturas').innerHTML = text_resultado;
	
	document.getElementById('modulo-buscar').style.display = '';

}
//___________________________________________________________________________________________________________

function ImprimeResultadosAnimaciones(xml){

	var resultado;
	var paginado;
	
	try{
		resultado = xml.getElementsByTagName('resultado');
		//paginado = xml.getElementsByTagName('paginado')[0];
	}catch(ex){
		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 img = res.getElementsByTagName('img')[0].firstChild.data;
		var titulo = res.getElementsByTagName('titulo')[0].firstChild.data;
		var archivo = res.getElementsByTagName('archivo')[0].firstChild.data;
		
		text_resultado+= '<a href="javascript:MuestraDatosAnimacion('+id+');">';
		
		//text_resultado+= '<img src="../uploads/porfolios/animaciones/'+id+'/'+img+'" width="94" height="65" title="'+titulo+'" />';
		if (archivo != "none"){
			text_resultado+= '<img src="../uploads/porfolios/animaciones/'+id+'/'+img+'" width="94" height="65" title="'+titulo+'" />';
		}else{
			text_resultado+= '<img src="http://i.ytimg.com/vi/'+img+'/default.jpg" width="94" height="65" title="'+titulo+'" />';
		}
		
		text_resultado+= '</a>';
		
		if ((i+1)%5 == 0) text_resultado+="<br /><br />";
		
	}
	
	if (resultado.length == 0){
		text_resultado = '<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<strong class="tit">No hay elementos<strong /><br /><br />';
	}
		
	document.getElementById('miniaturas').innerHTML = text_resultado;
	
	document.getElementById('modulo-buscar').style.display = '';
	//document.getElementById('modulo-buscar').innerHTML = paginado.getElementsByTagName('text')[0].firstChild.data;
}
//___________________________________________________________________________________________________________

function ImprimeResultadosComentarios(xml){

	var resultado;
		
	try{
		resultado = xml.getElementsByTagName('resultado');
	}catch(ex){
		alert('error');
		resultado = new Array();
	}
	//Pintamos el Resultado 
	
	var text_resultado = "";
	text_resultado += '<div id="menu-porfolio"><ul><li>Comentarios:</li></ul></div>';
	
	for(var i=0;i<resultado.length;i++){
		var res = resultado[i];
		
		var id = res.getElementsByTagName('id')[0].firstChild.data;
		var comentario = res.getElementsByTagName('comentario')[0].firstChild.data;
		var nombre = res.getElementsByTagName('nombre')[0].firstChild.data;
		var fecha= res.getElementsByTagName('fecha')[0].firstChild.data;
		var enlace = res.getElementsByTagName('enlace')[0].firstChild.data;
		
		text_resultado+='<label>';
		
		if (enlace == 0){
			text_resultado+=nombre;
		}else{
			text_resultado+='<a href="porfolio.php?pid='+enlace+'">'+nombre+'</a>';
		}
		
		text_resultado+=' '+fecha+'</label>';
		text_resultado+='<p>'+comentario+'</p>';
		
		
	}
	
	if (resultado.length == 0){
		text_resultado = '<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<strong class="tit">No hay comentarios<strong /><br /><br />';
	}
	
	
	
	document.getElementById('resultadoscomentarios').innerHTML = text_resultado;
	
	
}
//___________________________________________________________________________________________________________

function GuardaComentario(){

	var campos = new Object();
	campos.obligatorios = new Array('autor_comentario', 'texto_comentario');

	if (!CompruebaCampos(campos, document.frmporfolio)) return;

	var pid = document.frmporfolio.pid.value;
	var nombre = document.frmporfolio.autor_comentario.value;
	var comentario = document.frmporfolio.texto_comentario.value;
	
	
	data('galeria.cod.php', 'accion=nuevocomentario&pagina=1&iid='+g_iid+'&aid='+g_aid+'&id_porfolio='+pid+'&nombre='+nombre+'&comentario='+comentario, ImprimeResultadosComentarios);
	
	document.frmporfolio.autor_comentario.value = nombre;
	document.frmporfolio.texto_comentario.value = "";
	
	CompruebaCampos(campos, document.frmporfolio);
	
}
//___________________________________________________________________________________________________________

function MuestraDatosImagen(id){
	
	g_iid=id;
	g_iid=0;
	functionlater="VerComentarios()";
	data('galeria.cod.php', 'accion=datosimagen&id='+id+'&pagina=1', function(xml){
																		
			try{
				
				
				res = xml.getElementsByTagName('resultado')[0]; //Solo el primero
			
				//Pintamos el Resultado
				
				var id = res.getElementsByTagName('id')[0].firstChild.data;
				var img = res.getElementsByTagName('img')[0].firstChild.data;
				var titulo = res.getElementsByTagName('titulo')[0].firstChild.data;
				var descripcion = res.getElementsByTagName('descripcion')[0].firstChild.data;
				var fecha = res.getElementsByTagName('fecha')[0].firstChild.data;
				var valoracion = res.getElementsByTagName('valoracion')[0].firstChild.data;
				
				//Calculamos el ancho que mostraremos
				
				var image = new Image();
				image.src = "../uploads/porfolios/imagenes/"+id+"/"+img;
				image.onload = function (){
					if (this.width > 540){
						document.getElementById("img_"+id).width = 540;
						
					}else{
						document.getElementById("img_"+id).width = this.width;
						document.getElementById("div_img_"+id).style.width = this.width+"px";
					}
				}
				
				i_width = 540;
				text_resultado = "<div style='background-color:#666666;'><span style='float:left;'>"+titulo+"</span> <span style='float:right;'>"+fecha+"</span><br clear='all' /></div>";
				text_resultado+= "<div id='div_img_"+id+"' style='margin:5px auto; width:"+i_width+"px;'><a rel='lightbox' href='../uploads/porfolios/imagenes/"+id+"/"+img+"' title='"+titulo+"'><img id='img_"+id+"' src='../uploads/porfolios/imagenes/"+id+"/"+img+"' width='"+i_width+"' style='margin:0px;' /></a></div>";
				text_resultado+= "<div>"+descripcion+"</div>";
				text_resultado+= "<div id='bloquevotacion' style='background-color:#666666;'><label>Votos</label>";
				for (ii=0;ii<5;ii++) {
					if (valoracion > ii){
						text_resultado+='<a href="javascript:vota('+id+','+(ii+1)+',\'i\');" title="Votar con un '+(ii+1)+'" alt=Votar con un '+(ii+1)+'" class="staron">&nbsp;</a> ';
					}else{
						text_resultado+='<a href="javascript:vota('+id+','+(ii+1)+',\'i\');" title="Votar con un '+(ii+1)+'" alt=Votar con un '+(ii+1)+'" class="staroff">&nbsp;</a> ';
					}
				}
				text_resultado+= "<br clear='all' /></div>";
				
				document.getElementById('miniaturas').innerHTML = text_resultado;
				document.getElementById('insertacomentario').style.display = '';
				document.getElementById('resultadoscomentarios').style.display = '';
				g_iid = id;
				g_aid = 0;
				//VerComentarios(id);

				
			}catch(ex){
				alert('Error al procesar los datos');
			}
	});

}
//___________________________________________________________________________________________________________

function MuestraDatosAnimacion(id){

	g_iid=0;
	g_iid=id;
	functionlater="VerComentarios()";
	data('galeria.cod.php', 'accion=datosanimacion&id='+id+'&pagina=1', function(xml){
																		
						try{
							res = xml.getElementsByTagName('resultado')[0]; //Solo el primero
						
							//Pintamos el Resultado
							
							var id = res.getElementsByTagName('id')[0].firstChild.data;
							var img = res.getElementsByTagName('img')[0].firstChild.data;
							var titulo = res.getElementsByTagName('titulo')[0].firstChild.data;
							var descripcion = res.getElementsByTagName('descripcion')[0].firstChild.data;
							var fecha = res.getElementsByTagName('fecha')[0].firstChild.data;
							var archivo = res.getElementsByTagName('archivo')[0].firstChild.data;
							var valoracion = res.getElementsByTagName('valoracion')[0].firstChild.data;
							
							var contenido = "";
							
							var i_width = "416";
							
							if (archivo == "none"){
								contenido = '<object width="425" height="344">\n';
								contenido += '<param name="movie" value="http://www.youtube.com/v/'+img+'&hl=es"></param>';
								contenido += '<embed src="http://www.youtube.com/v/'+img+'&hl=es" type="application/x-shockwave-flash" width="425" height="344"></embed>\n</object>';
							}else{
								
								var image = new Image();
								image.src = "../uploads/porfolios/animaciones/"+id+"/"+img;
								
								/*if (image.width > 540){
									i_width = 540;
								}else{
									i_width = image.width;
								}
								*/
								
								image.onload = function (){
									if (this.width > 540){
											document.getElementById("img_"+id).width = 540;
											
									}else{
											document.getElementById("img_"+id).width = this.width;
											document.getElementById("div_img_"+id).style.width = this.width+"px";
									}
								}
								
								i_width = 540;
								contenido = "<a href='../uploads/porfolios/animaciones/"+id+"/"+archivo+"'>\n";
								contenido+= '<img id=img_'+id+' src="../uploads/porfolios/animaciones/'+id+'/'+img+'" width="'+i_width+'" title="'+titulo+'" />';
								contenido+= "</a>\n";
							}
							
							text_resultado = "<div style='background-color:#666666;'><span style='float:left;'>"+titulo+"</span> <span style='float:right;'>"+fecha+"</span><br clear='all' /></div>";
							text_resultado+= "<div style='margin:5px auto; width:"+i_width+"px;'>"+contenido+"</div>";
							text_resultado+= "<div>"+descripcion+"</div>";
							text_resultado+= "<div id='bloquevotacion' style='background-color:#666666;'><label>Votos</label>";
							for (ii=0;ii<5;ii++) {
								if (valoracion > ii){
									text_resultado+='<a href="javascript:vota('+id+','+(ii+1)+',\'a\');" title="Votar con un '+(ii+1)+'" alt=Votar con un '+(ii+1)+'" class="staron">&nbsp;</a> ';
								}else{
									text_resultado+='<a href="javascript:vota('+id+','+(ii+1)+',\'a\');" title="Votar con un '+(ii+1)+'" alt=Votar con un '+(ii+1)+'" class="staroff">&nbsp;</a> ';
								}
							}
							text_resultado+= "<br clear='all' /></div>";
							
							
							
							document.getElementById('miniaturas').innerHTML = text_resultado;
							document.getElementById('insertacomentario').style.display = '';
							document.getElementById('resultadoscomentarios').style.display = '';
							
							g_aid = id;
							g_iid = 0;
							
						}catch(ex){
							alert('Error al procesar los datos');
						}
	});

}
//___________________________________________________________________________________________________________

function vota(id, voto, tipo){
	
	if (!confirm('¿Desea dar un '+voto+' como su votación?')) return;
	
	data('galeria.cod.php', 'accion=votar&id='+id+'&tipo='+tipo+'&voto='+voto, function(xml){
			try{
				res = xml.getElementsByTagName('data')[0].firstChild.data; //Solo el primero
				if (res == "1"){
					document.getElementById('bloquevotacion').innerHTML = '<strong>Voto guardado correctamente.</strong>';
				}else{
					document.getElementById('bloquevotacion').innerHTML = '<strong>Voto procesado.</strong>';
				}

			}catch(ex){
				document.getElementById('bloquevotacion').innerHTML = '<strong>No se ha podido procesar el voto.</strong>';
			}				
		
	  });
}
//___________________________________________________________________________________________________________