var estrella;
var httpObject = null;
var httpObject2 = null;
var httpObject3 = null;

var undiv;

function getHTTPObject(){
	if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest) return new XMLHttpRequest();
	else {	alert("Your browser does not support AJAX.");
			return null;}
}

function cuenta_caracteres(maximo) {
	var caracteres = document.getElementById("zs3").value.length;
	var valor = maximo - caracteres;
	if(valor<0) { document.getElementById("cuantasletras").style.color='#FF0000'; }
	else { document.getElementById("cuantasletras").style.color='#000000'; } 
	document.getElementById("cuantasletras").innerHTML = valor;
}

function miraurl() {
	var theurl = document.getElementById("zs41").value;
	theurl = theurl.toLowerCase();
	theurl = theurl.replace("http://", "");
	theurl = theurl.replace("https://", "");	
	document.getElementById("zs41").value = theurl;
}

function zspaginar(id, page) {
	
			//pasamos los comentariosa a alpha y mostramos el loader
			$('comentarios').tween('opacity', 0);	
			$('comentarios_loader').style.visibility='visible';	
			$('comentarios_loader').style.display='block';	
			
			//myVerticalSlide_loader.slideIn();
			
			//creamos los objetos HTTP para los comentarios y los numeros
			httpObject3 = getHTTPObject();
			httpObject2 = getHTTPObject();
			
			if (httpObject3 != null) {
				url = "index.php";
				params = "option=com_zaragozacomments&task=paginar&page=" + page + "&id="+ id;	
				httpObject3.open("POST", url, true);
				httpObject3.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
				httpObject3.setRequestHeader("Content-length", params.length);
				httpObject3.setRequestHeader("Connection", "close");
				httpObject3.send(params);	
				httpObject3.onreadystatechange = setOutput2;
			}
			
			if (httpObject2 != null) {
				url = "index.php";
				params = "option=com_zaragozacomments&task=numeros&page=" + page + "&id="+ id;
				httpObject2.open("POST", url, true);
				httpObject2.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
				httpObject2.setRequestHeader("Content-length", params.length);
				httpObject2.setRequestHeader("Connection", "close");
				httpObject2.send(params);
				httpObject2.onreadystatechange = setOutput3;
			}	
		}
	
		function setOutput2(){
			if(httpObject3.readyState == 4) {	
				//mostramos los comentarios y ocultamos el cargador
				$('comentarios').innerHTML = httpObject3.responseText;
				$('comentarios').tween('opacity', 1);

				$('comentarios_loader').style.visibility='hidden';	
				$('comentarios_loader').style.display='none';	
				//myVerticalSlide_loader.slideOut();
				//crea_slides();
				milkbox.reloadGalleries();
	
			}
		}

		function setOutput3(){
			if(httpObject2.readyState == 4) {
				//ponemos los numeraco
				document.getElementById('zspaginar').innerHTML = httpObject2.responseText;
				//llamamos a la funcion de caraga paginacion
				$('comentarios_back').style.backgroundImage='none';
				$('comentarios_loader').style.visibility='hidden';	
				$('comentarios_loader').style.display='none';	
				milkbox.reloadGalleries();
	
		}

		
	}//fin funcion






function contesta(id, titulo){
	
	
	document.getElementById("answer_comment").style.display = 'block';
	document.getElementById("answer_comment").style.visibility = 'visible';

	document.getElementById("zs40").value = 'Re: ' + titulo;
	document.getElementById("articleid").value = id;
	
	
	document.getElementById("answer_comment_comentario2").innerHTML = document.getElementById('zc-sc-' + id).innerHTML;

	document.getElementById("zstitulonew_").style.display = 'none';
	document.getElementById("zstitulonew_").style.visibility = 'hidden';
	
	document.getElementById("zc_answer").style.display = 'block';
	document.getElementById("zc_answer").style.visibility = 'visible';	
	document.getElementById("zsanswernew_").style.display = 'block';
	document.getElementById("zsanswernew_").style.visibility = 'visible';	
	
	var scroll = new Fx.Scroll(window, { wait: false, duration: 2500, transition: Fx.Transitions.Quad.easeInOut });
	scroll.toElement('ancla_nuevo');
	
	
	}
	
