var banner = function() {
var imagesToLoad = [
    'SLIDE_head_01.png',
    'SLIDE_head_02.png',
    'SLIDE_head_03.png',
    'SLIDE_head_04.png',
    'SLIDE_head_05.png',
    'SLIDE_head_06.png',
    'SLIDE_head_07.png',
    'SLIDE_head_08.png'];
var imagesToShow = [];
var indexImage = 0;
var imgPath='img/banner/'
var effects = ['blind', 'clip', 'drop', 'explode', 'fold', 'puff', 'slide', 'scale'];
var indexEffects = 0;
var enableLog = false;
var enableEffects = true;

var getImageToShow = function () {
    return imagesToShow[indexImage];
};

var getNextImageToShow = function () {
    indexImage += 1;
    if (indexImage >= imagesToShow.length) {
        indexImage = 0;
    }
    return imagesToShow[indexImage];
};

function log(msg) {
    if (enableLog)
        console.log(msg);
}

$("document").ready(function (){
    log("Documento pronto");
    startDownloading(imagesToLoad);
});

var startDownloading = function (images) {
    var img = null;
    var imgTag = '';
    for (i in images) {
        img_src = images[i];
        img_name = getNameFromSource(img_src);
        imgTag = '<img id="' + img_name + '" />';
        $("#backstage").append(imgTag);
        $("#" + img_name).load(function(a, b, c) {
            imageReadyToShow(this.src); 
        }).attr('src', imgPath + img_src);
    }
};

var getNameFromSource = function (imgSource) {
    return imgSource.replace('.', '_');
};

var imageReadyToShow = function (image) {
    log('[imageReadyToShow] image "' + image + '" is ready to show');
    imagesToShow.push(image);
    if (imagesToShow.length == 1) {
        startCycle();
    }
};

var startCycle = function () {
    img = getImageToShow();
    log('[startCycle] showing img "' + img + '"');
    startShow(img);
};

var startShow = function (img_src){
    log('[startShow] img="' + img_src + '"');
    $('#div_slide').append('<img id="img_show_down" class="hidden img_slide"/>');
    $('#div_slide').append('<img id="img_show_upper" class="hidden img_slide"/>');
    $('#img_show_down').attr('src', img_src);

    showImageWithEffect(img_src);
};

var endShowAnimation = function () {
    $('#img_show_down').attr('src', $('#img_show_upper').attr('src'));
    $('#img_show_down').show();
    $('#img_show_upper').hide();
    var img_src = getNextImageToShow();
    showImageWithEffect(img_src);
};

var showImageWithEffect = function (img_src) {
    $('#img_show_upper').attr('src', img_src);
    var effects = getNextEffect();
    log('[showImageWithEffect] img_src: ' + img_src + ' effects: ' + effects);
    if (enableEffects) {
        $('#img_show_upper').show(effects, {}, 2000, function() { 
            endShowAnimation();
        });
    } else {
        $('#img_show_upper').show('');
        setTimeout(function() { 
            endShowAnimation();
        }, 2000);
    }
};

// var endHideAnimation = function() {
//     img = getNextImageToShow();
//     startShow(img);
// };


var getNextEffect = function (){
    indexEffects += 1;
    if (indexEffects >= effects.length) {
        indexEffects = 0;
    }
    return effects[indexEffects];
};
}();
