var menu, vf, vf_container, vfc_xpos, vf_cat, vf_bbox, chrono;
var rubriques = new Array();
rubriques[114] = 'bio-ecolo';
rubriques[118] = 'co-brande';
rubriques[119] = 'bien-etre';
rubriques[121] = 'decouverte';
rubriques[122] = 'saveurs';

$(function(){
    menu = $('#menu_vflash');
    vf = $('#ventes-flash');
    vf_cat = window.location.href;
    vf_cat = new RegExp(/ventes-flash\/([a-z-]+)$/gi).test(vf_cat) ? vf_cat.substr(vf_cat.lastIndexOf('/')+1) : 'en-cours';
    vf_bbox = 0;
    if(vf_cat !== null) getVentesFlash();
    
    $('label',menu).click(function(){
        vf_cat = $(this).attr('id');
        vf_cat = vf_cat.replace('vf-','');
        vf_bbox = 0;
        $(this).addClass('active').siblings('label').removeClass('active');
        getVentesFlash();
    });
    
    $('span.tps_restant').live('click',function(){
        $(this).text(ts_diff($(this).next('span.ts_fin').text()));
    });
    
    $('#filtres_vflash label').click(function(){
        vf_bbox = $(this).attr('id');
        vf_bbox = vf_bbox.replace('filtre-vf-','');
        $(this).addClass('active').siblings('label').removeClass('active');
        getVentesFlash();
    });
    
    chrono = setInterval(update_ts,1000);
    
    vf_container = $('#ventes-flash-container > div');
    $(vf_container).width(function(){
        return $('div.vente',this).length * 155;
    });
    slideVentesFlash();
});

function getVentesFlash(){
    $.ajax({
        url : '/xml-ventes-flash.php',
        data : 'cat=' + vf_cat + '&bbox=' + vf_bbox,
        cache : false,
        success : function(xml){
            var html = '';
            var vente;
            var ventes = $('vente',xml);
            var nb_ventes = $(ventes).length;
            if(nb_ventes > 0){
                for(i=0; i < nb_ventes; i++){
                    vente = $(ventes[i]);
					lien = $('biobox_url',vente).text();
					nondeb = $('nondeb',vente).text();
                    html += '<div class="vente ' + rubriques[$(vente).attr('rubrique')] + '">' + "\n" +
                                '<div class="photo">' + "\n" +
                                    '<img src="/images/partenaire_box/original/' + $('image',vente).text() + '" alt="' + $('image',vente).text() + '" />' + "\n" +
                                '</div>' + "\n" +
                                '<div class="tps_restant">' + "\n";
								if(nondeb == 1){
									html += '<strong>Début vente dans :</strong><br />' + "\n" +
									'<span class="tps_restant"></span>' + "\n" +
									'<span class="ts_fin">' + $(vente).attr('debut') + '</span>' + "\n";
								}else{
                                    html += '<strong>Fin de la vente dans :</strong><br />' + "\n" +
									'<span class="tps_restant"></span>' + "\n" +
									'<span class="ts_fin">' + $(vente).attr('fin') + '</span>' + "\n";
									
								}
                                    html += '</div>' + "\n" +
                                '<div class="prix">' + "\n" +
                                    '<span class="region">' + $('region',vente).text() + '</span>' + "\n" +
                                    '<p>' + "\n" +
                                        '<span>' + $(vente).attr('promo') + '</span> &euro;<br />' + "\n"; 
                                       if($(vente).attr('prix')>$(vente).attr('promo')) { html +=  'au lieu de ' + $(vente).attr('prix') + ' &euro;<br />' + "\n"; }
                                        html += '<span class="nb_personnes">' +
                                        ($('nb_personnes',vente).text() !== '' ? $('nb_personnes',vente).text() : ($(vente).attr('rubrique') == 119 ? 'Pour 1 personne' : 'Pour 2 personnes')) +
                                        '</span>' + "\n" +
                                    '</p>' + "\n" +
                                '</div>' + "\n" +
                                '<h3>' + $('designation',vente).text() + '</h3>' + "\n" +
                                '<p class="description">' + $('description',vente).text() + '</p>' + "\n";
								if(lien != ''){
                                 html += '<a href="/' + $('biobox_url',vente).text() + '&vf=1">&gt; accéder à la vente</a>' + "\n";
								}
                             html += '</div>' + "\n";
                }
            }
            else {
                html = "<p class=\"noresult\">Aucune vente n'est disponible pour l'instant.</p>\n";
            }
            $(vf).html(html);
        }
    });
}

function slideVentesFlash(){
    $(vf_container).delay(5000).animate({ left:'-=155' },300,'swing',function(){
        vfc_xpos = $(this).css('left');
        vfc_xpos = Math.abs(vfc_xpos.replace('px',''));
        if( ($('div.vente',this).length-4) * 155 <= vfc_xpos ){
            $(this).delay(5000).animate({ left:0 },300,'swing',slideVentesFlash);
        } else {
            slideVentesFlash();
        }
    });
}

function update_ts(){
    var ts;
    $('span.tps_restant').each(function(){
        ts = parseInt($(this).next('span.ts_fin').text());
        if(ts - Math.ceil(new Date().getTime()/1000) <= 0){
            $(this).parents('div.vente').remove();
            if(vf_cat === null){
                window.location.reload();
            }
        } else {
            $(this).text(ts_diff(ts));
        }
    });
}

function check_ts(form){
    var ts = parseInt( $(form).parent('div').children('div.footer').children('span.ts_fin').text() ) - Math.ceil(new Date().getTime()/1000);
    if(ts <= 0) alert('Cette vente est terminée.');
    return ts <= 0 ? false : true;
}

function ts_diff(ts){
    if(typeof ts === 'undefined') return false;
    var d, h, m, s;
    var tps = new Array();
    ts -= Math.ceil(new Date().getTime()/1000);
    d = Math.floor(ts/86400);
    h = Math.floor((ts - d*86400)/3600);
    m = Math.floor((ts - d*86400 - h*3600)/60);
    s = ts - d*86400 - h*3600 - m*60;
    if(d > 0) tps.push(d + 'j');
    if(h > 0) tps.push(h + 'h');
    if(m > 0) tps.push(m + 'm');
    tps.push(s + 's');
    tps = tps.join(' / ');
    return tps;
}
