var camaraActual = 'campo'; // cámara por defecto
var idIntervalo = null;
var estadoError = false;
var imgCache = null;
var segundos=0;
var imgCargando = new Image;
imgCargando.src = '/img/elems/webcam/cargando.gif';

function iniciarWebcam(){           
    /*imgCache = null;        
    */
    if( imgCache == null ){
        imgCache = new Image();
        if( navigator.userAgent.indexOf('Gecko')!=-1 ) {
            imgCache.addEventListener('load', loadImage, false);
            imgCache.addEventListener('error', showError, false);
        } else {
            imgCache.onerror=showError;
            imgCache.onload=loadImage;
        }                    
    }
    
    if( idIntervalo != null ){
        window.clearInterval(idIntervalo);        
        idIntervalo = null;
    }    
    segundos = 0;
    actualizarImagen();
}

function verCampo(){    
    if( camaraActual != 'campo' ){       
        document.getElementById('botonCampo').className = 'botonIzqOn';
        document.getElementById('botonCasaClub').className = 'botonDerOff';
        camaraActual = 'campo';
        iniciarWebcam();
    }
}
function verCasaClub(){    
    if( camaraActual != 'casaclub' ){        
        document.getElementById('botonCampo').className = 'botonIzqOff';
        document.getElementById('botonCasaClub').className = 'botonDerOn';
        camaraActual = 'casaclub';
        iniciarWebcam();
    }
}

function actualizarTiempo(){
    if( (objTiempo = document.getElementById('tiempoRestante')) ){
        objTiempo.firstChild.nodeValue = segundos;
    }
}

function actualizarImagen(){
    // cargar la imagen dependiendo de la variable "camaraActual" que puede ser "campo" o "casaclub"
    
    if( segundos > 0 ){
        segundos--;
        actualizarTiempo();
        return;
    }
    window.clearInterval(idIntervalo);
    segundos = 30;    
    
    divImagen = document.getElementById('cajaImagen');
    
    if ( estadoError ) {
        divImagen.innerHTML = imagenHTML();
        estadoError = false;
    }    
    objImagen = document.getElementById('imagen');
    objImagen.src = imgCargando.src
        
    imgCache.src = URLImagen();
       
}

function loadImage() {
    divImagen = document.getElementById('cajaImagen');

    if ( estadoError ) {
        divImagen.innerHTML = imagenHTML();
        estadoError = false;
    }
    objImagen = document.getElementById('imagen');
    objImagen.src = imgCache.src;
    
    actualizarTiempo();
    if( idIntervalo != null ){
        window.clearInterval(idIntervalo);
        idIntervalo = null;
    }
    idIntervalo = window.setInterval( actualizarImagen, 1000 );    
}

function showError() {
    divImagen = document.getElementById('cajaImagen');

    divImagen.innerHTML = MensajeErrorHTML();
    estadoError = true;
}

function URLImagen() {
    var ahora = new Date();
    return '/libre/rgb/getImage.ats?' + camaraActual + '+' + ahora.getTime();
}

function imagenHTML() {
    return '<img id="imagen" src="/img/sp.gif" width="640" height="480" border="0" alt="webcam">';
}

function MensajeErrorHTML(){

    return '<div class="error"><div class="texto">'+mensajeError+'</div></div>';
}