jQuery(document).ready(function() {
    
    jQuery.each(jQuery(".jcountdown-open"), function(i,value){
        var cd = jQuery(value);
        jQuery(value).countdown({
            date: cd.attr('time'),
            onComplete: function( event ) {
                jQuery.post('/web/produits/open/', {}, function (data) {
                    if (data.success) {
                        top.location.reload(true);
                    } else {
                    }
                }, 'json');
            },
            leadingZero: true,
            direction: "down",
            htmlTemplate: "%{d} <span class='cd-time'>Jours</span> %{h} <span class='cd-time'>Heures</span> %{m} <span class='cd-time'>mins</span> %{s} <span class='cd-time'>sec</span>"
        });
    });
    
    jQuery.each(jQuery(".jcountdown-close"), function(i,value){
        var cd = jQuery(value);
        
        jQuery(value).countdown({
            date: cd.attr('time'),
            onComplete: function( event ) {
                var me = this;
                jQuery.post('/web/produits/close/', {}, function (data) {
                    if (data.success) {
                        cd.parents("#produits").find('.mise-action').hide();
                        cd.parents("#produits").find('.ribbon').show();
                        $(me).html("<a href='javascript:top.location.reload(true);'>Cliquer ici pour connaître le gagnant de ce lot.</a>");
                    } else {
                    }
                }, 'json');
            },
            leadingZero: true,
            direction: "down",
            htmlTemplate: "%{d} <span class='cd-time'>Jours</span> %{h} <span class='cd-time'>Heures</span> %{m} <span class='cd-time'>mins</span> %{s} <span class='cd-time'>sec</span>"
        });
    });
    
    
        
    jQuery('.mise-liste').click(function(){
        jQuery(this).parent().next().slideToggle();
    });
        
    jQuery( '#slider' ).slider({
        step:5,
        min:5,
        max:1000,
        change:function(event, ui){
            var bet = jQuery('#slider').parents("#popupOver").find(".action-mise").attr('bet');
            jQuery('.mise-value').html(ui.value + '.00 $');
            jQuery('.mise-value-final').html(( ui.value + parseFloat(bet) ) + '.00 $');
        }
    });
        
    jQuery('.mise-action').click(function(){
        var step = jQuery(this).attr('step');
        var ref = jQuery(this).attr('ref');
        var betValue = jQuery(this).attr('bet');
        bet(this,step,'#slider','#popupOver',ref,betValue);
    });
        
    jQuery('.action-cancel').click(function(){
        jQuery('#popupOver').fadeOut('fast');
    });
        
    jQuery('.action-mise').click(function(){
        var bnt = jQuery(this);
        var ref = jQuery(this).attr('ref');
        
        var mise = jQuery('#slider').slider( 'option', 'value' );
            
        bnt.parent().find('.ajax-loader-w').show();
        disable(bnt,true);
            
        bnt.parent().find('.alert').hide();
            
        jQuery.post('/web/bets/bet/', {
            item_id:ref,
            bet:mise
        }, function (data) {
            if (data.success) {
                bnt.parent().find('.ajax-loader-w').hide();
                bnt.parent().find('.info').html(data.message);
                bnt.parent().find('.info').fadeIn('fast');
                betRepos('#popupOver');
                window.setTimeout(function () {
                    top.location.reload(true);
                },2000);
            } else {
                bnt.parent().find('.ajax-loader-w').hide();
                disable(bnt,false);
                bnt.parent().find('.alert').html(data.message);
                bnt.parent().find('.alert').fadeIn('fast');
                betRepos('#popupOver');
            }
        }, 'json');
    });
        
    function disable(item , value ){
        if( value ){
            item.attr('disabled', 'disabled');
            item.fadeTo('fast', 0.5);
        }else{
            item.removeAttr('disabled');
            item.fadeTo('fast', 1);
        }
    }
});
