﻿function NivPresentation(p_parent) {

    var parent;
    var items = new Array();
    var imageContainers = new Array();
    var images = new Array();

    var timer;

    var currentImagesId;
    var maxImagesId;


    initialize(p_parent);


    function initialize(p_parent) {

        items = $(p_parent).find("li.headObject");
        imageContainers = $(items).find("div.bigBanner ul li");
        images = $(items).find("img");

        parent = p_parent;

        currentImagesId = 0;
        maxImagesId = images.length - 1;

        initLink();
        createEvents();
        

        play();        
    }

    function createEvents() {
        $("div#buttonLeft").bind("mouseover", { Id: "0" }, onMouseOver);
        $("div#buttonMiddle").bind("mouseover", { Id: "1" }, onMouseOver);
        $("div#buttonRight").bind("mouseover", { Id: "2" }, onMouseOver);

        $("div#buttonLeft").bind("mouseout", onMouseOut);
        $("div#buttonMiddle").bind("mouseout", onMouseOut);
        $("div#buttonRight").bind("mouseout", onMouseOut);
    }

    function initLink() {
        toggleLink(0);
        $(images[2]).css("opacity", 0);
        $(images[1]).css("opacity", 0);
    }




    function showNextImages() {
        var idToShow = (+currentImagesId)+1;
        if (idToShow > 2) {
            idToShow = 0;
        }
        showImagesId(idToShow);
    }
    NivPresentation.prototype.showNextImages = function() { showNextImages(); }


    function showImagesId(id) {            

         if (id < currentImagesId) {
             hideImages(id);
         } else {
             showImages(id);
         }
         currentImagesId = id;
         toggleLink(id);
    }

    function hideImages(id) {
        hideUnderImage(id);
        $(images[currentImagesId]).stop().animate({ opacity: 0.0 }, { queue: false, duration: 1200 });
    }
    function hideUnderImage(id) {
        for (var i = currentImagesId - 1; i > id; i--) {
            $(images[i]).css("opacity", 0.0);
        }
    } 

    function showImages(id) {
        $(images[id]).stop().animate({ opacity: 1.0 }, { queue: false, duration: 1200 });
        showUnderImages(id);
    }
    
    /**
    * Problème ici, on crée un cross-fade, mais ceci est nécessaire dans le cas ou on commence a afficher l'image
    * d'industrue alors que mine était encore en train de s'afficher.
    *
    */
    function showUnderImages(id) {
        for (var i = 0; i < id; i++) {
            $(images[i]).stop().animate({ opacity: 1.0 }, { queue: false, duration: 1200});
        }
        
    }


    function play() {
        timer = setInterval(function() { showNextImages(); }, 5000);
    }

    function stop() {
        clearInterval(timer);
    }

    function restart() {
        stop();
        play();
    }

    function onMouseOver(event) {
        stop();
        showImagesId(event.data.Id);
    }

    function onMouseOut(event) {
        play();
    }


    function toggleLink(id) {
        var span = $(items).children("span");
        var arrow = $(items).children("div.arrowMenu");
        var bgSelected = $(items).children("div.bgSelected");

        //Désactive tous les liens
        $(span).each(function() {
            $(this).find("span").stop().css("color", "#929291");
        });
        

        //Désactive tous les fonds
        $(bgSelected).each(function() {
            $(this).stop().css("opacity", 0.0);

        });

        //Active ce lien
        $(span[id]).find("span").stop().animate({ color: "#646460" }, { queue: false, duration: 250 });
        $(bgSelected[id]).stop().animate({ opacity: "1.0" }, { queue: false, duration: 250 });    
    }

}
