//var actif = 0;
var first = 1;
var cur_diap = false;
var diapos = new Array();
var boutons = new Array();
var is_timeout = false;
var delai = 5000;

jQuery(document).ready(function() {
    var nbDiapos = $('#animation-offre').children('a').length;
    for (i = 1; i <= nbDiapos; i++) {
        diapos[i] = $('#onglet' + i + '_img');
        boutons[i] = $('#onglet' + i);
    }
    diapo_suivante();
    $('div[rel*=diapo_home]').hover(function(){
        if (is_timeout) {
            clearTimeout(timeout);
            is_timeout = false;
        }
        var btn_id = intval(str_replace('onglet', '', $(this).attr('id')));
        active_diapo(btn_id);
    }, function(){
        if (!is_timeout) {
            timeout = setTimeout(function() { diapo_suivante(); }, delai);
            is_timeout = true;
        }
    });
});

function diapo_suivante() {
    var nbDiapos = $('#animation-offre').children('a').length;
    if (!cur_diap) {
        var next_diapo = first;
    } else {
        if (cur_diap >= nbDiapos) next_diapo = 1;
        else next_diapo = cur_diap + 1;
    }
    active_diapo(next_diapo);
    timeout = setTimeout(function() { diapo_suivante(); }, delai);
    is_timeout = true;
}

function active_diapo(id) {
    var nbDiapos = $('#animation-offre').children('a').length;
    if (cur_diap == id) return;
    var btn = boutons[id];
    var diapo = diapos[id];

    for (i = 1; i <= nbDiapos; i++) {
        if (id == i) continue;
        diapos[i].hide();
        boutons[i].removeClass('onglet' + i + '-actif');
    }
    diapo.fadeIn('slow');
    btn.addClass('onglet' + id + '-actif');
    cur_diap = id;
}
function str_replace (search, replace, subject, count) {
            f = [].concat(search),
            r = [].concat(replace),
            s = subject,
            ra = r instanceof Array, sa = s instanceof Array;    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }
     for (i=0, sl=s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j=0, fl=f.length; j < fl; j++) {            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length-s[i].length)/f[j].length;}        }
    }
    return sa ? s : s[0];
}
function intval (mixed_var, base) {

    var type = typeof( mixed_var );

    if (type === 'boolean') {        return (mixed_var) ? 1 : 0;
    } else if (type === 'string') {
        tmp = parseInt(mixed_var, base || 10);
        return (isNaN(tmp) || !isFinite(tmp)) ? 0 : tmp;
    } else if (type === 'number' && isFinite(mixed_var) ) {        return Math.floor(mixed_var);
    } else {
        return 0;
    }
}

function soumettre(id) {
    document.getElementById(id).target = 'formulaire';
    window.open('', 'formulaire','width=280,height=280');
}

