﻿var carrousel1 = null;

$(document).ready(function () {
	var activFrame = 0;
	vitesse = 5000;
	var pause = 0;
	
	carrousel1 = new WunCarrousel("#carrousel", true, 2500, 0, "sliding", true, false, "v");
	carrousel1.init();
	
	carrousel2 = new WunCarrousel("#iphone_carrousel", true, 3000, 0, "sliding", true, true, "h");
	carrousel2.init();

});



// Custom Carrousel //
function WunCarrousel(aID, aAuto, aTimerLenght, aCurrentSlide, aAnimation, aContinue, ShowSlideCount, aOrientation) {
    this.ID = aID;
    this.Lenght = 0;
    this.Timer = null;
    this.TimerLenght = aTimerLenght;
    this.Auto = aAuto;
    this.Direction = 'suivant';
    this.CurrentSlide = aCurrentSlide - 1; // Start : 0
    this.Animation = aAnimation;    // Option: "fade", "sliding"
    this.Delta = 0;
    this.Continue = aContinue;
    this.ShowSlideCount = ShowSlideCount;
    this.IsAnimated = false;
    this.marginR = 0;
    this.marginT = 0;
    this.VisibleSlide = 0;
    this.Orientation = (aOrientation == null || aOrientation == "") ? "h" : aOrientation; //Options: "h", "V"

    this.init = function () {
        var monContexte = this; // Récupère ce carrousel dans les méthodes 'function'

        $(this.ID + ' .slide:first')
            .fadeIn('fast')
            .addClass('actif')
            .next().addClass('prev');

        if (this.Orientation == "h")
            this.VisibleSlide = Math.round($(this.ID + ' .slide-window').width() / ($(this.ID + ' .slide:first').width() + this.marginR));
        else
            this.VisibleSlide = Math.round($(this.ID + ' .slide-window').height() / ($(this.ID + ' .slide:first').height() + this.marginT));


        this.Lenght = $(this.ID + ' .slide').length;

        if (this.Continue) {
            $(this.ID + ' .slider').prepend($(this.ID + ' .slide:gt(' + (this.Lenght - this.VisibleSlide - 1) + ')').clone());
            $(this.ID + ' .slider').append($(this.ID + ' .slide:gt(' + (this.VisibleSlide - 1) + '):lt(' + (this.VisibleSlide) + ')').clone());

            this.Lenght = $(this.ID + ' .slide').length;
            this.CurrentSlide = this.VisibleSlide - 1;
        }

        // Obsolete pour ce carrousel
        // Clic slide gauche
        $(this.ID + ' .fleche_d').click(function () {
            monContexte.clearTimer();
            monContexte.Direction = 'suivant';
            monContexte.nextSlide();
            return false;
        });

        // Clic slide droite
        $(this.ID + ' .fleche_g').click(function () {
            monContexte.clearTimer();
            monContexte.Direction = 'precedent';
            monContexte.nextSlide();
            return false;
        });

        // Hover Stop animation :
        $(this.ID).hover(function () { monContexte.clearTimer(); }, function () { monContexte.setTimer(); });

        // Si il y a 1 seule slide, on ne fait pas apparaitre les flèches et on bloque le carrousel
        if (this.Lenght > 1) {
            $(this.ID + ' .fleche_g, ' + this.ID + ' .fleche_d').fadeIn('fast');
        } else if (this.Lenght == 1) {
            this.Auto = false;
        }

        $(this.ID + ' .slide:last').css('margin-right', 0);

        if (this.Orientation == "h") {
            this.marginR = parseInt($(this.ID + ' .slide:first').css('margin-right'));
            tailleSlider = (($(this.ID + ' .slide:first').width() + this.marginR) * $(this.ID + ' .slide').size());
            $(this.ID + ' .slider').css('width', tailleSlider);
        }
        else {
            this.marginT = parseInt($(this.ID + ' .slide:first').css('margin-top'));
            tailleSlider = (($(this.ID + ' .slide:first').height() + this.marginT) * $(this.ID + ' .slide').size());
            $(this.ID + ' .slider').css('height', tailleSlider);
        }




        if (this.ShowSlideCount) {
            $(this.ID + ' .slide-count').html('');
            for (var i = 0; i < this.Lenght - (this.Continue ? 2 : 0); i++) { $(this.ID + ' .slide-count').append('<div tabindex="0">' + (i + 1) + '</div>'); }
            $(this.ID + ' .slide-count > div').click(function () {
                monContexte.clearTimer();
                monContexte.Direction = 'suivant';
                monContexte.nextSlide($(monContexte.ID + ' .slide-count > div').index($(this)) + (monContexte.Continue ? 1 : 0));
            });
        } else { $(this.ID + ' .slide-count').hide(); }



        this.nextSlide();
    }

    this.setTimer = function () {
        if (this.Auto == true) {
            var monContexte = this;
            this.Timer = setTimeout(function () { monContexte.nextSlide(); }, this.TimerLenght);
        }
    }
    this.clearTimer = function () {
        if (this.Auto == true) {
            clearTimeout(this.Timer);
        }
    }
    this.nextSlide = function (Position, NoAnimation) {
        if (this.IsAnimated) { return false; }
        if (this.Animation == "sliding") { this.IsAnimated = true; }
        if (Position != undefined) { this.CurrentSlide = Position - 1; this.Direction = 'suivant'; } // Position Start: 0
        if (this.Direction == 'suivant') {
            if (this.CurrentSlide == (this.Lenght - 1)) {
                this.CurrentSlide = 0;
            } else {
                this.CurrentSlide++;
            }
        } else if (this.Direction == 'precedent') {
            if (this.CurrentSlide == 0) {
                this.CurrentSlide = this.Lenght - 1;
            } else {
                this.CurrentSlide--;
            }
        }

        //Check Fleches
        if ((this.CurrentSlide == (this.Lenght - 1) || this.CurrentSlide + this.VisibleSlide >= this.Lenght) && (this.Continue == false)) {
            $(this.ID + ' .fleche_d, ' + this.ID + ' .blur_d').fadeOut('fast');
        } else {
            $(this.ID + ' .fleche_d, ' + this.ID + ' .blur_d').fadeIn('fast');
        }
        if ((this.CurrentSlide == 0) && (this.Continue == false)) {
            $(this.ID + ' .fleche_g, ' + this.ID + ' .blur_g').fadeOut('fast');
        } else {
            $(this.ID + ' .fleche_g, ' + this.ID + ' .blur_g').fadeIn('fast');
        }


        $(this.ID + ' .slide').removeClass('prev');
        $(this.ID + ' .actif').addClass('prev');
        $(this.ID + ' .prev').removeClass('actif');
        $(this.ID + ' .slide').eq(this.CurrentSlide).addClass('actif');

        $(this.ID + ' .slide').eq(this.CurrentSlide + 1).addClass('opaque');

        var context = this;
        if (this.Animation == "fade") {
            $(this.ID + ' .prev').fadeOut('slow');
            $(this.ID + ' .actif').fadeIn('slow');
        }
        else if (this.Animation == "sliding") {
            if (NoAnimation) {
                if (this.Orientation == "h") {
                    $(this.ID + ' .slider').css('left', -this.CurrentSlide * ($(this.ID + ' .slide').width() + this.marginR) - (this.CurrentSlide == 0 ? -this.Delta : -this.Delta));
                }
                else {
                    $(this.ID + ' .slider').css('top', -this.CurrentSlide * ($(this.ID + ' .slide').height() + this.marginT) - (this.CurrentSlide == 0 ? -this.Delta : -this.Delta));
                }

                this.IsAnimated = false;
            }
            else {
                if (this.Orientation == "h") {
                    $(this.ID + ' .slider').animate({ left: -this.CurrentSlide * ($(this.ID + ' .slide').width() + this.marginR) - (this.CurrentSlide == 0 ? -this.Delta : -this.Delta) }, { duration: 1200, easing: 'easeOutExpo', complete: function () { context.IsAnimated = false; if (context.Continue && context.CurrentSlide == (context.Lenght - context.VisibleSlide)) { context.nextSlide(context.VisibleSlide, true); } if (context.Continue && context.CurrentSlide == 0) { context.nextSlide(context.Lenght - context.VisibleSlide * 2, true); } } });
                }
                else {
                    $(this.ID + ' .slider').animate({ top: -this.CurrentSlide * ($(this.ID + ' .slide').height() + this.marginT) - (this.CurrentSlide == 0 ? -this.Delta : -this.Delta) }, { duration: 1200, easing: 'easeOutExpo', complete: function () { context.IsAnimated = false; if (context.Continue && context.CurrentSlide == (context.Lenght - context.VisibleSlide)) { context.nextSlide(context.VisibleSlide, true); } if (context.Continue && context.CurrentSlide == 0) { context.nextSlide(context.Lenght - context.VisibleSlide * 2, true); } } });
                }
            }
            $('.header_g .link a').removeClass('on').eq(this.CurrentSlide).addClass("on"); // Highlight de l'onglet dans la nav
        }
        if (this.ShowSlideCount) {
            $(this.ID + ' .slide-count > div').removeClass("slide-count-on");
            $(this.ID + ' .slide-count > div:eq(' + (this.CurrentSlide - (this.Continue ? 1 : 0)) + ')').addClass("slide-count-on");
        }

        this.clearTimer();
        this.setTimer();
    }
}