function anewcomment(){
	
	document.getElementById("answer_comment").style.display = 'none';
	document.getElementById("answer_comment").style.visibility = 'hidden';

	document.getElementById("zs40").value = document.getElementById("title2").value;
	document.getElementById("articleid").value = document.getElementById("articleid2").value;
	document.getElementById("answer_comment_comentario2").innerHTML = "";

	document.getElementById("zstitulonew_").style.display = 'block';
	document.getElementById("zstitulonew_").style.visibility = 'visible';	
	
	document.getElementById("zc_answer").style.display = 'none';
	document.getElementById("zc_answer").style.visibility = 'hidden';
	document.getElementById("zsanswernew_").style.display = 'none';
	document.getElementById("zsanswernew_").style.visibility = 'hidden';
	
	}	
	
function quote(){
	var texto_quote;
	texto_quote = '[quote]' + document.getElementById("answer_comment_comentario2").innerHTML + '[/quote]';
	tinyMCE.activeEditor.dom.setHTML(tinyMCE.activeEditor.dom.select('p'), texto_quote);
	}	

	
function contesta2(){
	var scroll = new Fx.Scroll(window, { wait: false, duration: 2500, transition: Fx.Transitions.Quad.easeInOut });
	scroll.toElement('ancla_nuevo');
	}
function ancla_comentarios(){
	var scroll = new Fx.Scroll(window, { wait: false, duration: 2500, transition: Fx.Transitions.Quad.easeInOut });
	scroll.toElement('ancla_comentarios');
	}


function zsreload() {
	//funcion que vuelve a cargar el capcha
	var ruta;
	var aleatorio;
	var aleatoriotexto;
	ruta = document.getElementById('route').value;
	aleatorio = Math.random();
	aleatoriotexto = String(aleatorio);
	aleatoriotexto = aleatoriotexto.replace(".","A") ;
	document.getElementById('zscaptchaimage').src= ruta + '&sid=' + aleatoriotexto ; 
	return false;
}
	
function quitarimagen(numero) {
	// quita la imagen y muestra de nuevo el formulario
	
	if(numero==1) { 
		document.getElementById('zsimagenplus1').innerHTML='';
		document.getElementById('zsimg').value='';
		}
	if(numero==2) { 
		document.getElementById('zsimagenplus2').innerHTML='';
		document.getElementById('zsimg2').value='';
		}	
	if(numero==3) { 
		document.getElementById('zsimagenplus3').innerHTML='';
		document.getElementById('zsimg3').value='';
		}		
	
	var cuantasfotosvan =  document.getElementById('contador_img').value;
	cuantasfotosvan =  cuantasfotosvan - 1;
	document.getElementById('contador_img').value = cuantasfotosvan;
	
	if(cuantasfotosvan==0) {
		document.getElementById('zstheimage').innerHTML='';
	}
	document.getElementById('zsaddimage').style.display='block';
	document.getElementById('zsaddimage').style.visibility='visible';
	document.getElementById('zsaddimagetxt').style.display='block';
	document.getElementById('zsaddimagetxt').style.visibility='visible';
}

function verhilo(id){
		//muestra hilo  ¿funciona?
		document.getElementById('zo-hilo' + id).style.visibility='visible';
		document.getElementById('zo-hilo' + id).style.display='block';
}

function ocultahilo(id){
		//oculta hilo ¿funciona?
		document.getElementById('zo-hilo' + id).style.visibility='hidden';
		document.getElementById('zo-hilo' + id).style.display='none';
}
//-----------------------------------------------------------------------------------------------------votos

