﻿$(document).ready(function() {
    if (isHomePage()) {
        initHomePageCarousel();
    }
});

var hp_current = 0;
var hp_previous = 0;
var hp_count = null;
var hp_timer = null;
var hp_timer_ms = 6000;

function isHomePage() {
    return $('.Accueil').length > 0;
}

function initHomePageCarousel() {
    hp_count = $('.teaser').length;
    $('.teaser').each(function(index) {
        $(this).hover(function() {
            if(index != hp_current)
                initHomePageCarouselItem(index, this);
        });
    });
    initHomePageCarouselTimeout();
}

function initHomePageCarouselItem(index, elem) {
    clearTimeout(hp_timer);
    
    if (isUndefined(index)) {
        if (hp_current >= hp_count - 1)
            hp_current = 0;
        else
            hp_current++;

        elem = $('.teaser:eq(' + hp_current + ')');
    }
    else {
        hp_current = index;
    }

    $('.teaser').children('a').removeClass('on');
    $('.full:eq(' + hp_previous + ')').fadeOut('fast');
    $('.full:eq(' + hp_current + ')').fadeIn('fast');
    $(elem).children('a').addClass('on');

    hp_previous = hp_current;
    initHomePageCarouselTimeout();
}

function initHomePageCarouselTimeout() {
    hp_timer = setTimeout('initHomePageCarouselItem()', hp_timer_ms);
}

function isUndefined(data) {
    return data == null || data == 'undefined';
}