﻿var numImagenesPorCategoria = 3;
var rnd = Math.floor(Math.random() * numImagenesPorCategoria);
var prevRand = [0, 0, 0, 0];
$(document).ready(function () {
    var imgAleatorias = $('.imageCompany .randomImg');
    var imgAlSrc1 = "images/p_img_petroquimicos_";
    var imgAlSrc3 = "images/p_img_autopartes_";
    var imgAlSrc2 = "images/p_img_alimentos_";
    var imgAlSrc4 = "images/p_img_telecomunicaciones_";

    CambiarImagen(rnd);
});

function CambiarImagen(rand) {
    rand = Math.floor(Math.random() * 4);
    while (rand == rnd) {
        rand = Math.floor(Math.random() * 4);
    }

    rnd = rand;

    if (rnd == 0) {
        var random = Math.floor(Math.random() * numImagenesPorCategoria);
        while (random == prevRand[0]) {
            random = Math.floor(Math.random() * numImagenesPorCategoria);
        };
        prevRand[0] = random;
        self.setTimeout("$('.imageCompany .randomImg:eq(0)').animate({ 'opacity': 0 }, 30).attr('src', 'images/p_img_petroquimicos_' + (" + (random + 1) + ") + '.jpg').animate({ 'opacity': 1 }, 600);", 1000 * (random + 1));
    } else if (rnd == 1) {
        var random = Math.floor(Math.random() * numImagenesPorCategoria);
        while (random == prevRand[1]) {
            random = Math.floor(Math.random() * numImagenesPorCategoria);
        };
        prevRand[2] = random;
        self.setTimeout("$('.imageCompany .randomImg:eq(1)').animate({ 'opacity': 0 }, 30).attr('src', 'images/p_img_autopartes_' + (" + (random + 1) + ") + '.jpg').animate({ 'opacity': 1 }, 600);", 1000 * (random + 1));
    } else if (rnd == 2) {
        var random = Math.floor(Math.random() * numImagenesPorCategoria);
        while (random == prevRand[2]) {
            random = Math.floor(Math.random() * numImagenesPorCategoria);
        };
        prevRand[1] = random;
        self.setTimeout("$('.imageCompany .randomImg:eq(2)').animate({ 'opacity': 0 }, 30).attr('src', 'images/p_img_alimentos_' + (" + (random + 1) + ") + '.jpg').animate({ 'opacity': 1 }, 600);", 1000 * (random + 1));
    } else if (rnd == 3) {
        var random = Math.floor(Math.random() * numImagenesPorCategoria);
        while (random == prevRand[3]) {
            random = Math.floor(Math.random() * numImagenesPorCategoria);
        };
        prevRand[3] = random;
        self.setTimeout("$('.imageCompany .randomImg:eq(3)').animate({ 'opacity': 0 }, 30).attr('src', 'images/p_img_telecomunicaciones_' + (" + (random + 1) + ") + '.jpg').animate({ 'opacity': 1 }, 600);", 1000 * (random + 1));
    };
    self.setTimeout("CambiarImagen(rnd)", 2000 * (rnd + 1));
}
