var variable_temporizador;
var total_imagenes;

function dimetamaño(){
    var _i = new Image();
    var imagenVisible = false;
    var margen=0;
    $('#back').css('margin-top',margen+'px');
    $('#back').css('margin-left',margen+'px');

    //Tamaño ventana
    var size = { x: $(window).width(), y: $(window).height() };

    //Mediadas Imagen a redimensionar
    
    $(document).find(".background").each( function() {
        if ($(this).css('display') != 'none') {
            _i.src= url_web + $(this).attr('src');
            imagenVisible=true;
        }
    });

    if(imagenVisible){
        if((size.x/size.y)<=(_i.width/_i.height)){
            //Resolucion ventana menor o igual a la de la imagen, imagen ajustada a altura
            _i.width = (_i.width*size.y)/_i.height;
            _i.height = size.y;
            $('.background').css('width',_i.width+"px");
            $('.background').css('height',_i.height+"px");
            
            margen = (_i.width-size.x)/2;
            margen=-margen;
            $('#back').css('margin-left',margen+'px');
        }else{
            //Resolucion ventana mayor a la de la imagen, imagen ajustada a anchura
            _i.height = (_i.height*size.x)/_i.width;
            _i.width = size.x;
            $('.background').css('width',_i.width+"px");
            $('.background').css('height',_i.height+"px");
            
            margen = (_i.height-size.y)/2;
            margen=-margen;
            $('#back').css('margin-top',margen+"px");
        }
    }
    $('body').css('width',size.x+"px");
    $('body').css('height',size.y+"px");
    $('#auxiliar').css("height",(size.y-50)+"px");
    
}


function stopBackgroundRotative(){
    $(document).find('.background').each(function(){
                $(this).hide();
            })
    clearInterval(variable_temporizador);
}

function startBackgroundRotative(){
    $(document).find('.background:first-child').show();
    dimetamaño();
    variable_temporizador = setInterval(function(){changeBackground()}, 10000);
}

function changeBackground() {
    //Consigo la url de la imagen mostrada
    $(document).find(".background").each( function() {
        if ($(this).css('display') != 'none') {
            $.src_image = $(this).attr('src');
        }
    });
    if ($.src_image != 'none') {
        //Consigo el nombre de la imagen sin extensión
        var array = $.src_image.split("/");
        var file = array[array.length-1];
        var number = file.substring(0, file.length-4);
        var numberToShow = parseInt(number) +1;
        var srcShow = "img/fondos/" + numberToShow + ".jpg";

        //Oculto la anterior y pongo visble la siguiente
        if(number==total_imagenes){
            $(document).find('.background:last-child').hide();
            $(document).find('.background:first-child').show();
        }else{
            $(document).find(".background").each( function() {
                if ($(this).attr('src') == $.src_image) {
                    $(this).hide();
                }else if ($(this).attr('src') == srcShow){
                    $(this).show();
                }
            });
        }
        dimetamaño();
    }
}
