//LLAMADAS AJAX
function enviarRespuesta(url,padre,comentario,captcha) {
	var querystring = crearQueryStringPost(padre,comentario)+"&code="+captcha;
	var myAjax = null;

	myAjax = new Ajax.Request(
	url,
	{
		method: 'post',
		asynchronous: true,
		parameters: querystring,
		onComplete:processPOST
	});
}

function enviarRespuestaAnonima(url,padre,comentario,captcha,nombre) {

	var querystring = crearQueryStringPost(padre,comentario)+"&code="+captcha+"&nombre="+nombre;
	var myAjax = null;

	myAjax = new Ajax.Request(
	url,
	{
		method: 'post',
		asynchronous: true,
		parameters: querystring,
		onComplete:processPOST
	});
}

function enviarComentario(url,comentario,captcha) {
	var querystring = crearQueryStringPost(0,comentario)+"&code="+captcha;
	var myAjax = null;
	
	
	myAjax = new Ajax.Request(
	url,
	{
		method: 'post',
		asynchronous: true,
		parameters: querystring,
		onComplete:processPOSTComentario
	});
}

function enviarComentarioAnonimo(url,comentario, captcha,nombre) {
	var querystring = crearQueryStringPost(0,comentario)+"&code="+captcha+"&nombre="+nombre;
	var myAjax = null;

	myAjax = new Ajax.Request(
	url,
	{
		method: 'post',
		asynchronous: true,
		parameters: querystring,
		onComplete:processPOSTComentario
	});
}

function eliminarComentario(cod) {
	var querystring = crearQueryStringEliminar(cod);
	var url='/verVideo/eliminar.phtml';
	var myAjax = null;

	myAjax = new Ajax.Request(
	url,
	{
		method: 'post',
		asynchronous: true,
		parameters: querystring,
		onComplete:processDeleteRecord
	});
}

function enviarVotoComentario(url,codComentario,tipo,pag){
	var querystring ="codComentario="+codComentario+"&tipo="+tipo+"&pag="+pag;
	var myAjax = null;

	myAjax = new Ajax.Request(
	url,
	{
		method: 'post',
		asynchronous: true,
		parameters: querystring,
		onComplete:proccessVotoComentario
	});
}
//FIN LLAMADAS AJAX

function mostrarComentarios(id){
	document.getElementById("respuesta"+id).style.display = "block";
	document.getElementById("comentarioMas"+id).style.display = "none";
	document.getElementById("comentarioMenos"+id).style.display = "block";
}
function ocultarComentarios(id){
	document.getElementById("respuesta"+id).style.display = "none";
	document.getElementById("comentarioMas"+id).style.display = "block";
	document.getElementById("comentarioMenos"+id).style.display = "none";
}

function responder(padre){
	document.getElementById('capaComentar').style.display='block';
	document.getElementById('capaComentarios').style.display='none';
	document.getElementById("padre").value =padre;
	document.getElementById("btnInsertarComentario").onclick=function(){respondiendo();};
	texto=document.getElementById("ocultarPorSpamTexto"+padre).innerHTML;
	document.getElementById("respondiendoA").style.display='block';	
	document.getElementById("textoRespondiendoA").innerHTML=texto;	
}
function borrarComentario(id_comentario){
	document.getElementById('comentarioABorrar').value=id_comentario;
	document.getElementById('avisoBorrar').style.display='block';
	document.getElementById('avisoDiv').innerHTML='¡¡ATENCIÓN!!<br />SI TU COMENTARIO TIENE RESPUESTAS TAMBIÉN SERÁN ELIMINADAS <br />¿Estás seguro que deseas eliminar este comentario? ';
	document.getElementById('avisoDiv').style.display='block';
	document.getElementById('votos_envoltorio').style.display='block';
	document.getElementById("votos_envoltorio").style.top = posicY + 'px';
	document.getElementById("votos_envoltorio").style.left = (posicX -200) + 'px';
}
function borrarRespuesta(id_respuesta){
	document.getElementById('comentarioABorrar').value=id_respuesta;
	document.getElementById('avisoBorrar').style.display='block';
	document.getElementById('avisoDiv').innerHTML='¿Estas seguro de que deseas eliminar esta respuesta?';
	document.getElementById('avisoDiv').style.display='block';
	document.getElementById('votos_envoltorio').style.display='block';
	document.getElementById("votos_envoltorio").style.top = posicY + 'px';
	document.getElementById("votos_envoltorio").style.left = (posicX -200) + 'px';
}

