var indexPhoto = 1;
var indexSales = 1;

// celkovy pocet fotek
var photosCount = 4;

// rychlost animace fotky
var speedAnimate = 2000;

// rychlost animace akci na uvodce
var speedSales    = 3000;

// animace fotek
var photo_animate;
var photo_hover = false;

// animace akci na uvodce
var sales_animate;
var sales_hover = false;


$(document).ready(function() {
    photo_animate = setTimeout(function() { runAnimate(); }, speedAnimate);

    // animaci akcniho banneru spustim pouze v pripade, ze je zde vice nez 1 banner
    if (document.getElementById("akce-2")) {
        sales_animate = setTimeout(function() { runSales(); }, speedSales);

        // stop / start animace akci na uvodce
        $("#action-box").hover(
            function() {
                // zastaveni animace, pokud na ni uzivatel najel mysi
                clearTimeout(sales_animate);
                sales_hover = true;
            },
            function() {
                // znovuspusteni animace, pokud na ni uzivatel uz neni mysi
                sales_animate = setTimeout(function() { runSales(); }, speedSales);
                sales_hover = false;
            }
        );
    }

    // stop / start animace fotky
    $("#image-photo").hover(
        function() {
            // zastaveni animace, pokud na ni uzivatel najel mysi
            clearTimeout(photo_animate);
            photo_hover = true;
        },
        function() {
            // znovuspusteni animace, pokud na ni uzivatel uz neni mysi
            photo_animate = setTimeout(function() { runAnimate(); }, speedAnimate);
            photo_hover = false;
        }
    );
});



function runAnimate()
{
    $("#image-photo-holder").fadeOut("slow", function () {
        changePhoto();
        $("#image-photo-holder").fadeIn("slow");

        if (!photo_hover) {
            photo_animate = setTimeout(function() { runAnimate(); }, speedAnimate);
        }
    });
}

function runSales()
{
    $("#akce-"+indexSales).slideUp("normal", function () {
        $("#action-box div.akce").hide();

        indexSales = indexSales + 1;

        if (!document.getElementById("akce-"+indexSales)) {
            indexSales = 1;
        }

        $("#akce-"+indexSales).removeClass("non-visible");
        $("#akce-"+indexSales).slideDown("normal");

        if (!sales_hover) {
            sales_animate = setTimeout(function() { runSales(); }, speedSales);
        }
    });
}


function changePhoto()
{
    indexPhoto = indexPhoto + 1;

    if (indexPhoto > photosCount) {
        indexPhoto = 1;
    }

    $("#image-photo-holder").attr({ src: "/media/index/image-photos/image-photo-" + indexPhoto + ".jpg" });
}