﻿var slideshowSpeed = 5000;
var photos = [{ "image": "bg0.jpg" }, { "image": "bg1.jpg" }, { "image": "bg2.jpg" }, { "image": "bg3.jpg" }, { "image": "bg4.jpg" }, { "image": "bg5.jpg" }, { "image": "bg6.jpg"}];

$(document).ready(function () {
    var interval;
    var autoplay = false;
    var currentImg;
    var animating = false;

    $("#back").click(function () { stopAnimation(); navigate("back"); });
    $("#next").click(function () { stopAnimation(); navigate("next"); });

    $("#control").toggle(
        function () {
            autoplay = true;
            $(this).css({ "background-image": "url(/images/btn_pause.png)" });
            navigate("next");
            interval = setInterval(function () { navigate("next"); }, slideshowSpeed);
        },
        function () { stopAnimation(); autoplay = false; }
    );


    if (get_cookie("gout") != null) { currentImg = get_cookie("gout"); }
    else { currentImg = 0; }


    var navigate = function (direction) {
        if (animating) { return; }
        if (direction == "next") {
            currentImg++;
            if (currentImg == photos.length + 1) { currentImg = 1; }
        } else {
            currentImg--;
            if (currentImg == 0) { currentImg = photos.length; }
        }
        showImage(photos[currentImg - 1]);
    };

    var showImage = function (photoObject) {
        $("body").css({ "background-image": "url(/images/bg/" + photoObject.image + ")"});
        animating = false;
        if (!autoplay) { set_cookie("gout", currentImg - 1, 365, "/", ".perfea.fr", ""); }
    };

    var stopAnimation = function () {
        $("#control").css({ "background-image": "url(/images/btn_play.png)" });
        clearInterval(interval);
    };

    navigate("next");
    //interval = setInterval(function () { navigate("next"); }, slideshowSpeed);
});