function responderAnonimo(padre){
	document.getElementById('capaComentar').style.display='block';
	document.getElementById('capaComentarios').style.display='none';
	document.getElementById("padre").value =padre;
	document.getElementById("btnInsertarComentario").onclick=function(){respondiendoAnonimo();}; 
	texto=document.getElementById("ocultarPorSpamTexto"+padre).innerHTML;
	document.getElementById("respondiendoA").style.display='block';	
	document.getElementById("textoRespondiendoA").innerHTML=texto;	
}

function respondiendoAnonimo(){
	enviarRespuestaAnonima("/verVideo/respuesta.phtml",document.getElementById("padre").value,document.getElementById("comentario").value,document.getElementById("code").value,document.getElementById("nombre").value);
}

function respondiendo(){
	enviarRespuesta("/verVideo/respuesta.phtml",document.getElementById("padre").value,document.getElementById("comentario").value,document.getElementById("code").value);
}


function insertarComentario(){
	enviarComentario("/verVideo/comentar.phtml",document.getElementById("comentario").value,document.getElementById("code").value);
}

function insertarComentarioAnonimo(){
	enviarComentarioAnonimo("/verVideo/comentar.phtml",document.getElementById("comentario").value,document.getElementById("code").value,document.getElementById("nombre").value);
}

function comentarVideo(){
	document.getElementById('capaComentar').style.display='block';
	document.getElementById('capaComentarios').style.display='none';
	document.getElementById("btnInsertarComentario").onclick=function(){insertarComentario();};
	document.getElementById("respondiendoA").style.display='none';	
}

function comentarVideoAnonimo(){
	document.getElementById('capaComentar').style.display='block';
	document.getElementById('capaComentarios').style.display='none';
	document.getElementById("btnInsertarComentario").onclick=function(){insertarComentarioAnonimo();};
	document.getElementById("respondiendoA").style.display='none';	
}

function votarComentario(codComentario,tipo,pag){
	document.getElementById('manos'+codComentario).innerHTML='<img src="/img/ico_ani_votar.gif" align="absmiddle"/>';
	enviarVotoComentario("/verVideo/votarComentario.phtml",codComentario,tipo,pag);
}

function cancelarComentario(){
	document.getElementById('capaComentar').style.display='none';
	document.getElementById('capaComentarios').style.display='block';
}

function redibujarCajaComentarios(text){
	try {
	    img = document.getElementById('imgCaptcha');
	    img.src = '/verVideo/securimage_show.php?' + Math.random();
	}
	catch(e){}
	try {
	    btn = document.getElementById('btnInsertarComentario');
	    btn.disabled = false;
	}
	catch(e){}
	try{
	    spanError=document.getElementById("error_comentario");
	    spanError.innerHTML="<center>"+text+"</center>";
	}
	catch(e){}	
}


function cerrarAviso(){
	document.getElementById('votos_envoltorio').style.display='none';
}

function mostrarComentario(codComentario){
	document.getElementById('ocultarPorSpam'+codComentario).style.display='block';
	document.getElementById('ocultarPorSpamTexto'+codComentario).style.display='block';
	document.getElementById('spamMostrar'+codComentario).style.display='none';
	document.getElementById('spamOcultar'+codComentario).style.display='block';
	if (document.getElementById('respuesta'+codComentario)) document.getElementById('respuesta'+codComentario).style.display='block';
}

function ocultarComentario(codComentario){
	document.getElementById('ocultarPorSpam'+codComentario).style.display='none';
	document.getElementById('ocultarPorSpamTexto'+codComentario).style.display='none';
	document.getElementById('spamMostrar'+codComentario).style.display='block';
	document.getElementById('spamOcultar'+codComentario).style.display='none';
	if (document.getElementById('respuesta'+codComentario)) document.getElementById('respuesta'+codComentario).style.display='none';
}

