var inTransition = false;
var carousel_interval;
var igal_index = -1;
var current_image = "";
var next_image = "";

function initHomeImage() {
    if (igal_img_tot > 1) {
        $("#preLoadImg").show();
        inTransition = true;
        $.preload(igal_array_src, {
            init: function (loaded, total) {
            },
            loaded: function (img, loaded, total) {
                if (loaded == 1) {
                    inTransition = false;
                    carousel_interval = setInterval("galleryImmaginiNext(true)", 5000);
                }
                else if (loaded == 2) {
                    carousel_interval = setInterval("galleryImmaginiNext(false)", 5000);
                }
                $("#preLoadImg").hide();
            },
            loaded_all: function (loaded, total) {
                $("#preLoadImg").hide();
            }
        });
    }
}

(function ($) {
    var imgList = [];
    $.extend({
        preload: function (imgArr, option) {
            var setting = $.extend({
                init: function (loaded, total) { },
                loaded: function (img, loaded, total) { },
                loaded_all: function (loaded, total) { }
            }, option);
            var total = imgArr.length;
            var loaded = 0;

            try {
                setting.init(0, total);
                for (var i in imgArr) {
                    $("." + imgArr[i]).load(function () {
                        loaded++;
                        setting.loaded(this.href, loaded, total);
                        if (loaded == total) {
                            setting.loaded_all(loaded, total);
                        }
                    });
                }
            }
            catch (e) {
                $("#preLoadImg").hide();
                inTransition = false;
                carousel_interval = setInterval("galleryImmaginiNext(false)", 5000);
            }
            finally {
                $("#preLoadImg").hide();
            }
        }
    });
})(jQuery);



/**
* Visualizza immagine successiva (destra)
*/
function galleryImmaginiNext(stop_carousel) {
    if (!inTransition) {

        if (stop_carousel) {
            clearInterval(carousel_interval);
        }

        igal_index++;

        if (igal_index < 0)
            current_image = igal_array_src[0];
        else
            current_image = igal_array_src[igal_index];

        next_index = igal_index + 1;
        if (igal_index >= igal_img_tot - 1) {
            next_index = 0;
            igal_index = -1;
        }
        next_image = igal_array_src[next_index];

        galleryImmaginiShowImage();
    }
}

function galleryImmaginiNextForced(stop_carousel) {

    if (stop_carousel) {
        clearInterval(carousel_interval);
    }

    igal_index++;

    if (igal_index < 0)
        current_image = igal_array_src[0];
    else
        current_image = igal_array_src[igal_index];



    next_index = igal_index + 1;
    if (igal_index >= igal_img_tot - 1) {
        next_index = 0;
    }
    next_image = igal_array_src[next_index];

    galleryImmaginiShowImageForced();
}

function galleryImmaginiShowId(ix) {

    igal_index = ix - 2;
    galleryImmaginiNext(true);
}

function galleryImmaginiShowIdForced(ix) {
    igal_index = ix - 2;
    galleryImmaginiNextForced(true);

}

function onHighligth(x) {

    $(x).animate({
        opacity: 0.9
    }, 315);
}

function offHighligth(x) {

    $(x).animate({
        opacity: 0.4
    }, 315);
};

function on(x) {
    var x1 = x.alt;
    galleryImmaginiShowIdForced(x1);
    $(x).animate({
        opacity: 0.9
    }, 315);
}

function off(x) {
    inTransition = false;
    carousel_interval = setInterval("galleryImmaginiNext(false)", 5000);
    $(x).animate({
        opacity: 0.4
    }, 315);
};

function goto(x) {  
    var url = x.longDesc;
    window.location.href = url;
};


/**
* Visualizza immagine precedente (sinistra)
*/
function galleryImmaginiPrev() {
    if (!inTransition) {
        clearInterval(carousel_interval);

        igal_index--;
        if (igal_index < 0) {
            igal_index = igal_img_tot - 1;
        }
        current_image = igal_array_src[igal_index];
        next_index = igal_index - 1;
        if (next_index < 0) {
            next_index = igal_img_tot - 1;
        }
        next_image = igal_array_src[next_index];
        galleryImmaginiShowImage();
    }
}


function galleryImmaginiShowImage() {
    //indica che c'è una transizione in corso
    inTransition = true;
    //visualizza la prossima immagine nel contenitore sotto (contenitore_immagine2)
    $("#contenitore_immagine2 img").css('display', 'none');
    $("#contenitore_immagine2 img." + next_image).css('display', 'block');
    //fade-out dell'immagine corrente
    $('#contenitore_immagine1 img.' + current_image).fadeOut(800, function () {
        //quando finisce la transizione "sposta" l'immagine visualizzata nel contenitore sopra (contenitore_immagine1)
        $("#contenitore_immagine1 img").css('display', 'none');
        $("#contenitore_immagine1 img." + next_image).css('display', 'block');
        //indica il termine della transizione
        inTransition = false;
    });
}

function galleryImmaginiShowImageForced() {
    //indica che c'è una transizione in corso
    inTransition = true;
    //visualizza la prossima immagine nel contenitore sotto (contenitore_immagine2)
    $("#contenitore_immagine2 img").css('display', 'none');
    $("#contenitore_immagine2 img." + next_image).css('display', 'block');
    //fade-out dell'immagine corrente
    $('#contenitore_immagine1 img.' + current_image).fadeOut(800, function () {
        //quando finisce la transizione "sposta" l'immagine visualizzata nel contenitore sopra (contenitore_immagine1)
        $("#contenitore_immagine1 img").css('display', 'none');
        $("#contenitore_immagine1 img." + next_image).css('display', 'block');
        //indica il termine della transizione
    });
}

function LoadNext() {
    var url = $('#nextGallery').val();
    window.location.href = url;
}

