﻿/* bgFade
- - - - - - - - - - - - - - - - - - - - */
function bgFade(isInit) {
    winWidth = $(window).width();
    var imgRel;
    var imgEq;
    var imgHeight;
    var imgUrl;
    $("#bg-fade>div").each(function () {
        imgRel = $(this).attr("rel")
        if (winWidth <= 1024) {
            imgHeight = 397;
            imgEq = 0;
        } else if (winWidth >= 1024 && winWidth < 1440) {
            imgHeight = 558;
            imgEq = 1;
        } else if (winWidth >= 1440) {
            imgHeight = 744;
            imgEq = 2;
        }
        imgUrl = $("#bg-img>div[rel=" + imgRel + "] span:eq(" + imgEq + ")").text();
        $(this).css({ "background": "url(" + imgUrl + ") no-repeat center top" });
        $("#bg-fade>div").css({ "width": winWidth + "px", "height": imgHeight + "px" });
        $("#head").css({ "min-height": imgHeight + "px" });
    });
    if (isInit) {
        $("#etichetta span").text($("#bg-fade>div:first").attr("title"));
       fadeCycle();
    }
}

function fadeCycle() {
    $('#bg-fade').cycle({
        fx: 'fade',
        speed: 900,
        timeout: 10000,
        before: onBefore
    });
    function onBefore() {
        if ($("#etichetta span").text() != this.title) {
            var title = this.title;
            $("#etichetta span").animate({
                opacity: 0
            }, 900, function () {
                $(this).text(title).css({ opacity: 1 })
            });
        }
    }
}


/* INIT
- - - - - - - - - - - - - - - - - - - - */
$(function () {
    if ($("#bg-fade").size()) {
        $("#bg-img").hide();
        bgFade(true);
    }
});

$(window).resize(function () {
    if ($("#bg-fade").size()) {
        bgFade(false);
    }
});