//PROCESAR RESPUESTAS AJAX
function processXML(request) {
	var comentarios;
	comentarios =  request.responseText;
	if (comentarios=='') {
		setTimeout("getXMLget('/verVideo/comentarios.phtml')", 10000);
		return;
	}
	document.getElementById("DIV-comentarios").innerHTML = comentarios;
}

//FUNCIONES PARA FACEBOOK
function obtenerDatosVideo(url,comentario) {
	var strurl = '/verVideo/obtenerDatos.phtml';
	var querystring = 'url='+url+'&comentario='+comentario;
	var myAjax = null;
	
	myAjax = new Ajax.Request(
	strurl,
	{
		method: 'post',
		asynchronous: true,
		parameters: querystring,
		onComplete: function(request){
			var mensaje = eval('('+request.responseText+')');
		}
	});
}

function getXMLComentar(url) {
	var xmlHttpReq = false;
	var self = this;
	var strURL = url+'?'+crearQueryString();;
	if (window.XMLHttpRequest) self.xmlHttpReq = new XMLHttpRequest();
	else if (window.ActiveXObject) {
		try {self.xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");}   
	    catch (err){
	        try {self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");}
	        catch (err2){}
	    }
	}
	self.xmlHttpReq.open('GET', strURL, true);
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	self.xmlHttpReq.onreadystatechange = function() {
		if (self.xmlHttpReq.readyState == 4) {
			processXMLComentar(self.xmlHttpReq);
		}
	}
	
	self.xmlHttpReq.send(null);
}

function processXMLComentar(request) {
	var comentarios;
	comentarios =  request.responseText;
	if (comentarios=='') {
		setTimeout("getXMLget('/verVideo/comentarios.phtml')", 10000);
		return;
	}
	var url = document.URL;

	var comentario = document.getElementById("comentario").value;

	document.getElementById("DIV-comentarios").innerHTML = comentarios;
	obtenerDatosVideo(url,comentario);
}


function proccessVotoComentario(request){
	//NADA
	if (request.responseText!=''){
		arrayResp=request.responseText.split('-#-#-');
		codComentario=arrayResp[0];
		resp=arrayResp[1];
		document.getElementById('manos'+codComentario).style.display='none';
		document.getElementById('manos_off'+codComentario).style.display='inline';
		if (resp!="error") document.getElementById('numvotos'+codComentario).innerHTML=resp;
		else {
			document.getElementById('votos_envoltorio').style.display='block';
			document.getElementById("votos_envoltorio").style.top = posicY + 'px';
			document.getElementById("votos_envoltorio").style.left = (posicX -200) + 'px';
			document.getElementById('avisoDiv').innerHTML=arrayResp[2];
			document.getElementById('avisoBorrar').style.display='none';
		}
	}
}

function processPOST(request){
    if(request.responseText){
        var texto = Trim(request.responseText)
    	if(texto.length>0) redibujarCajaComentarios(request.responseText);
        else getXMLget("/verVideo/comentarios.phtml");
     }
     else{
    	getXMLget("/verVideo/comentarios.phtml");
    }
}

function processPOSTComentario(request){
    if(request.responseText){
        var texto = Trim(request.responseText)
    	if(texto.length>0){
            redibujarCajaComentarios(request.responseText);
        }else{
        	pagina='last';
        	//getXMLget("/verVideo/comentarios.phtml");
        	getXMLComentar("/verVideo/comentarios.phtml");
        }
    }else{
    	pagina='last';
		//getXMLget("/verVideo/comentarios.phtml");
    	getXMLComentar("/verVideo/comentarios.phtml");
    }
}

//FIN PROCESAR RESPUESTAS AJAX

//Para mostrar la capa oculta de mensajes
var IE = document.all?true:false
if (!IE) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = posicRatonXY;
var posicX = 0
var posicY = 0

function posicRatonXY(e) {
	if (IE) {
		posicX = event.clientX + document.documentElement.scrollLeft;
		posicY = event.clientY + document.documentElement.scrollTop;
	}
	else {
		posicX = e.pageX; posicY = e.pageY;
	}
	if (posicX < 0) {posicX = 0};
	if (posicY < 0) {posicY = 0};
	
	return true
}