var speed=100;
var div=1;
//var max=4;
var mov=0;
var alp=0;
var moo=0;
var max=0;

function rand ( n )
{
  return ( Math.floor ( Math.random ( ) * n + 1 ) );
}

function inicio(idm){
  max=idm;
  div=rand(max);
  if (max>1){
    document.getElementById('oferta0').style.display='none';
    reiniciar(div);
    update();
  }
}

function reiniciar(noferta){
  if (max>1){
    nofertaa=noferta+1;
    z=max;

    lightup('oferta'+noferta,100);
    document.getElementById('oferta'+noferta).style.zIndex = max;

    if (nofertaa>max){
      nofertaa=1;
      for (i=nofertaa; i<max; i++){
        z=z-1;
        lightup('oferta'+i,100);
        document.getElementById('oferta'+i).style.zIndex = z;
      }
    }else{
      for (i=nofertaa; i<=max; i++){
        z=z-1;
        lightup('oferta'+i,100);
        document.getElementById('oferta'+i).style.zIndex = z;
      }
      for (i=1; i<noferta; i++){
        z=z-1;
        lightup('oferta'+i,100);
        document.getElementById('oferta'+i).style.zIndex = z;
      }
    }
  }
}

function update(){
  if (moo==0 && max>1){
    mov=mov+1;
    if (mov>30 && mov<=80){
      alp=alp+2;
      lightup('oferta'+div,100-alp);
    }else if (mov>80){
      div=div+1;
      if (div>max){
        div=1;
      }
      reiniciar(div);
      mov=0;
      alp=0;
    }
  }else{
    if (mov>55){
      div=div+1;
      if (div>max){
        div=1;
      }
      reiniciar(div);
    }
    mov=30;
    alp=0;
    lightup('oferta'+div,100-alp);
  }
  setTimeout("update()",speed);
}

function lightup(oferta, opacity){
  if (navigator.appName.indexOf("Netscape")!=-1&&parseInt(navigator.appVersion)>=5){
    document.getElementById(oferta).style.MozOpacity=opacity/100;
  }else if (navigator.appName.indexOf("Microsoft")!= -1&&parseInt(navigator.appVersion)>=4){
    document.getElementById(oferta).filters.alpha.opacity=opacity;
  }
}

function cargaContenido(url, metodo, funcion) {
  peticion_http = inicializa_xhr();
 
  if(peticion_http) {
    peticion_http.onreadystatechange = funcion;
    peticion_http.open(metodo, url, true);
    peticion_http.send(null);
  }
}
 
function inicializa_xhr() {
  if(window.XMLHttpRequest) {
    return new XMLHttpRequest();
  }
  else if(window.ActiveXObject) {
    return new ActiveXObject("Microsoft.XMLHTTP");
  }
}
function milista() {
  if(peticion_http.readyState == READY_STATE_COMPLETE) {
    if(peticion_http.status == 200) {
      var texto;
      texto=peticion_http.responseText;
      texto=texto.split(";");
      document.cita.hora.length = texto.length; 
      for(var i=0; i< texto.length; i++){
        texto[i]=texto[i].split("|");
        document.cita.hora.options[i].value=texto[i][0]; 
        document.cita.hora.options[i].text=texto[i][1]; 
      }
      document.cita.hora.disabled=false;
    }
  }
} 
function lista(act, centro) {
  cargaContenido("cita.php?act=js&select=horas&fecha="+act+"&centro="+centro, "GET", milista);
}

