var id_Temporizador = '';
var id_Scroll = '';

var tempVisual = 5000;
var tempScroll = 10;
var posicion = 0;
var div_alto = 240; //Define la altura del div
var incremento = 124; //Cuanto va a ser el incremento
var numero_divs = 0;

var tDivs = ['div_1','div_2','div_3','div_4','div_5','div_6','div_7','div_8','div_9','div_10','div_11'];  //Array con todos los divs a mostrar
var tMostrando = [2,0,1];  //Array con los divs que se estan mostrando
var tTope = [0,0,0,0,0];  //Array para indicar hasta donde tienen que bajar los divs
var tIncremento = [0,0,0,0,0];  //Array para indicar en que posicion esta cada divs el incrementar hasta el tope
var div_siguiente = 3;
function init(){
   posicion = -190;
   posicion = 0;
   numero_divs = devolverNumeroDivs();
   for(i=0;i<tMostrando.length;i++){
      document.getElementById(tDivs[tMostrando[i]]).style.visibility = 'visible';
      document.getElementById(tDivs[tMostrando[i]]).style.top = posicion + 'px';
      posicion = posicion + incremento;
   }
   setTimeout('doPersiana()', tempVisual);
}

function doPersiana(){

   for(i=0;i<tMostrando.length;i++){
      posActual = recogerNumerico(document.getElementById(tDivs[tMostrando[i]]).style.top);
      tTope[i] = posActual + incremento;
      tIncremento[i] = posActual;
   }

   if(recogerNumerico(document.getElementById(tDivs[tMostrando[tMostrando.length-1]]).style.top)>div_alto){
      document.getElementById(tDivs[tMostrando[tMostrando.length-1]]).style.visibility = 'hidden';
      document.getElementById(tDivs[tMostrando[tMostrando.length-1]]).style.top = -124;
      ordenarTabla();
   }

   clearTimeout(id_Scroll);
   doSiguiente();

   id_Temporizador = setTimeout("doPersiana()", tempVisual);
}

function doSiguiente(){

   for(i=0;i<tMostrando.length;i++){
      posActual = recogerNumerico(document.getElementById(tDivs[tMostrando[i]]).style.top)
      if(tIncremento[i]<=tTope[i]){
         /*nombre = 'valor'+i;
         document.getElementById(nombre).value=tIncremento[i];*/

         tIncremento[i]=tIncremento[i]+1;
         document.getElementById(tDivs[tMostrando[i]]).style.top = posActual + 1 + 'px';
      }
   }
   id_Scroll = setTimeout("doSiguiente()", tempScroll);
}

function ordenarTabla(){
   for(i=tMostrando.length-1;i>0;i--){
      tMostrando[i] = tMostrando[i-1];
   }
   tMostrando[0] = div_siguiente;
   div_siguiente++;
   document.getElementById(tDivs[tMostrando[i]]).style.top = '-124px';
   document.getElementById(tDivs[tMostrando[i]]).style.visibility = 'visible';
   if(div_siguiente>numero_divs) div_siguiente = 0;

}

function recogerNumerico(valor){
   var posicion = valor.indexOf('px');
   valor=parseInt(valor.substring(0,posicion));
   return valor;
}

function devolverNumeroDivs(){
   for(i=0;i<tDivs.length;i++){
      if(!eval(document.getElementById(tDivs[i]))){
         return i-1;
         break;
      }
   }
}