//------------------------------------------------------------------------------------------- dom ready mootools
window.addEvent('domready', function() {

	// hilos de los comentarios
	zshilos = new Array;
	crea_slides(); //crea los slides dinamicamente
	
	// --------------------------------------------------------------------si esxiste el formulario de envio
	if($('existeform')) { 
	
	
		$('show_link').style.visibility = 'visible';
		$('show_link').style.display = 'block';
		$('show_images').style.visibility = 'visible';
		$('show_images').style.display = 'block';
		$('show_map').style.visibility = 'visible';
		$('show_map').style.display = 'block';
		$('show_event').style.visibility = 'visible';
		$('show_event').style.display = 'block';
		$('show_youtube').style.visibility = 'visible';
		$('show_youtube').style.display = 'block';
		$('show_facebook').style.visibility = 'visible';
		$('show_facebook').style.display = 'block';
		$('show_twitter').style.visibility = 'visible';
		$('show_twitter').style.display = 'block';
			
			
		if($('yourlink')) { 
				var yourlink = new Fx.Slide('show_link');
				yourlink.hide();
				$('yourlink').addEvent('click', function(e){
				e.stop();	
				oculta_slides('yourlink');
				(function() { yourlink.toggle(); }).delay(400);  
				});}	
				
		if($('yourimages')) { 
				var yourimages = new Fx.Slide('show_images');
				yourimages.hide();
				$('yourimages').addEvent('click', function(e){
				e.stop();	
				oculta_slides('yourimages');
				(function() { yourimages.toggle(); }).delay(400);  
				});}	
				
		if($('yourmap')) { 
				var yourmap = new Fx.Slide('show_map');
				yourmap.hide();
				$('yourmap').addEvent('click', function(e){
				e.stop();	
				oculta_slides('yourmap');
				(function() { yourmap.toggle(); }).delay(400);  
				});}	
				
		if($('yourevent')) { 
				var yourevent = new Fx.Slide('show_event');
				yourevent.hide();
				$('yourevent').addEvent('click', function(e){
				e.stop();	
				oculta_slides('yourevent');
				(function() { yourevent.toggle(); }).delay(400);  
				});}					
			
			
		if($('yourfacebook')) { 
				var yourfacebook = new Fx.Slide('show_facebook');
				yourfacebook.hide();
				$('yourfacebook').addEvent('click', function(e){
				e.stop();	
				oculta_slides('yourfacebook');
				(function() { yourfacebook.toggle(); }).delay(400);  
				});}
		
		if($('yourtwitter')) { 
				var yourtwitter = new Fx.Slide('show_twitter');
				yourtwitter.hide();
				$('yourtwitter').addEvent('click', function(e) {
				e.stop();	
				oculta_slides('yourtwitter');
				(function() { yourtwitter.toggle(); }).delay(400);  
				});}
		
		if($('youryoutube')) { var youryoutube = new Fx.Slide('show_youtube');youryoutube.hide();
				$('youryoutube').addEvent('click', function(e){
				e.stop();	
				oculta_slides('youryoutube');
				(function() { youryoutube.toggle(); }).delay(400);  
				});}
				
		function oculta_slides(noculto) {
				if(noculto!='yourlink'&&$('yourlink')) { 	yourlink.slideOut();	};
				if(noculto!='yourimages'&&$('yourimages')) { 	yourimages.slideOut();	};
				if(noculto!='yourmap'&&$('yourmap')) { 	yourmap.slideOut();	};
				if(noculto!='yourevent'&&$('yourevent')) { 	yourevent.slideOut();	};
				if(noculto!='yourfacebook'&&$('yourfacebook')) { 	yourfacebook.slideOut();	};
				if(noculto!='yourtwitter'&&$('yourtwitter')) { 		yourtwitter.slideOut();	};	
				if(noculto!='youryoutube'&&$('youryoutube')) {		youryoutube.slideOut();	};
		}
		
		//Slides
		$('enviando').tween('opacity', 0);

		$('botonenvio').addEvent('click', function(e){
			e.stop();
			$('enviando').style.visibility='visible';
			$('enviando').style.display='block';
			$('enviando').tween('opacity', 1);
			$('mensaje').tween('opacity', 0);
			$('mensaje').style.visibility='hidden';
			$('mensaje').style.display='none';
			
			httpObject = getHTTPObject();
		
			if (httpObject != null) {
					var publicemail;
					var id =  document.getElementById('articleid').value;
					var id2 =  document.getElementById('articleid2').value;
					var zs20 = document.getElementById('zs20');
					var zs45 = document.getElementById('zs45');
					var zs42 = document.getElementById('zs42');
					var zsnot = document.getElementById('zsnot');
					var zsimg = document.getElementById('zsimg');
					var zsimg2 = document.getElementById('zsimg2');
					var zsimg3 = document.getElementById('zsimg3');
					var zsavat = document.getElementById('zsavat');
					var zs85 = document.getElementById('zs85');
					var zs86 = document.getElementById('zs86');
					var zs87 = document.getElementById('zs87');
					var zslat = document.getElementById('zslat');
					var zslon = document.getElementById('zslon');	
					var zs3 = document.getElementById('zs3').value;
					var zsavatar = document.getElementById('zsavatar').value;
					var lon = document.getElementById('lng').value;
					var lat = document.getElementById('lat').value;
					var dayi = document.getElementById('diai').value;
					var dayf = document.getElementById('diaf').value;
					var mesi = document.getElementById('mesi').value;
					var mesf = document.getElementById('mesf').value;
					var anoi = document.getElementById('anoi').value;
					var anof = document.getElementById('anof').value;
					var userfb = document.getElementById('userfb').value;
					
					publicemail=0; notify=0;
				
					var comments;
					comments = zs3;
					comments = encodeURIComponent(comments);
					
					
					var zsavfb = 0 ; var zsnotfb = 0;
					if(document.getElementById('zsavfb').checked==true) { zsavfb=1; }
					if(document.getElementById('zsnotfb').checked==true) { zsnotfb=1; }
					
					if(document.getElementById('zs5').checked==true) { publicemail=1; }
					if(zsnot) {	if(document.getElementById('zsnot').checked==true) { notify=1; } }
					
					url = "index.php";
					params = "option=com_zaragozacomments&task=new&zs1="+document.getElementById('zs1').value+ "&zs2=" + document.getElementById('zs2').value + "&zs3=" + comments  + "&zs5=" + publicemail + "&zs40=" +document.getElementById('zs40').value + "&zs41=" +document.getElementById('zs41').value + '&id=' + id+ '&id2=' + id2 +'&zsav=' + zsavatar +'&userfb=' + userfb;
					if(zs20) { params = params + "&zs20=" + document.getElementById('zs20').value;}
					if(zs45) { params = params + "&zs45=" + document.getElementById('zs45').value;}
					if(zs42) { params = params + "&zs42=" + document.getElementById('zs42').value;}
					if(zs85) { params = params + "&zs85=" + document.getElementById('zs85').value;}
					if(zs86) { params = params + "&zs86=" + document.getElementById('zs86').value;}
					if(zs87) { params = params + "&zs87=" + document.getElementById('zs87').value;}		
					if(zsimg) { params = params + "&zsimg=" + document.getElementById('zsimg').value;}
					if(zsimg2) { params = params + "&zsimg2=" + document.getElementById('zsimg2').value;}
					if(zsimg3) { params = params + "&zsimg3=" + document.getElementById('zsimg3').value;}
					
					if(lon) { params = params + "&lon=" + document.getElementById('lng').value;}
					if(lat) { params = params + "&lat=" + document.getElementById('lat').value;}
					if(dayi) { params = params + "&dayi=" + document.getElementById('diai').value;}
					if(dayf) { params = params + "&dayf=" + document.getElementById('diaf').value;}
					if(mesi) { params = params + "&mesi=" + document.getElementById('mesi').value;}
					if(mesf) { params = params + "&mesf=" + document.getElementById('mesf').value;}
					if(anoi) { params = params + "&anoi=" + document.getElementById('anoi').value;}
					if(anof) { params = params + "&anof=" + document.getElementById('anof').value;}
					
					params = params + "&zsnot=" + notify + "&zsavfb=" + zsavfb + "&zsnotfb=" + zsnotfb;
					
					httpObject.open("POST", url, true);
					httpObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
					httpObject.setRequestHeader("Content-length", params.length);
					httpObject.setRequestHeader("Connection", "close");
					httpObject.send(params);
					httpObject.onreadystatechange = setOutput;
					
				}
			});
	
		
		}
		//-------------------------------------------------------------fin de existencia de formulario
		
		function crea_slides(){
			
			zs = 0; zss = zs;
			if($('zo-hilo'+zs)) { 
				zshilos[zs]  = new Fx.Slide('zo-hilo'+zs);
			}		
			if($('zsshow' + zss)) {
				zshilos[zss].hide();
				$('zo-hilo' + zss).style.visibility='visible';
				$('zo-hilo' + zss).style.display='block';
				$('zsshow' + zss).addEvent('click', function(e){
					e.stop();			
					zshilos[this.rel].slideIn();
					});
				$('zshide' + zss).addEvent('click', function(e){
					e.stop();			
					zshilos[this.rel].slideOut();
					});
			}
		}
		
		
		function setOutput(){
			
			if(httpObject.readyState == 4){
			
				var izquierda;
				var derecha;
	
				$('mensaje').style.visibility='visible';
				$('mensaje').style.display='block';	
				$('enviando').style.visibility='hidden';
				$('enviando').style.display='none';	
				$('enviando').tween('opacity', 0);
				$('mensaje').tween('opacity', 1);
				
				
				izquierda = httpObject.responseText.substr(0,2);
				
				
				
				if(izquierda=='1#')	{ 
							derecha = httpObject.responseText.substr(2,(httpObject.responseText.length -2));
				} else {
							derecha = httpObject.responseText;
				}
			
				document.getElementById('mensaje').innerHTML = derecha;
				
				if(izquierda=='1#')	{
					var scroll = new Fx.Scroll(window, { wait: false, duration: 2500, transition: Fx.Transitions.Quad.easeInOut });
					scroll.toElement('ancla_comentarios');
					zspaginar(document.getElementById('articleid2').value,1);
				} else {
					$('botonenvio').disabled = false;	
				}
				
			}
		}//fin funcion
	
		if($('comentarios_loader')) {
			var myVerticalSlide_loader = new Fx.Slide('comentarios_loader');
		}
	
		
	
});
