// Blogs la-ciel js functions by Biktor

var blc_xmlHttp;

var posMax = 35;
var posArray=new Array();
var position = 1; //siguiente pos a pedir al servidor
var rposicion = 0;  //iteraciones realizadas para completar posMax
var goforward = 1; //bandera que señala si las posiciones van hacía adelante: 1 hacía adelante, 0 hacía atrás
var busy = 1;

	function createBlcXMLHttpRequest() {
		if (window.ActiveXObject) {
			blc_xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		else if (window.XMLHttpRequest) {
			blc_xmlHttp = new XMLHttpRequest();
		}
	}

	function getBlogInfo(){
		
		if(busy == 0)
			return;
		
		busy = 0;
		
		goforward = 1;
		
		rposicion++;
		
		if(rposicion >posMax){
			position = 1;
			rposicion = 1;
		}

		var opts = "?num="+position+"&noCache="+(new Date).getTime()+(Math.random()*1234567);

		//save position on array
		posArray[rposicion] = position;

	    createBlcXMLHttpRequest();
	    blc_xmlHttp.onreadystatechange = showBlogInfo;
    	blc_xmlHttp.open("GET", "blogs/remote/ajax_request.php"+opts, true);
    	blc_xmlHttp.send(null);
						
	}
	
	function getPreviousBlogInfo(){ 
		
		if(busy == 0)
			return;
		
		busy = 0;
		
		goforward = 0;
		
		if(rposicion-1<=0){

			if(posArray[posMax]){
				rposicion = posMax;
			}else{
				busy = 1;
				return;
			}
		}
		
		rposicion--;
		
		position = posArray[rposicion+1]; //save next position on position variable
		
		var opts = "?num="+posArray[rposicion]+"&noCache="+(new Date).getTime()+(Math.random()*1234567);
		
	    createBlcXMLHttpRequest();
	    blc_xmlHttp.onreadystatechange = showBlogInfo;
    	blc_xmlHttp.open("GET", "blogs/remote/ajax_request.php"+opts, true);
    	blc_xmlHttp.send(null);

	}
	
	function showBlogInfo(){
		
		if(blc_xmlHttp.readyState == 4) {
			if(blc_xmlHttp.status == 200) {
				takeElements();
			}
		}
		
	}
	
	function takeElements(){
		
		var xmlDoc = blc_xmlHttp.responseXML;
			
		var npos = xmlDoc.getElementsByTagName("nueva_posicion")[0].childNodes[0].nodeValue;
				
		position = npos;
		
		if(position==1){ //si nos envían 1 como nueva posición quiere decir que hemos llegado al límite verdadero, reiniciamos
			posMax = rposicion;
			rposicion = 0;
		}
				
		var url_blog = xmlDoc.getElementsByTagName("url_blog")[0].childNodes[0].nodeValue;
		
		try
		{
			var url_post = xmlDoc.getElementsByTagName("url_post")[0].childNodes[0].nodeValue;
		}catch(err){
			
			busy = 1;
			
			if(goforward == 1)
				getBlogInfo();
			else{
				getPreviousBlogInfo();
			}
			
			return;
		}
		
		try
		 {
			var titulo_blog = xmlDoc.getElementsByTagName("titulo_blog")[0].childNodes[0].nodeValue;
			
		 }catch(err){
			
			var titulo_blog = url_blog;
		 }

		try
		 {
			var titulo_post = xmlDoc.getElementsByTagName("titulo_post")[0].childNodes[0].nodeValue;
			
		 }catch(err){
			
			var titulo_post = titulo_blog;

		 }
		
		var imagen_autor = xmlDoc.getElementsByTagName("imagen_autor")[0].childNodes[0].nodeValue;

		document.getElementById("blc_img").src=imagen_autor;
		
		document.getElementById("blc_titulo_post_t").innerHTML = titulo_post;
		
		document.getElementById("blc_titulo_post_a").href = url_post;
		
		document.getElementById("blc_titulo_blog_t").innerHTML = titulo_blog;

		document.getElementById("blc_titulo_blog_a").href = url_blog;
				
		busy = 1;
		
		startGettingBlogInfo(rposicion);
	}
	
	
	function autoGetBlogInfo(pos){
		
		if(pos == rposicion)
			getBlogInfo();
		
	}
	
	function startGettingBlogInfo(pos){
		
		setTimeout("autoGetBlogInfo("+pos+")", 3500);
		
	}


